|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @see https://github.com/zendframework/zend-expressive for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) |
| 5 | + * @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace ZendTest\Expressive\Response; |
| 11 | + |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Prophecy\Argument; |
| 14 | +use Prophecy\Prophecy\ObjectProphecy; |
| 15 | +use Psr\Http\Message\ResponseInterface; |
| 16 | +use Psr\Http\Message\StreamInterface; |
| 17 | +use Throwable; |
| 18 | +use RuntimeException; |
| 19 | +use Zend\Expressive\Response\ServerRequestErrorResponseGenerator; |
| 20 | +use Zend\Expressive\Template\TemplateRendererInterface; |
| 21 | +use Zend\Stratigility\Utils; |
| 22 | + |
| 23 | +class ServerRequestErrorResponseGeneratorTest extends TestCase |
| 24 | +{ |
| 25 | + /** @var TemplateRendererInterface|ObjectProphecy */ |
| 26 | + private $renderer; |
| 27 | + |
| 28 | + /** @var ResponseInterface|ObjectProphecy */ |
| 29 | + private $response; |
| 30 | + |
| 31 | + /** @var callable */ |
| 32 | + private $responseFactory; |
| 33 | + |
| 34 | + public function setUp() |
| 35 | + { |
| 36 | + $this->response = $this->prophesize(ResponseInterface::class); |
| 37 | + $this->responseFactory = function () { |
| 38 | + return $this->response->reveal(); |
| 39 | + }; |
| 40 | + |
| 41 | + $this->renderer = $this->prophesize(TemplateRendererInterface::class); |
| 42 | + } |
| 43 | + |
| 44 | + public function testPreparesTemplatedResponseWhenRendererPresent() |
| 45 | + { |
| 46 | + $stream = $this->prophesize(StreamInterface::class); |
| 47 | + $stream->write('data from template')->shouldBeCalled(); |
| 48 | + |
| 49 | + $this->response->withStatus(422)->will([$this->response, 'reveal']); |
| 50 | + $this->response->getBody()->will([$stream, 'reveal']); |
| 51 | + $this->response->getStatusCode()->willReturn(422); |
| 52 | + $this->response->getReasonPhrase()->willReturn('Unexpected entity'); |
| 53 | + |
| 54 | + $template = 'some::template'; |
| 55 | + $e = new RuntimeException('This is the exception message', 422); |
| 56 | + $this->renderer |
| 57 | + ->render($template, [ |
| 58 | + 'response' => $this->response->reveal(), |
| 59 | + 'status' => 422, |
| 60 | + 'reason' => 'Unexpected entity', |
| 61 | + 'error' => $e, |
| 62 | + ]) |
| 63 | + ->willReturn('data from template'); |
| 64 | + |
| 65 | + $generator = new ServerRequestErrorResponseGenerator( |
| 66 | + $this->responseFactory, |
| 67 | + true, |
| 68 | + $this->renderer->reveal(), |
| 69 | + $template |
| 70 | + ); |
| 71 | + |
| 72 | + $this->assertSame($this->response->reveal(), $generator($e)); |
| 73 | + } |
| 74 | + |
| 75 | + public function testPreparesResponseWithDefaultMessageOnlyWhenNoRendererPresentAndNotInDebugMode() |
| 76 | + { |
| 77 | + $stream = $this->prophesize(StreamInterface::class); |
| 78 | + $stream->write('An unexpected error occurred')->shouldBeCalled(); |
| 79 | + |
| 80 | + $this->response->withStatus(422)->will([$this->response, 'reveal']); |
| 81 | + $this->response->getBody()->will([$stream, 'reveal']); |
| 82 | + $this->response->getStatusCode()->shouldNotBeCalled(); |
| 83 | + $this->response->getReasonPhrase()->shouldNotBeCalled(); |
| 84 | + |
| 85 | + $e = new RuntimeException('This is the exception message', 422); |
| 86 | + |
| 87 | + $generator = new ServerRequestErrorResponseGenerator($this->responseFactory); |
| 88 | + |
| 89 | + $this->assertSame($this->response->reveal(), $generator($e)); |
| 90 | + } |
| 91 | + |
| 92 | + public function testPreparesResponseWithDefaultMessageAndStackTraceWhenNoRendererPresentAndInDebugMode() |
| 93 | + { |
| 94 | + $stream = $this->prophesize(StreamInterface::class); |
| 95 | + $stream |
| 96 | + ->write(Argument::that(function ($message) { |
| 97 | + if (! preg_match('/^An unexpected error occurred; stack trace:/', $message)) { |
| 98 | + echo "Failed first assertion: $message\n"; |
| 99 | + return false; |
| 100 | + } |
| 101 | + if (false === strpos($message, 'Stack Trace:')) { |
| 102 | + echo "Failed second assertion: $message\n"; |
| 103 | + return false; |
| 104 | + } |
| 105 | + return $message; |
| 106 | + })) |
| 107 | + ->shouldBeCalled(); |
| 108 | + |
| 109 | + $this->response->withStatus(422)->will([$this->response, 'reveal']); |
| 110 | + $this->response->getBody()->will([$stream, 'reveal']); |
| 111 | + $this->response->getStatusCode()->shouldNotBeCalled(); |
| 112 | + $this->response->getReasonPhrase()->shouldNotBeCalled(); |
| 113 | + |
| 114 | + $e = new RuntimeException('This is the exception message', 422); |
| 115 | + |
| 116 | + $generator = new ServerRequestErrorResponseGenerator($this->responseFactory, true); |
| 117 | + |
| 118 | + $this->assertSame($this->response->reveal(), $generator($e)); |
| 119 | + } |
| 120 | +} |
0 commit comments