|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rebing\GraphQL\Tests\Database; |
| 6 | + |
| 7 | +use Rebing\GraphQL\Tests\TestCaseDatabase; |
| 8 | + |
| 9 | +class EmptyQueryTest extends TestCaseDatabase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @dataProvider dataForEmptyQuery |
| 13 | + * @param array<mixed> $parameters |
| 14 | + * @param bool $isBatchRequest |
| 15 | + * @param bool $expectErrors |
| 16 | + */ |
| 17 | + public function testEmptyQuery(array $parameters, bool $isBatchRequest, bool $expectErrors): void |
| 18 | + { |
| 19 | + $response = $this->call('GET', '/graphql', $parameters); |
| 20 | + |
| 21 | + $this->assertSame(200, $response->getStatusCode()); |
| 22 | + $results = $isBatchRequest ? $response->getData(true) : [$response->getData(true)]; |
| 23 | + |
| 24 | + foreach ($results as $result) { |
| 25 | + if ($expectErrors) { |
| 26 | + $this->assertCount(1, $result['errors']); |
| 27 | + $this->assertSame('Syntax Error: Unexpected <EOF>', $result['errors'][0]['message']); |
| 28 | + $this->assertSame('graphql', $result['errors'][0]['extensions']['category']); |
| 29 | + } else { |
| 30 | + $this->assertArrayNotHasKey('errors', $result); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return array<mixed> |
| 37 | + */ |
| 38 | + public function dataForEmptyQuery(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + // completely empty request |
| 42 | + [ |
| 43 | + 'parameters' => [], |
| 44 | + 'isBatchRequest' => false, |
| 45 | + 'expectErrors' => false, |
| 46 | + ], |
| 47 | + // single request with an empty query parameter |
| 48 | + [ |
| 49 | + 'parameters' => ['query' => null], |
| 50 | + 'isBatchRequest' => false, |
| 51 | + 'expectErrors' => true, |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'parameters' => ['query' => ''], |
| 55 | + 'isBatchRequest' => false, |
| 56 | + 'expectErrors' => true, |
| 57 | + ], |
| 58 | + [ |
| 59 | + 'parameters' => ['query' => ' '], |
| 60 | + 'isBatchRequest' => false, |
| 61 | + 'expectErrors' => true, |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'parameters' => ['query' => '#'], |
| 65 | + 'isBatchRequest' => false, |
| 66 | + 'expectErrors' => true, |
| 67 | + ], |
| 68 | + // batch request with one completely empty batch, and batches with an empty query parameter |
| 69 | + [ |
| 70 | + 'parameters' => [[], ['query' => null], ['query' => ''], ['query' => ' '], ['query' => '#']], |
| 71 | + 'isBatchRequest' => true, |
| 72 | + 'expectErrors' => true, |
| 73 | + ], |
| 74 | + ]; |
| 75 | + } |
| 76 | +} |
0 commit comments