@@ -264,26 +264,65 @@ public function testError() : void
264
264
}
265
265
}
266
266
267
- public function testDispatchWithOptions (): void
267
+ public function testDispatchOptions (): void
268
268
{
269
269
$ this ->request ->setPathInfo ('/graphql ' );
270
270
$ this ->request ->setMethod ('OPTIONS ' );
271
271
$ response = $ this ->graphql ->dispatch ($ this ->request );
272
- self ::assertEquals (204 , $ response ->getStatusCode ());
272
+ self ::assertEquals (200 , $ response ->getStatusCode ());
273
273
self ::assertEmpty ($ response ->getContent ());
274
274
}
275
275
276
- public function testDispatchWithGetWithoutQuery (): void
276
+ public function testDispatchGetWithoutQuery (): void
277
277
{
278
278
$ this ->request ->setPathInfo ('/graphql ' );
279
279
$ this ->request ->setMethod ('GET ' );
280
280
$ response = $ this ->graphql ->dispatch ($ this ->request );
281
281
self ::assertEquals (400 , $ response ->getStatusCode ());
282
282
$ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
283
+ self ::assertArrayHasKey ('errors ' , $ output );
283
284
self ::assertNotEmpty ($ output ['errors ' ]);
285
+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
286
+ self ::assertStringStartsWith ('Syntax Error: ' , $ output ['errors ' ][0 ]['message ' ]);
284
287
}
285
288
286
- public function testDispatchWithPostAndWrongContentType (): void
289
+ public function testDispatchGetWithInvalidQuery (): void
290
+ {
291
+ $ query = <<<QUERY
292
+ {
293
+ products(filter: {sku: {eq: "simple1"}
294
+ }
295
+ QUERY ;
296
+
297
+ $ this ->request ->setPathInfo ('/graphql ' );
298
+ $ this ->request ->setMethod ('GET ' );
299
+ $ this ->request ->setQueryValue ('query ' , $ query );
300
+ $ response = $ this ->graphql ->dispatch ($ this ->request );
301
+ self ::assertEquals (400 , $ response ->getStatusCode ());
302
+ $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
303
+ self ::assertArrayHasKey ('errors ' , $ output );
304
+ self ::assertNotEmpty ($ output ['errors ' ]);
305
+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
306
+ self ::assertStringStartsWith ('Syntax Error: ' , $ output ['errors ' ][0 ]['message ' ]);
307
+ }
308
+
309
+ public function testDispatchPostWithoutQuery (): void
310
+ {
311
+ $ this ->request ->setPathInfo ('/graphql ' );
312
+ $ this ->request ->setMethod ('POST ' );
313
+ $ headers = $ this ->objectManager ->create (\Laminas \Http \Headers::class)
314
+ ->addHeaders (['Content-Type ' => 'application/json ' ]);
315
+ $ this ->request ->setHeaders ($ headers );
316
+ $ response = $ this ->graphql ->dispatch ($ this ->request );
317
+ self ::assertEquals (400 , $ response ->getStatusCode ());
318
+ $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
319
+ self ::assertArrayHasKey ('errors ' , $ output );
320
+ self ::assertNotEmpty ($ output ['errors ' ]);
321
+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
322
+ self ::assertStringStartsWith ('Syntax Error: ' , $ output ['errors ' ][0 ]['message ' ]);
323
+ }
324
+
325
+ public function testDispatchPostWithInvalidJson (): void
287
326
{
288
327
$ query = <<<QUERY
289
328
{
@@ -296,18 +335,47 @@ public function testDispatchWithPostAndWrongContentType(): void
296
335
}
297
336
}
298
337
QUERY ;
299
- $ postData = [
300
- 'query ' => $ query ,
301
- 'variables ' => null ,
302
- 'operationName ' => null
303
- ];
338
+ $ postData = ['query ' => $ query ];
339
+
340
+ $ this ->request ->setPathInfo ('/graphql ' );
341
+ $ this ->request ->setMethod ('POST ' );
342
+ $ this ->request ->setContent (http_build_query ($ postData ));
343
+ $ headers = $ this ->objectManager ->create (\Laminas \Http \Headers::class)
344
+ ->addHeaders (['Content-Type ' => 'application/json ' ]);
345
+ $ this ->request ->setHeaders ($ headers );
346
+ $ response = $ this ->graphql ->dispatch ($ this ->request );
347
+ self ::assertEquals (400 , $ response ->getStatusCode ());
348
+ $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
349
+ self ::assertArrayHasKey ('errors ' , $ output );
350
+ self ::assertNotEmpty ($ output ['errors ' ]);
351
+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
352
+ self ::assertEquals ('Unable to parse the request. ' , $ output ['errors ' ][0 ]['message ' ]);
353
+ }
354
+
355
+ public function testDispatchPostWithWrongContentType (): void
356
+ {
357
+ $ query = <<<QUERY
358
+ {
359
+ products(filter: {sku: {eq: "simple1"}}) {
360
+ items {
361
+ id
362
+ name
363
+ sku
364
+ }
365
+ }
366
+ }
367
+ QUERY ;
368
+ $ postData = ['query ' => $ query ];
304
369
305
370
$ this ->request ->setPathInfo ('/graphql ' );
306
371
$ this ->request ->setMethod ('POST ' );
307
372
$ this ->request ->setContent (json_encode ($ postData ));
308
373
$ response = $ this ->graphql ->dispatch ($ this ->request );
309
374
self ::assertEquals (400 , $ response ->getStatusCode ());
310
375
$ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
376
+ self ::assertArrayHasKey ('errors ' , $ output );
311
377
self ::assertNotEmpty ($ output ['errors ' ]);
378
+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
379
+ self ::assertEquals ('Request content type must be application/json ' , $ output ['errors ' ][0 ]['message ' ]);
312
380
}
313
381
}
0 commit comments