Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -430,16 +430,6 @@ parameters:
count: 1
path: tests/Functional/Command/ValidateCommandTest.php

-
message: "#^Parameter \\#1 \\$json of function json_decode expects string, string\\|false given\\.$#"
count: 5
path: tests/Functional/Controller/GraphControllerTest.php

-
message: "#^Parameter \\#3 \\$message of method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\) expects string, string\\|false given\\.$#"
count: 5
path: tests/Functional/Controller/GraphControllerTest.php

-
message: "#^Method Overblog\\\\GraphQLBundle\\\\Tests\\\\Functional\\\\BaseTestCase\\:\\:createKernel\\(\\) should return Symfony\\\\Component\\\\HttpKernel\\\\KernelInterface but returns object\\.$#"
count: 1
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ private function createResponse(Request $request, ?string $schemaName, bool $bat
return new JsonResponse('', 405);
}
$payload = $this->processQuery($request, $schemaName, $batched);
$response = new JsonResponse($payload, 200);
$response = new JsonResponse('', 200);
$response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_PRESERVE_ZERO_FRACTION);
$response->setData($payload);
}
$this->addCORSHeadersIfNeeded($response, $request);

Expand Down
15 changes: 15 additions & 0 deletions tests/Functional/App/config/preserveFloats/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
imports:
- { resource: ../config.yml }
- { resource: ../exception/services.yml }

overblog_graphql:
definitions:
class_namespace: "Overblog\\GraphQLBundle\\PreserveFloats\\__DEFINITIONS__"
schema:
query: RootQuery
mutation: RootQuery
mappings:
types:
-
type: yaml
dir: "%kernel.project_dir%/config/preserveFloats/mapping"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RootQuery:
type: object
config:
fields:
float:
type: Float
resolve: '1.0'
44 changes: 34 additions & 10 deletions tests/Functional/Controller/GraphControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Overblog\GraphQLBundle\Tests\Functional\TestCase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand Down Expand Up @@ -71,8 +72,8 @@ public function testEndpointAction(string $uri): void
$this->disableCatchExceptions($client);

$client->request('GET', $uri, ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql;charset=utf8', 'HTTP_Origin' => 'http://example.com']);
$result = $client->getResponse()->getContent();
$this->assertSame(['data' => $this->expectedData], json_decode($result, true), $result);
$result = self::decodeResponse($client->getResponse());
$this->assertSame(['data' => $this->expectedData], $result);
$this->assertCORSHeadersExists($client);
}

Expand Down Expand Up @@ -108,8 +109,9 @@ public function testEndpointWithJsonContentTypeAndGetQuery(): void
$client = static::createClient(['test_case' => 'connectionWithCORS']);
$this->disableCatchExceptions($client);
$client->request('GET', '/', ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/json']);
$result = $client->getResponse()->getContent();
$this->assertSame(['data' => $this->expectedData], json_decode($result, true), $result);
$result = self::decodeResponse($client->getResponse());

$this->assertSame(['data' => $this->expectedData], $result);
}

public function testEndpointWithInvalidBodyQuery(): void
Expand Down Expand Up @@ -164,6 +166,17 @@ public function testEndpointActionWithInvalidVariables(): void
$client->request('GET', '/', ['query' => $query, 'variables' => '"firstFriends": 2}']);
}

public function testEndpointActionPreservesFloats(): void
{
$client = static::createClient(['test_case' => 'preserveFloats']);
$this->disableCatchExceptions($client);

$client->request('GET', '/', ['query' => 'query { float }'], [], ['CONTENT_TYPE' => 'application/graphql;charset=utf8', 'HTTP_Origin' => 'http://example.com']);
$result = self::decodeResponse($client->getResponse());

$this->assertSame(['data' => ['float' => 1.0]], $result);
}

public function testMultipleEndpointActionWithUnknownSchemaName(): void
{
$this->expectException(NotFoundHttpException::class);
Expand All @@ -188,8 +201,9 @@ public function testEndpointActionWithOperationName(): void
$query = $this->friendsQuery."\n".$this->friendsTotalCountQuery;

$client->request('POST', '/', ['query' => $query, 'operationName' => 'FriendsQuery'], [], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
$result = $client->getResponse()->getContent();
$this->assertSame(['data' => $this->expectedData], json_decode($result, true), $result);
$result = self::decodeResponse($client->getResponse());

$this->assertSame(['data' => $this->expectedData], $result);
}

/**
Expand All @@ -213,13 +227,13 @@ public function testBatchEndpointAction(string $uri): void

$content = json_encode($data) ?: null;
$client->request('POST', $uri, [], [], ['CONTENT_TYPE' => 'application/json'], $content);
$result = $client->getResponse()->getContent();
$result = self::decodeResponse($client->getResponse());

$expected = [
['id' => 'friends', 'payload' => ['data' => $this->expectedData]],
['id' => 'friendsTotalCount', 'payload' => ['data' => ['user' => ['friends' => ['totalCount' => 4]]]]],
];
$this->assertSame($expected, json_decode($result, true), $result);
$this->assertSame($expected, $result);
}

public function graphQLBatchEndpointUriProvider(): array
Expand Down Expand Up @@ -301,8 +315,8 @@ public function testNoCORSHeadersIfOriginHeaderNotExists(): void
$client = static::createClient(['test_case' => 'connectionWithCORS']);
$this->disableCatchExceptions($client);
$client->request('GET', '/', ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql']);
$result = $client->getResponse()->getContent();
$this->assertSame(['data' => $this->expectedData], json_decode($result, true), $result);
$result = self::decodeResponse($client->getResponse());
$this->assertSame(['data' => $this->expectedData], $result);
$this->assertCORSHeadersNotExists($client);
}

Expand Down Expand Up @@ -332,4 +346,14 @@ private function assertCORSHeadersExists($client): void
$this->assertSame('Content-Type, Authorization', $response->headers->get('Access-Control-Allow-Headers'));
$this->assertSame('3600', $response->headers->get('Access-Control-Max-Age'));
}

private static function decodeResponse(Response $response): array
{
$result = $response->getContent();
self::assertIsString($result);
$result = json_decode($result, true);
self::assertNotNull($result);

return $result;
}
}