Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.48% |
19 / 21 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ValidCreateResourceApiCall | |
90.48% |
19 / 21 |
|
60.00% |
3 / 5 |
6.03 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| shouldDoRequestValidation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| shouldDoResponseValidation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRequest | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| verifyValidResponse | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\IntegrationTests\Requests; |
| 4 | |
| 5 | use Apie\Common\IntegrationTestLogger; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\Entities\EntityInterface; |
| 8 | use Apie\IntegrationTests\Requests\JsonFields\JsonGetFieldInterface; |
| 9 | use Apie\IntegrationTests\Requests\JsonFields\JsonSetFieldInterface; |
| 10 | use Nyholm\Psr7\ServerRequest; |
| 11 | use PHPUnit\Framework\TestCase; |
| 12 | use Psr\Http\Message\ResponseInterface; |
| 13 | use Psr\Http\Message\ServerRequestInterface; |
| 14 | use ReflectionClass; |
| 15 | |
| 16 | class ValidCreateResourceApiCall implements TestRequestInterface |
| 17 | { |
| 18 | /** |
| 19 | * @param class-string<EntityInterface> $resourceName |
| 20 | */ |
| 21 | public function __construct( |
| 22 | private readonly BoundedContextId $boundedContextId, |
| 23 | private readonly string $resourceName, |
| 24 | private readonly JsonGetFieldInterface&JsonSetFieldInterface $inputOutput, |
| 25 | private readonly bool $discardRequestValidation = false, |
| 26 | private readonly bool $discardResponseValidation = false |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | public function shouldDoRequestValidation(): bool |
| 31 | { |
| 32 | return !$this->discardRequestValidation; |
| 33 | } |
| 34 | |
| 35 | public function shouldDoResponseValidation(): bool |
| 36 | { |
| 37 | return !$this->discardResponseValidation; |
| 38 | } |
| 39 | |
| 40 | public function getRequest(): ServerRequestInterface |
| 41 | { |
| 42 | $data = $this->inputOutput->getInputValue(); |
| 43 | return new ServerRequest( |
| 44 | 'POST', |
| 45 | 'http://localhost/api/' . $this->boundedContextId . '/' . (new ReflectionClass($this->resourceName))->getShortName(), |
| 46 | [ |
| 47 | 'content-type' => 'application/json', |
| 48 | 'accept' => 'application/json', |
| 49 | ], |
| 50 | json_encode($data) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | public function verifyValidResponse(ResponseInterface $response): void |
| 55 | { |
| 56 | $body = (string) $response->getBody(); |
| 57 | $statusCode = $response->getStatusCode(); |
| 58 | if ($statusCode === 500) { |
| 59 | IntegrationTestLogger::failTestShowError(); |
| 60 | } |
| 61 | TestCase::assertEquals(201, $statusCode, 'Expect object created, got: ' . $body); |
| 62 | $data = json_decode($body, true); |
| 63 | $this->inputOutput->assertResponseValue($data); |
| 64 | TestCase::assertEquals('application/json', $response->getHeaderLine('content-type')); |
| 65 | } |
| 66 | } |