Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 84dc641

Browse files
committed
Adds tests covering ServerRequestErrorResponseGenerator behavior
Corrects an issue in the default message when a stack trace is produced as well.
1 parent 6329c34 commit 84dc641

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

src/Response/ErrorResponseGeneratorTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function prepareDefaultResponse(Throwable $e, ResponseInterface $respons
6565
$message = 'An unexpected error occurred';
6666

6767
if ($this->debug) {
68-
$message .= "; strack trace:\n\n" . $this->prepareStackTrace($e);
68+
$message .= "; stack trace:\n\n" . $this->prepareStackTrace($e);
6969
}
7070

7171
$response->getBody()->write($message);

src/Response/ServerRequestErrorResponseGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public function __construct(
4545
$this->template = $template;
4646
}
4747

48-
public function __invoke(Throwable $e)
48+
public function __invoke(Throwable $e) : ResponseInterface
4949
{
50-
$response = ($this->responseFactory)()
51-
->withStatus(Utils::getStatusCode($e, $response));
50+
$response = ($this->responseFactory)();
51+
$response = $response->withStatus(Utils::getStatusCode($e, $response));
5252

5353
if ($this->renderer) {
5454
return $this->prepareTemplatedResponse($e, $response, [
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)