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

Commit dadaccb

Browse files
committed
Added test to cover how the default types map works
1 parent 8499719 commit dadaccb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test/ProblemDetailsResponseFactoryTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use function array_keys;
2525
use function fclose;
2626
use function fopen;
27+
use function json_decode;
2728
use function stripos;
2829

2930
class ProblemDetailsResponseFactoryTest extends TestCase
@@ -475,4 +476,55 @@ function (array $payload) {
475476

476477
$this->assertSame($this->response->reveal(), $response);
477478
}
479+
480+
public function provideMappedStatuses() : array
481+
{
482+
$defaultTypes = [
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+
[$defaultTypes, 404, 'https://example.com/problem-details/error/not-found'],
489+
[$defaultTypes, 500, 'https://example.com/problem-details/error/internal-server-error'],
490+
[$defaultTypes, 400, 'https://httpstatus.es/400'],
491+
[[], 500, 'https://httpstatus.es/500'],
492+
];
493+
}
494+
495+
/**
496+
* @dataProvider provideMappedStatuses
497+
*/
498+
public function testTypeIsInferredFromDefaultTypesMap(array $defaultTypes, 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+
$witStatus = $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+
$defaultTypes
523+
);
524+
525+
$factory->createResponse($this->request->reveal(), $status, 'detail');
526+
527+
$writeStream->shouldHaveBeenCalled();
528+
$witStatus->shouldHaveBeenCalled();
529+
}
478530
}

0 commit comments

Comments
 (0)