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