Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.45% |
21 / 22 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| GetResourceApiCall | |
95.45% |
21 / 22 |
|
83.33% |
5 / 6 |
11 | |
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 |
2 | |||
| bootstrap | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getRequest | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| verifyValidResponse | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\IntegrationTests\Requests; |
| 4 | |
| 5 | use Apie\Common\Interfaces\ApieFacadeInterface; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\Entities\EntityInterface; |
| 8 | use Apie\Faker\Datalayers\FakerDatalayer; |
| 9 | use Apie\IntegrationTests\Interfaces\TestApplicationInterface; |
| 10 | use Apie\IntegrationTests\Requests\JsonFields\JsonSetFieldInterface; |
| 11 | use Nyholm\Psr7\ServerRequest; |
| 12 | use PHPUnit\Framework\TestCase; |
| 13 | use Psr\Http\Message\ResponseInterface; |
| 14 | use Psr\Http\Message\ServerRequestInterface; |
| 15 | use ReflectionClass; |
| 16 | |
| 17 | class GetResourceApiCall implements TestRequestInterface, BootstrapRequestInterface |
| 18 | { |
| 19 | protected bool $faked = false; |
| 20 | /** |
| 21 | * @param class-string<EntityInterface> $resourceName |
| 22 | * @param array<int, EntityInterface> $entities |
| 23 | */ |
| 24 | public function __construct( |
| 25 | private readonly BoundedContextId $boundedContextId, |
| 26 | private readonly string $resourceName, |
| 27 | private readonly string $id, |
| 28 | private readonly array $entities, |
| 29 | private readonly JsonSetFieldInterface $inputOutput, |
| 30 | private readonly bool $discardValidationOnFaker = false, |
| 31 | ) { |
| 32 | } |
| 33 | |
| 34 | public function shouldDoRequestValidation(): bool |
| 35 | { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | public function shouldDoResponseValidation(): bool |
| 40 | { |
| 41 | return !$this->discardValidationOnFaker || !$this->faked; |
| 42 | ; |
| 43 | } |
| 44 | |
| 45 | public function bootstrap(TestApplicationInterface $testApplication): void |
| 46 | { |
| 47 | /** @var ApieFacadeInterface $apieFacade */ |
| 48 | $apieFacade = $testApplication->getServiceContainer()->get('apie'); |
| 49 | foreach ($this->entities as $entity) { |
| 50 | $apieFacade->persistNew($entity, $this->boundedContextId); |
| 51 | } |
| 52 | $this->faked = $testApplication->getApplicationConfig()->getDatalayerImplementation()->name === FakerDatalayer::class; |
| 53 | } |
| 54 | |
| 55 | public function getRequest(): ServerRequestInterface |
| 56 | { |
| 57 | return new ServerRequest( |
| 58 | 'GET', |
| 59 | 'http://localhost/api/' . $this->boundedContextId . '/' . (new ReflectionClass($this->resourceName))->getShortName() . '/' . $this->id, |
| 60 | [ |
| 61 | 'accept' => 'application/json', |
| 62 | ] |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function verifyValidResponse(ResponseInterface $response): void |
| 67 | { |
| 68 | $body = (string) $response->getBody(); |
| 69 | $statusCode = $response->getStatusCode(); |
| 70 | if (!$this->faked || !$this->discardValidationOnFaker) { |
| 71 | TestCase::assertEquals(200, $statusCode, 'Expect object retrieved, got: ' . $body); |
| 72 | } |
| 73 | $data = json_decode($body, true); |
| 74 | if (!$this->faked) { |
| 75 | $this->inputOutput->assertResponseValue($data); |
| 76 | } |
| 77 | TestCase::assertEquals('application/json', $response->getHeaderLine('content-type')); |
| 78 | } |
| 79 | } |