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

Commit 57efae3

Browse files
author
Mateusz Lopacinski
committed
Add JsonResponseHandler test case, add previously forgotten import
1 parent 731167f commit 57efae3

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Middleware/WhoopsErrorResponseGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use InvalidArgumentException;
1111
use Psr\Http\Message\ResponseInterface;
1212
use Psr\Http\Message\ServerRequestInterface;
13+
use Whoops\Handler\JsonResponseHandler;
1314
use Whoops\Handler\PrettyPageHandler;
1415
use Whoops\Run;
1516
use Whoops\RunInterface;

test/Middleware/WhoopsErrorResponseGeneratorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Psr\Http\Message\ServerRequestInterface;
1414
use Psr\Http\Message\StreamInterface;
1515
use RuntimeException;
16+
use Whoops\Handler\JsonResponseHandler;
1617
use Whoops\Handler\PrettyPageHandler;
1718
use Whoops\Run;
1819
use Whoops\RunInterface;
@@ -111,4 +112,37 @@ public function testAddsRequestMetadataToWhoopsPrettyPageHandler()
111112
$generator($error, $this->request->reveal(), $this->response->reveal())
112113
);
113114
}
115+
116+
public function testJsonContentTypeResponseWithJsonResponseHandler()
117+
{
118+
$error = new RuntimeException();
119+
120+
$handler = $this->prophesize(JsonResponseHandler::class);
121+
$handler->contentType()->willReturn('application/json');
122+
123+
$this->whoops->getHandlers()->willReturn([$handler->reveal()]);
124+
$this->whoops->handleException($error)->willReturn('error');
125+
126+
$this->request->getAttribute('originalUri', false)->willReturn('https://example.com/foo');
127+
$this->request->getAttribute('originalRequest', false)->will([$this->request, 'reveal']);
128+
$this->request->getMethod()->willReturn('POST');
129+
$this->request->getServerParams()->willReturn(['SCRIPT_NAME' => __FILE__]);
130+
$this->request->getHeaders()->willReturn([]);
131+
$this->request->getCookieParams()->willReturn([]);
132+
$this->request->getAttributes()->willReturn([]);
133+
$this->request->getQueryParams()->willReturn([]);
134+
$this->request->getParsedBody()->willReturn([]);
135+
136+
$this->response->withHeader('Content-Type', 'application/json')->will([$this->response, 'reveal']);
137+
$this->response->getBody()->will([$this->stream, 'reveal']);
138+
139+
$this->stream->write('error')->shouldBeCalled();
140+
141+
$generator = new WhoopsErrorResponseGenerator($this->whoops->reveal());
142+
143+
$this->assertSame(
144+
$this->response->reveal(),
145+
$generator($error, $this->request->reveal(), $this->response->reveal())
146+
);
147+
}
114148
}

0 commit comments

Comments
 (0)