|
24 | 24 | use function array_keys; |
25 | 25 | use function fclose; |
26 | 26 | use function fopen; |
| 27 | +use function json_decode; |
27 | 28 | use function stripos; |
28 | 29 |
|
29 | 30 | class ProblemDetailsResponseFactoryTest extends TestCase |
@@ -475,4 +476,55 @@ function (array $payload) { |
475 | 476 |
|
476 | 477 | $this->assertSame($this->response->reveal(), $response); |
477 | 478 | } |
| 479 | + |
| 480 | + public function provideMappedStatuses() : array |
| 481 | + { |
| 482 | + $defaultTypesMap = [ |
| 483 | + 404 => 'https://example.com/problem-details/error/not-found', |
| 484 | + 500 => 'https://example.com/problem-details/error/internal-server-error', |
| 485 | + ]; |
| 486 | + |
| 487 | + return [ |
| 488 | + [$defaultTypesMap, 404, 'https://example.com/problem-details/error/not-found'], |
| 489 | + [$defaultTypesMap, 500, 'https://example.com/problem-details/error/internal-server-error'], |
| 490 | + [$defaultTypesMap, 400, 'https://httpstatus.es/400'], |
| 491 | + [[], 500, 'https://httpstatus.es/500'], |
| 492 | + ]; |
| 493 | + } |
| 494 | + |
| 495 | + /** |
| 496 | + * @dataProvider provideMappedStatuses |
| 497 | + */ |
| 498 | + public function testTypeIsInferredFromDefaultTypesMap(array $map, int $status, string $expectedType) : void |
| 499 | + { |
| 500 | + $this->request->getHeaderLine('Accept')->willReturn('application/json'); |
| 501 | + |
| 502 | + $stream = $this->prophesize(StreamInterface::class); |
| 503 | + $writeStream = $stream->write(Argument::that(function (string $body) use ($expectedType) { |
| 504 | + $payload = json_decode($body, true); |
| 505 | + Assert::assertEquals($expectedType, $payload['type']); |
| 506 | + |
| 507 | + return $body; |
| 508 | + })); |
| 509 | + |
| 510 | + $this->response->getBody()->will([$stream, 'reveal']); |
| 511 | + $withStatus = $this->response->withStatus($status)->will([$this->response, 'reveal']); |
| 512 | + $this->response->withHeader('Content-Type', 'application/problem+json')->will([$this->response, 'reveal']); |
| 513 | + |
| 514 | + $factory = new ProblemDetailsResponseFactory( |
| 515 | + function () { |
| 516 | + return $this->response->reveal(); |
| 517 | + }, |
| 518 | + false, |
| 519 | + null, |
| 520 | + false, |
| 521 | + '', |
| 522 | + $map |
| 523 | + ); |
| 524 | + |
| 525 | + $factory->createResponse($this->request->reveal(), $status, 'detail'); |
| 526 | + |
| 527 | + $writeStream->shouldHaveBeenCalled(); |
| 528 | + $withStatus->shouldHaveBeenCalled(); |
| 529 | + } |
478 | 530 | } |
0 commit comments