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