Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
21 / 22
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetResourceListApiCall
95.45% covered (success)
95.45%
21 / 22
83.33% covered (warning)
83.33%
5 / 6
11
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 shouldDoRequestValidation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldDoResponseValidation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 bootstrap
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getRequest
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 verifyValidResponse
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace Apie\IntegrationTests\Requests;
4
5use Apie\Common\Interfaces\ApieFacadeInterface;
6use Apie\Core\BoundedContext\BoundedContextId;
7use Apie\Core\Entities\EntityInterface;
8use Apie\Faker\Datalayers\FakerDatalayer;
9use Apie\IntegrationTests\Interfaces\TestApplicationInterface;
10use Apie\IntegrationTests\Requests\JsonFields\JsonSetFieldInterface;
11use Nyholm\Psr7\ServerRequest;
12use PHPUnit\Framework\TestCase;
13use Psr\Http\Message\ResponseInterface;
14use Psr\Http\Message\ServerRequestInterface;
15use ReflectionClass;
16
17class GetResourceListApiCall implements TestRequestInterface, BootstrapRequestInterface
18{
19    protected bool $faked = false;
20    
21    /**
22     * @param class-string<EntityInterface> $resourceName
23     * @param array<int, EntityInterface> $entities
24     */
25    public function __construct(
26        private readonly BoundedContextId $boundedContextId,
27        private readonly string $resourceName,
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(),
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}