Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.35% |
28 / 34 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ActionMethodApiCall | |
82.35% |
28 / 34 |
|
75.00% |
6 / 8 |
20.98 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withAdditionalHeaders | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 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% |
11 / 11 |
|
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 | /** @var array<string, string> */ |
| 27 | private array $additionalHeaders = []; |
| 28 | |
| 29 | /** |
| 30 | * @param array<int, EntityInterface> $entities |
| 31 | */ |
| 32 | public function __construct( |
| 33 | protected readonly BoundedContextId $boundedContextId, |
| 34 | protected readonly string $url, |
| 35 | protected readonly JsonGetFieldInterface|JsonSetFieldInterface $inputOutput, |
| 36 | private readonly bool $discardRequestValidation = false, |
| 37 | private readonly bool $discardResponseValidation = false, |
| 38 | protected readonly array $entities = [], |
| 39 | private readonly bool $discardValidationOnFaker = false, |
| 40 | protected readonly ?int $expectedAuditLogsAdded = null, |
| 41 | ) { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param array<string, string> $additionalHeaders |
| 46 | */ |
| 47 | public function withAdditionalHeaders(array $additionalHeaders): self |
| 48 | { |
| 49 | $new = clone $this; |
| 50 | $new->additionalHeaders = [...$this->additionalHeaders, ...$additionalHeaders]; |
| 51 | return $new; |
| 52 | } |
| 53 | |
| 54 | public function bootstrap(TestApplicationInterface $testApplication): void |
| 55 | { |
| 56 | $this->apieFacade = $testApplication->getServiceContainer()->get('apie'); |
| 57 | foreach ($this->entities as $entity) { |
| 58 | $this->apieFacade->persistNew($entity, $this->boundedContextId); |
| 59 | } |
| 60 | $this->faked = $testApplication->getApplicationConfig()->getDatalayerImplementation()->name === FakerDatalayer::class; |
| 61 | } |
| 62 | |
| 63 | public function isFakeDatalayer(): bool |
| 64 | { |
| 65 | return $this->faked; |
| 66 | } |
| 67 | |
| 68 | public function shouldDoRequestValidation(): bool |
| 69 | { |
| 70 | return !$this->discardRequestValidation && !($this->faked && $this->discardValidationOnFaker); |
| 71 | } |
| 72 | |
| 73 | public function shouldDoResponseValidation(): bool |
| 74 | { |
| 75 | return !$this->discardResponseValidation && !($this->faked && $this->discardValidationOnFaker); |
| 76 | } |
| 77 | |
| 78 | public function getRequest(): ServerRequestInterface |
| 79 | { |
| 80 | $data = $this->inputOutput instanceof JsonGetFieldInterface ? $this->inputOutput->getInputValue() : []; |
| 81 | return new ServerRequest( |
| 82 | 'POST', |
| 83 | 'http://localhost/api/' . $this->boundedContextId . '/' . $this->url, |
| 84 | [ |
| 85 | 'content-type' => 'application/json', |
| 86 | 'accept' => 'application/json', |
| 87 | ...$this->additionalHeaders, |
| 88 | ], |
| 89 | json_encode($data) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | public function verifyValidResponse(ResponseInterface $response): void |
| 94 | { |
| 95 | $body = (string) $response->getBody(); |
| 96 | $statusCode = $response->getStatusCode(); |
| 97 | if ($statusCode === 500) { |
| 98 | IntegrationTestLogger::failTestShowError(); |
| 99 | } |
| 100 | TestCase::assertEquals(200, $statusCode, 'Expect status code 200, got: ' . $body); |
| 101 | if ($this->shouldDoRequestValidation() && $this->inputOutput instanceof JsonSetFieldInterface) { |
| 102 | $data = json_decode($body, true); |
| 103 | $this->inputOutput->assertResponseValue($data); |
| 104 | } |
| 105 | TestCase::assertEquals('application/json', $response->getHeaderLine('content-type')); |
| 106 | if (null !== $this->expectedAuditLogsAdded && !$this->faked) { |
| 107 | $auditLogs = $this->apieFacade->all(new ReflectionClass(AuditLog::class), $this->boundedContextId); |
| 108 | TestCase::assertEquals($this->expectedAuditLogsAdded, $auditLogs->getTotalCount(), 'Expected ' . $this->expectedAuditLogsAdded . ' audit logs to be added, but got: ' . $auditLogs->getTotalCount()); |
| 109 | } |
| 110 | } |
| 111 | } |