Skip to content

Commit e29468f

Browse files
authored
test(APQ): Simplify dynamic page cache test code (#1394)
1 parent 53eb21e commit e29468f

File tree

3 files changed

+25
-32
lines changed

3 files changed

+25
-32
lines changed

tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Symfony\Component\HttpFoundation\Request;
99

1010
/**
11-
* Tests the automatic persisted query plugin.
11+
* Tests the APQ plugin in combination with dynamic page cache.
1212
*
1313
* @group graphql
1414
*/
@@ -83,7 +83,6 @@ public function testPageCacheWithDifferentVariables(): void {
8383
$this->server->removeAllPersistedQueryInstances();
8484
$this->server->addPersistedQueryInstance($this->pluginApq);
8585
$this->server->save();
86-
$endpoint = $this->server->get('endpoint');
8786

8887
NodeType::create([
8988
'type' => 'test',
@@ -108,44 +107,36 @@ public function testPageCacheWithDifferentVariables(): void {
108107
$idQuery = 'query($id: String!) { node(id: $id) { id } }';
109108

110109
// Post query to endpoint to register the query hashes.
111-
$parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery);
112-
$parameters['variables'] = '{"id": "2"}';
113-
$content = json_encode(['query' => $titleQuery] + $parameters);
114-
$request = Request::create($endpoint, 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], $content);
115-
$result = $this->container->get('http_kernel')->handle($request);
110+
$extensions['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery);
111+
$variables = ['id' => '2'];
112+
$result = $this->query($titleQuery, $this->server, $variables, $extensions, FALSE, Request::METHOD_POST);
116113
$this->assertSame(200, $result->getStatusCode());
117114
$this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE));
118115

119-
$parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery);
120-
$parameters['variables'] = '{"id": "2"}';
121-
$content = json_encode(['query' => $idQuery] + $parameters);
122-
$request = Request::create($endpoint, 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], $content);
123-
$result = $this->container->get('http_kernel')->handle($request);
116+
$extensions['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery);
117+
$variables = ['id' => '2'];
118+
$result = $this->query($idQuery, $this->server, $variables, $extensions, FALSE, Request::METHOD_POST);
124119
$this->assertSame(200, $result->getStatusCode());
125120
$this->assertSame(['data' => ['node' => ['id' => 2]]], json_decode($result->getContent(), TRUE));
126121

127122
// Execute apq call.
128-
$parameters['variables'] = '{"id": "1"}';
129-
$request = Request::create($endpoint, 'GET', $parameters);
130-
$result = $this->container->get('http_kernel')->handle($request);
123+
$variables = ['id' => '1'];
124+
$result = $this->query(NULL, $this->server, $variables, $extensions);
131125
$this->assertSame(200, $result->getStatusCode());
132126
$this->assertSame(['data' => ['node' => ['id' => 1]]], json_decode($result->getContent(), TRUE));
133127

134128
// Execute apq call with different variables.
135-
$parameters['variables'] = '{"id": "2"}';
136-
$request = Request::create($endpoint, 'GET', $parameters);
137-
$result = $this->container->get('http_kernel')->handle($request);
129+
$variables = ['id' => '2'];
130+
$result = $this->query(NULL, $this->server, $variables, $extensions);
138131
$this->assertSame(200, $result->getStatusCode());
139132
$this->assertSame(['data' => ['node' => ['id' => 2]]], json_decode($result->getContent(), TRUE));
140133

141134
// Execute apq call with same parameters, but different query.
142-
$parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery);
143-
$parameters['variables'] = '{"id": "2"}';
144-
$request = Request::create($endpoint, 'GET', $parameters);
145-
$result = $this->container->get('http_kernel')->handle($request);
135+
$extensions['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery);
136+
$variables = ['id' => '2'];
137+
$result = $this->query(NULL, $this->server, $variables, $extensions);
146138
$this->assertSame(200, $result->getStatusCode());
147139
$this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE));
148-
149140
}
150141

151142
}

tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testAutomaticPersistedQueries(): void {
6464
$extensions = ['persistedQuery' => ['sha256Hash' => 'some random hash']];
6565

6666
// Check we get PersistedQueryNotFound.
67-
$result = $this->query('', $this->server, [], $extensions);
67+
$result = $this->query(NULL, $this->server, [], $extensions);
6868

6969
$this->assertSame(200, $result->getStatusCode());
7070
$this->assertSame([
@@ -97,7 +97,7 @@ public function testAutomaticPersistedQueries(): void {
9797
$this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE));
9898

9999
// Execute first GET request again.
100-
$result = $this->query($query, $this->server, [], $extensions);
100+
$result = $this->query(NULL, $this->server, [], $extensions);
101101
$this->assertSame(200, $result->getStatusCode());
102102
$this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE));
103103
}

tests/src/Traits/HttpRequestTrait.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ trait HttpRequestTrait {
2121
/**
2222
* Issue a simple query over http.
2323
*
24-
* @param string $query
25-
* The query string.
24+
* @param string|null $query
25+
* The query string. Can be omitted when testing auto persisted queries.
2626
* @param \Drupal\graphql\Entity\Server|null $server
2727
* The server instance.
2828
* @param array $variables
@@ -40,7 +40,7 @@ trait HttpRequestTrait {
4040
* The http response object.
4141
*/
4242
protected function query(
43-
string $query,
43+
?string $query,
4444
?Server $server = NULL,
4545
array $variables = [],
4646
array $extensions = [],
@@ -51,13 +51,15 @@ protected function query(
5151
$server = $server ?: $this->server;
5252
$endpoint = $this->server->get('endpoint');
5353
$extensions = !empty($extensions) ? ['extensions' => $extensions] : [];
54-
// If the persisted flag is true, then instead of sending the full query to
55-
// the server we only send the query id.
56-
$query_key = $persisted ? 'queryId' : 'query';
5754
$data = [
58-
$query_key => $query,
5955
'variables' => $variables,
6056
] + $extensions;
57+
if (!empty($query)) {
58+
// If the persisted flag is true, then instead of sending the full query
59+
// to the server we only send the query id.
60+
$query_key = $persisted ? 'queryId' : 'query';
61+
$data[$query_key] = $query;
62+
}
6163
if ($operationName) {
6264
$data['operationName'] = $operationName;
6365
}

0 commit comments

Comments
 (0)