Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.14% |
23 / 28 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| GraphqlProvider | |
82.14% |
23 / 28 |
|
33.33% |
2 / 6 |
11.69 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| bootstrap | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| shouldDoRequestValidation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| shouldDoResponseValidation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRequest | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| verifyValidResponse | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
5.12 | |||
| 1 | <?php |
| 2 | namespace Apie\IntegrationTests\Graphql; |
| 3 | |
| 4 | use Apie\Common\IntegrationTestLogger; |
| 5 | use Apie\Common\Interfaces\ApieFacadeInterface; |
| 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\BootstrapRequestInterface; |
| 11 | use Apie\IntegrationTests\Requests\TestRequestInterface; |
| 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 GraphqlProvider implements TestRequestInterface, BootstrapRequestInterface |
| 18 | { |
| 19 | private bool $faked = false; |
| 20 | /** |
| 21 | * @param array<string, mixed> $graphQlQuery |
| 22 | * @param array<string, mixed> $expectedResponse |
| 23 | * @param array<int, EntityInterface> $entities |
| 24 | */ |
| 25 | public function __construct( |
| 26 | private readonly BoundedContextId $boundedContextId, |
| 27 | protected readonly array $graphQlQuery, |
| 28 | protected array $expectedResponse, |
| 29 | protected array $entities = [], |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | public function bootstrap(TestApplicationInterface $testApplication): void |
| 34 | { |
| 35 | /** @var ApieFacadeInterface $apieFacade */ |
| 36 | $apieFacade = $testApplication->getServiceContainer()->get('apie'); |
| 37 | foreach ($this->entities as $entity) { |
| 38 | $apieFacade->persistNew($entity, $this->boundedContextId); |
| 39 | } |
| 40 | $this->faked = $testApplication->getApplicationConfig()->getDatalayerImplementation()->name === FakerDatalayer::class; |
| 41 | } |
| 42 | |
| 43 | public function shouldDoRequestValidation(): bool |
| 44 | { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | public function shouldDoResponseValidation(): bool |
| 49 | { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | public function getRequest(): ServerRequestInterface |
| 54 | { |
| 55 | return new ServerRequest( |
| 56 | 'POST', |
| 57 | 'http://localhost/' . $this->boundedContextId . '/graphql', |
| 58 | [ |
| 59 | 'content-type' => 'application/json', |
| 60 | 'accept' => 'application/json', |
| 61 | ], |
| 62 | json_encode($this->graphQlQuery) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function verifyValidResponse(ResponseInterface $response): void |
| 67 | { |
| 68 | $body = (string) $response->getBody(); |
| 69 | $statusCode = $response->getStatusCode(); |
| 70 | if ($statusCode === 500) { |
| 71 | IntegrationTestLogger::failTestShowError(); |
| 72 | } |
| 73 | TestCase::assertEquals(200, $statusCode, 'Expect object created, got: ' . $body); |
| 74 | $data = json_decode($body, true); |
| 75 | $error = isset($data['errors']) ? json_encode($data['errors'], JSON_PRETTY_PRINT) : $body; |
| 76 | if (IntegrationTestLogger::getLoggedException()) { |
| 77 | IntegrationTestLogger::failTestShowError($statusCode); |
| 78 | } |
| 79 | if (!$this->faked) { |
| 80 | TestCase::assertEquals($this->expectedResponse, $data, 'Expected response is not right, got: ' . $error); |
| 81 | } |
| 82 | TestCase::assertEquals('application/json', $response->getHeaderLine('content-type')); |
| 83 | } |
| 84 | } |