|
13 | 13 | use PHPUnit\Framework\Assert;
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Prophecy\Argument;
|
| 16 | +use Throwable; |
| 17 | +use TypeError; |
16 | 18 | use Psr\Http\Message\ResponseInterface;
|
17 | 19 | use Psr\Http\Message\ServerRequestInterface;
|
18 | 20 | use Psr\Http\Server\RequestHandlerInterface;
|
@@ -80,4 +82,60 @@ public function testRunPassesRequestGeneratedByRequestFactoryToHandleWhenNoReque
|
80 | 82 |
|
81 | 83 | $this->assertNull($runner->run());
|
82 | 84 | }
|
| 85 | + |
| 86 | + public function testRaisesTypeErrorIfServerRequestFactoryDoesNotReturnARequestInstance() |
| 87 | + { |
| 88 | + $serverRequestFactory = function () { |
| 89 | + return null; |
| 90 | + }; |
| 91 | + |
| 92 | + $response = $this->prophesize(ResponseInterface::class)->reveal(); |
| 93 | + $errorResponseGenerator = function (Throwable $e) use ($response) { |
| 94 | + Assert::assertInstanceOf(TypeError::class, $e); |
| 95 | + return $response; |
| 96 | + }; |
| 97 | + |
| 98 | + $handler = $this->prophesize(RequestHandlerInterface::class); |
| 99 | + $handler->handle(Argument::any())->shouldNotBeCalled(); |
| 100 | + |
| 101 | + $emitter = $this->prophesize(EmitterInterface::class); |
| 102 | + $emitter->emit($response)->shouldBeCalled(); |
| 103 | + |
| 104 | + $runner = new RequestHandlerRunner( |
| 105 | + $handler->reveal(), |
| 106 | + $emitter->reveal(), |
| 107 | + $serverRequestFactory, |
| 108 | + $errorResponseGenerator |
| 109 | + ); |
| 110 | + |
| 111 | + $this->assertNull($runner->run()); |
| 112 | + } |
| 113 | + |
| 114 | + public function testRaisesTypeErrorIfServerErrorResponseGeneratorFactoryDoesNotReturnAResponse() |
| 115 | + { |
| 116 | + $serverRequestFactory = function () { |
| 117 | + return null; |
| 118 | + }; |
| 119 | + |
| 120 | + $errorResponseGenerator = function (Throwable $e) { |
| 121 | + Assert::assertInstanceOf(TypeError::class, $e); |
| 122 | + return null; |
| 123 | + }; |
| 124 | + |
| 125 | + $handler = $this->prophesize(RequestHandlerInterface::class); |
| 126 | + $handler->handle(Argument::any())->shouldNotBeCalled(); |
| 127 | + |
| 128 | + $emitter = $this->prophesize(EmitterInterface::class); |
| 129 | + $emitter->emit(Argument::any())->shouldNotBeCalled(); |
| 130 | + |
| 131 | + $runner = new RequestHandlerRunner( |
| 132 | + $handler->reveal(), |
| 133 | + $emitter->reveal(), |
| 134 | + $serverRequestFactory, |
| 135 | + $errorResponseGenerator |
| 136 | + ); |
| 137 | + |
| 138 | + $this->expectException(TypeError::class); |
| 139 | + $runner->run(); |
| 140 | + } |
83 | 141 | }
|
0 commit comments