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