3
3
namespace Drupal \Tests \graphql \Traits ;
4
4
5
5
use Drupal \Core \Cache \CacheableMetadata ;
6
+ use Drupal \Core \Render \RenderContext ;
7
+ use Drupal \Core \Render \RendererInterface ;
6
8
use Drupal \graphql \GraphQL \Execution \ExecutionResult ;
7
9
use GraphQL \Server \OperationParams ;
8
10
@@ -16,6 +18,11 @@ trait QueryResultAssertionTrait {
16
18
*/
17
19
protected $ server ;
18
20
21
+ /**
22
+ * @var \Drupal\Core\Render\RendererInterface
23
+ */
24
+ protected $ renderer ;
25
+
19
26
/**
20
27
* Return the default cache max age for this test case.
21
28
*
@@ -81,16 +88,23 @@ protected function defaultMutationCacheMetaData() {
81
88
* The expected cache metadata object.
82
89
*/
83
90
protected function assertResults ($ query , array $ variables , array $ expected , CacheableMetadata $ metadata = NULL ): void {
84
- $ result = $ this ->server ->executeOperation (
85
- OperationParams::create ([
86
- 'query ' => $ query ,
87
- 'variables ' => $ variables ,
88
- ])
91
+ $ context = new RenderContext ();
92
+ $ result = $ this ->getRenderer ()->executeInRenderContext (
93
+ $ context ,
94
+ function () use ($ query , $ variables ) {
95
+ return $ this ->server ->executeOperation (
96
+ OperationParams::create ([
97
+ 'query ' => $ query ,
98
+ 'variables ' => $ variables ,
99
+ ])
100
+ );
101
+ }
89
102
);
90
103
91
104
$ this ->assertResultErrors ($ result , []);
92
105
$ this ->assertResultData ($ result , $ expected );
93
106
$ this ->assertResultMetadata ($ result , $ metadata ?: $ this ->defaultCacheMetaData ());
107
+ self ::assertTrue ($ context ->isEmpty (), "Metadata was leaked during operation execution: {$ context ->serialize ()}" );
94
108
}
95
109
96
110
/**
@@ -106,15 +120,22 @@ protected function assertResults($query, array $variables, array $expected, Cach
106
120
* The expected cache metadata object.
107
121
*/
108
122
protected function assertErrors ($ query , array $ variables , $ expected , CacheableMetadata $ metadata ): void {
109
- $ result = $ this ->server ->executeOperation (
110
- OperationParams::create ([
111
- 'query ' => $ query ,
112
- 'variables ' => $ variables ,
113
- ])
123
+ $ context = new RenderContext ();
124
+ $ result = $ this ->getRenderer ()->executeInRenderContext (
125
+ $ context ,
126
+ function () use ($ query , $ variables ) {
127
+ return $ this ->server ->executeOperation (
128
+ OperationParams::create ([
129
+ 'query ' => $ query ,
130
+ 'variables ' => $ variables ,
131
+ ])
132
+ );
133
+ }
114
134
);
115
135
116
136
$ this ->assertResultErrors ($ result , $ expected );
117
137
$ this ->assertResultMetadata ($ result , $ metadata );
138
+ self ::assertTrue ($ context ->isEmpty (), "Metadata was leaked during operation execution: {$ context ->serialize ()}" );
118
139
}
119
140
120
141
/**
@@ -210,4 +231,20 @@ private function assertResultMetadata(ExecutionResult $result, CacheableMetadata
210
231
$ this ->assertEmpty ($ unexpectedTags , 'Unexpected cache tags: ' . implode (', ' , $ unexpectedTags ));
211
232
}
212
233
234
+ /**
235
+ * Get the Drupal renderer.
236
+ *
237
+ * Uses either the renderer available in the test class or fetches the Drupal
238
+ * renderer service.
239
+ *
240
+ * @return \Drupal\Core\Render\RendererInterface
241
+ * The renderer service for the test.
242
+ */
243
+ private function getRenderer () : RendererInterface {
244
+ if (!isset ($ this ->renderer )) {
245
+ $ this ->renderer = \Drupal::service ('renderer ' );
246
+ }
247
+ return $ this ->renderer ;
248
+ }
249
+
213
250
}
0 commit comments