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