From e29468fd07162494b7838bd23780b2f532c41f77 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Mon, 8 Apr 2024 17:18:32 +0200 Subject: [PATCH] test(APQ): Simplify dynamic page cache test code (#1394) --- ...icPersistedQueriesDynamicPageCacheTest.php | 37 +++++++------------ .../AutomaticPersistedQueriesTest.php | 4 +- tests/src/Traits/HttpRequestTrait.php | 16 ++++---- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php b/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php index 16ee28767..4495487ee 100644 --- a/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php +++ b/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php @@ -8,7 +8,7 @@ use Symfony\Component\HttpFoundation\Request; /** - * Tests the automatic persisted query plugin. + * Tests the APQ plugin in combination with dynamic page cache. * * @group graphql */ @@ -83,7 +83,6 @@ public function testPageCacheWithDifferentVariables(): void { $this->server->removeAllPersistedQueryInstances(); $this->server->addPersistedQueryInstance($this->pluginApq); $this->server->save(); - $endpoint = $this->server->get('endpoint'); NodeType::create([ 'type' => 'test', @@ -108,44 +107,36 @@ public function testPageCacheWithDifferentVariables(): void { $idQuery = 'query($id: String!) { node(id: $id) { id } }'; // Post query to endpoint to register the query hashes. - $parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); - $parameters['variables'] = '{"id": "2"}'; - $content = json_encode(['query' => $titleQuery] + $parameters); - $request = Request::create($endpoint, 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], $content); - $result = $this->container->get('http_kernel')->handle($request); + $extensions['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); + $variables = ['id' => '2']; + $result = $this->query($titleQuery, $this->server, $variables, $extensions, FALSE, Request::METHOD_POST); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE)); - $parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery); - $parameters['variables'] = '{"id": "2"}'; - $content = json_encode(['query' => $idQuery] + $parameters); - $request = Request::create($endpoint, 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], $content); - $result = $this->container->get('http_kernel')->handle($request); + $extensions['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery); + $variables = ['id' => '2']; + $result = $this->query($idQuery, $this->server, $variables, $extensions, FALSE, Request::METHOD_POST); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['id' => 2]]], json_decode($result->getContent(), TRUE)); // Execute apq call. - $parameters['variables'] = '{"id": "1"}'; - $request = Request::create($endpoint, 'GET', $parameters); - $result = $this->container->get('http_kernel')->handle($request); + $variables = ['id' => '1']; + $result = $this->query(NULL, $this->server, $variables, $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['id' => 1]]], json_decode($result->getContent(), TRUE)); // Execute apq call with different variables. - $parameters['variables'] = '{"id": "2"}'; - $request = Request::create($endpoint, 'GET', $parameters); - $result = $this->container->get('http_kernel')->handle($request); + $variables = ['id' => '2']; + $result = $this->query(NULL, $this->server, $variables, $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['id' => 2]]], json_decode($result->getContent(), TRUE)); // Execute apq call with same parameters, but different query. - $parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); - $parameters['variables'] = '{"id": "2"}'; - $request = Request::create($endpoint, 'GET', $parameters); - $result = $this->container->get('http_kernel')->handle($request); + $extensions['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); + $variables = ['id' => '2']; + $result = $this->query(NULL, $this->server, $variables, $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE)); - } } diff --git a/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php b/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php index fd06fc74c..b0d158355 100644 --- a/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php +++ b/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php @@ -64,7 +64,7 @@ public function testAutomaticPersistedQueries(): void { $extensions = ['persistedQuery' => ['sha256Hash' => 'some random hash']]; // Check we get PersistedQueryNotFound. - $result = $this->query('', $this->server, [], $extensions); + $result = $this->query(NULL, $this->server, [], $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame([ @@ -97,7 +97,7 @@ public function testAutomaticPersistedQueries(): void { $this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE)); // Execute first GET request again. - $result = $this->query($query, $this->server, [], $extensions); + $result = $this->query(NULL, $this->server, [], $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE)); } diff --git a/tests/src/Traits/HttpRequestTrait.php b/tests/src/Traits/HttpRequestTrait.php index b7b4174bb..e5ac05e6a 100644 --- a/tests/src/Traits/HttpRequestTrait.php +++ b/tests/src/Traits/HttpRequestTrait.php @@ -21,8 +21,8 @@ trait HttpRequestTrait { /** * Issue a simple query over http. * - * @param string $query - * The query string. + * @param string|null $query + * The query string. Can be omitted when testing auto persisted queries. * @param \Drupal\graphql\Entity\Server|null $server * The server instance. * @param array $variables @@ -40,7 +40,7 @@ trait HttpRequestTrait { * The http response object. */ protected function query( - string $query, + ?string $query, ?Server $server = NULL, array $variables = [], array $extensions = [], @@ -51,13 +51,15 @@ protected function query( $server = $server ?: $this->server; $endpoint = $this->server->get('endpoint'); $extensions = !empty($extensions) ? ['extensions' => $extensions] : []; - // If the persisted flag is true, then instead of sending the full query to - // the server we only send the query id. - $query_key = $persisted ? 'queryId' : 'query'; $data = [ - $query_key => $query, 'variables' => $variables, ] + $extensions; + if (!empty($query)) { + // If the persisted flag is true, then instead of sending the full query + // to the server we only send the query id. + $query_key = $persisted ? 'queryId' : 'query'; + $data[$query_key] = $query; + } if ($operationName) { $data['operationName'] = $operationName; }