Skip to content

Commit

Permalink
test(APQ): Simplify dynamic page cache test code (#1394)
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi authored Apr 8, 2024
1 parent 53eb21e commit e29468f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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',
Expand All @@ -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));

}

}
4 changes: 2 additions & 2 deletions tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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));
}
Expand Down
16 changes: 9 additions & 7 deletions tests/src/Traits/HttpRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +40,7 @@ trait HttpRequestTrait {
* The http response object.
*/
protected function query(
string $query,
?string $query,
?Server $server = NULL,
array $variables = [],
array $extensions = [],
Expand All @@ -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;
}
Expand Down

0 comments on commit e29468f

Please sign in to comment.