66
77use Overblog \GraphQLBundle \Tests \Functional \TestCase ;
88use Symfony \Bundle \FrameworkBundle \KernelBrowser ;
9+ use Symfony \Component \HttpFoundation \Response ;
910use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
1011use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
1112
@@ -71,8 +72,8 @@ public function testEndpointAction(string $uri): void
7172 $ this ->disableCatchExceptions ($ client );
7273
7374 $ client ->request ('GET ' , $ uri , ['query ' => $ this ->friendsQuery ], [], ['CONTENT_TYPE ' => 'application/graphql;charset=utf8 ' , 'HTTP_Origin ' => 'http://example.com ' ]);
74- $ result = $ client ->getResponse ()-> getContent ( );
75- $ this ->assertSame (['data ' => $ this ->expectedData ], json_decode ( $ result , true ), $ result );
75+ $ result = self :: decodeResponse ( $ client ->getResponse ());
76+ $ this ->assertSame (['data ' => $ this ->expectedData ], $ result );
7677 $ this ->assertCORSHeadersExists ($ client );
7778 }
7879
@@ -108,8 +109,9 @@ public function testEndpointWithJsonContentTypeAndGetQuery(): void
108109 $ client = static ::createClient (['test_case ' => 'connectionWithCORS ' ]);
109110 $ this ->disableCatchExceptions ($ client );
110111 $ client ->request ('GET ' , '/ ' , ['query ' => $ this ->friendsQuery ], [], ['CONTENT_TYPE ' => 'application/json ' ]);
111- $ result = $ client ->getResponse ()->getContent ();
112- $ this ->assertSame (['data ' => $ this ->expectedData ], json_decode ($ result , true ), $ result );
112+ $ result = self ::decodeResponse ($ client ->getResponse ());
113+
114+ $ this ->assertSame (['data ' => $ this ->expectedData ], $ result );
113115 }
114116
115117 public function testEndpointWithInvalidBodyQuery (): void
@@ -164,6 +166,17 @@ public function testEndpointActionWithInvalidVariables(): void
164166 $ client ->request ('GET ' , '/ ' , ['query ' => $ query , 'variables ' => '"firstFriends": 2} ' ]);
165167 }
166168
169+ public function testEndpointActionPreservesFloats (): void
170+ {
171+ $ client = static ::createClient (['test_case ' => 'preserveFloats ' ]);
172+ $ this ->disableCatchExceptions ($ client );
173+
174+ $ client ->request ('GET ' , '/ ' , ['query ' => 'query { float } ' ], [], ['CONTENT_TYPE ' => 'application/graphql;charset=utf8 ' , 'HTTP_Origin ' => 'http://example.com ' ]);
175+ $ result = self ::decodeResponse ($ client ->getResponse ());
176+
177+ $ this ->assertSame (['data ' => ['float ' => 1.0 ]], $ result );
178+ }
179+
167180 public function testMultipleEndpointActionWithUnknownSchemaName (): void
168181 {
169182 $ this ->expectException (NotFoundHttpException::class);
@@ -188,8 +201,9 @@ public function testEndpointActionWithOperationName(): void
188201 $ query = $ this ->friendsQuery ."\n" .$ this ->friendsTotalCountQuery ;
189202
190203 $ client ->request ('POST ' , '/ ' , ['query ' => $ query , 'operationName ' => 'FriendsQuery ' ], [], ['CONTENT_TYPE ' => 'application/x-www-form-urlencoded ' ]);
191- $ result = $ client ->getResponse ()->getContent ();
192- $ this ->assertSame (['data ' => $ this ->expectedData ], json_decode ($ result , true ), $ result );
204+ $ result = self ::decodeResponse ($ client ->getResponse ());
205+
206+ $ this ->assertSame (['data ' => $ this ->expectedData ], $ result );
193207 }
194208
195209 /**
@@ -213,13 +227,13 @@ public function testBatchEndpointAction(string $uri): void
213227
214228 $ content = json_encode ($ data ) ?: null ;
215229 $ client ->request ('POST ' , $ uri , [], [], ['CONTENT_TYPE ' => 'application/json ' ], $ content );
216- $ result = $ client ->getResponse ()-> getContent ( );
230+ $ result = self :: decodeResponse ( $ client ->getResponse ());
217231
218232 $ expected = [
219233 ['id ' => 'friends ' , 'payload ' => ['data ' => $ this ->expectedData ]],
220234 ['id ' => 'friendsTotalCount ' , 'payload ' => ['data ' => ['user ' => ['friends ' => ['totalCount ' => 4 ]]]]],
221235 ];
222- $ this ->assertSame ($ expected , json_decode ( $ result , true ), $ result );
236+ $ this ->assertSame ($ expected , $ result );
223237 }
224238
225239 public function graphQLBatchEndpointUriProvider (): array
@@ -301,8 +315,8 @@ public function testNoCORSHeadersIfOriginHeaderNotExists(): void
301315 $ client = static ::createClient (['test_case ' => 'connectionWithCORS ' ]);
302316 $ this ->disableCatchExceptions ($ client );
303317 $ client ->request ('GET ' , '/ ' , ['query ' => $ this ->friendsQuery ], [], ['CONTENT_TYPE ' => 'application/graphql ' ]);
304- $ result = $ client ->getResponse ()-> getContent ( );
305- $ this ->assertSame (['data ' => $ this ->expectedData ], json_decode ( $ result , true ), $ result );
318+ $ result = self :: decodeResponse ( $ client ->getResponse ());
319+ $ this ->assertSame (['data ' => $ this ->expectedData ], $ result );
306320 $ this ->assertCORSHeadersNotExists ($ client );
307321 }
308322
@@ -332,4 +346,14 @@ private function assertCORSHeadersExists($client): void
332346 $ this ->assertSame ('Content-Type, Authorization ' , $ response ->headers ->get ('Access-Control-Allow-Headers ' ));
333347 $ this ->assertSame ('3600 ' , $ response ->headers ->get ('Access-Control-Max-Age ' ));
334348 }
349+
350+ private static function decodeResponse (Response $ response ): array
351+ {
352+ $ result = $ response ->getContent ();
353+ self ::assertIsString ($ result );
354+ $ result = json_decode ($ result , true );
355+ self ::assertNotNull ($ result );
356+
357+ return $ result ;
358+ }
335359}
0 commit comments