Skip to content

Commit

Permalink
Update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Feb 23, 2024
1 parent f8743fa commit cf24372
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 56 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require-dev": {
"ergebnis/composer-normalize": "^2.11",
"jangregor/phpstan-prophecy": "^1",
"mll-lab/php-cs-fixer-config": "^4.4",
"mll-lab/php-cs-fixer-config": "^5",
"orchestra/testbench": "~3.6.0 || ~3.7.0 || ~3.8.0 || ~3.9.0 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9 || ^9.x-dev",
"phpstan/extension-installer": "^1",
"phpstan/phpstan": "^1",
Expand Down
7 changes: 3 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.result.cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<testsuites>
<testsuite name="Unit">
Expand All @@ -11,7 +10,7 @@
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
<directory>src</directory>
</include>
</source>
</phpunit>
6 changes: 2 additions & 4 deletions src/BadMultipartRequestGraphQLException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class BadMultipartRequestGraphQLException extends BadRequestHttpException
{
/**
* @param array<mixed> $headers
*/
public function __construct(string $message, \Throwable $previous = null, int $code = 0, array $headers = [])
/** @param array<mixed> $headers */
public function __construct(string $message, ?\Throwable $previous = null, int $code = 0, array $headers = [])
{
parent::__construct(
"{$message} Be sure to conform to the GraphQL multipart request specification (https://github.com/jaydenseric/graphql-multipart-request-spec).",
Expand Down
4 changes: 1 addition & 3 deletions src/BadRequestGraphQLException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class BadRequestGraphQLException extends BadRequestHttpException
{
}
class BadRequestGraphQLException extends BadRequestHttpException {}
25 changes: 11 additions & 14 deletions src/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Safe\Exceptions\JsonException;

use function Safe\json_decode;

/**
* Follows https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md.
*/
/** Follows https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md. */
class RequestParser
{
/**
* @var \GraphQL\Server\Helper
*/
/** @var Helper */
protected $helper;

public function __construct()
Expand All @@ -28,15 +25,15 @@ public function __construct()
* Converts an incoming HTTP request to one or more OperationParams.
*
* @throws \GraphQL\Server\RequestError
* @throws \Laragraph\Utils\BadRequestGraphQLException
* @throws \Laragraph\Utils\BadMultipartRequestGraphQLException
* @throws BadRequestGraphQLException
* @throws BadMultipartRequestGraphQLException
*
* @return \GraphQL\Server\OperationParams|array<int, \GraphQL\Server\OperationParams>
*/
public function parseRequest(Request $request)
{
$method = $request->getMethod();
$bodyParams = 'POST' === $method
$bodyParams = $method === 'POST'
? $this->bodyParams($request)
: [];
/** @var array<string, mixed> $queryParams Laravel type is not precise enough */
Expand All @@ -48,8 +45,8 @@ public function parseRequest(Request $request)
/**
* Extracts the body parameters from the request.
*
* @throws \Laragraph\Utils\BadMultipartRequestGraphQLException
* @throws \Laragraph\Utils\BadRequestGraphQLException
* @throws BadMultipartRequestGraphQLException
* @throws BadRequestGraphQLException
*
* @return array<mixed>
*/
Expand Down Expand Up @@ -96,14 +93,14 @@ protected function bodyParams(Request $request): array
*
* Follows https://github.com/jaydenseric/graphql-multipart-request-spec.
*
* @throws \Laragraph\Utils\BadMultipartRequestGraphQLException
* @throws BadMultipartRequestGraphQLException
*
* @return array<mixed>
*/
protected function inlineFiles(Request $request): array
{
$mapParam = $request->post('map');
if (null === $mapParam) {
if ($mapParam === null) {
throw new BadMultipartRequestGraphQLException('Missing parameter map.');
}
if (! is_string($mapParam)) {
Expand All @@ -112,7 +109,7 @@ protected function inlineFiles(Request $request): array
}

$operationsParam = $request->post('operations');
if (null === $operationsParam) {
if ($operationsParam === null) {
throw new BadMultipartRequestGraphQLException('Missing parameter operations.');
}
if (! is_string($operationsParam)) {
Expand Down
40 changes: 10 additions & 30 deletions tests/Unit/RequestParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public function testGetWithQuery(): void
self::assertSame($query, $params->query);
}

/**
* @dataProvider jsonLikeContentTypes
*/
/** @dataProvider jsonLikeContentTypes */
public function testPostWithJsonLike(string $contentType): void
{
$query = /** @lang GraphQL */ '{ foo }';
Expand All @@ -43,19 +41,15 @@ public function testPostWithJsonLike(string $contentType): void
self::assertSame($query, $params->query);
}

/**
* @return iterable<array{string}>
*/
/** @return iterable<array{string}> */
public static function jsonLikeContentTypes(): iterable
{
yield ['application/json'];
yield ['application/graphql+json'];
yield ['application/json;charset=UTF-8'];
}

/**
* @dataProvider graphQLContentTypes
*/
/** @dataProvider graphQLContentTypes */
public function testPostWithQueryApplicationGraphQL(string $contentType): void
{
$query = /** @lang GraphQL */ '{ foo }';
Expand All @@ -72,18 +66,14 @@ public function testPostWithQueryApplicationGraphQL(string $contentType): void
self::assertSame($query, $params->query);
}

/**
* @return iterable<array{string}>
*/
/** @return iterable<array{string}> */
public static function graphQLContentTypes(): iterable
{
yield ['application/graphql'];
yield ['application/graphql;charset=UTF-8'];
}

/**
* @dataProvider formContentTypes
*/
/** @dataProvider formContentTypes */
public function testPostWithRegularForm(string $contentType): void
{
$query = /** @lang GraphQL */ '{ foo }';
Expand All @@ -100,9 +90,7 @@ public function testPostWithRegularForm(string $contentType): void
self::assertSame($query, $params->query);
}

/**
* @return iterable<array{string}>
*/
/** @return iterable<array{string}> */
public static function formContentTypes(): iterable
{
yield ['application/x-www-form-urlencoded'];
Expand Down Expand Up @@ -145,9 +133,7 @@ public function testPostDefaultsToRegularForm(): void
self::assertSame($query, $params->query);
}

/**
* @dataProvider nonsensicalContentTypes
*/
/** @dataProvider nonsensicalContentTypes */
public function testNonsensicalContentTypes(string $contentType): void
{
$request = $this->makeRequest(
Expand All @@ -163,9 +149,7 @@ public function testNonsensicalContentTypes(string $contentType): void
$parser->parseRequest($request);
}

/**
* @return iterable<array{string}>
*/
/** @return iterable<array{string}> */
public static function nonsensicalContentTypes(): iterable
{
yield ['foobar'];
Expand Down Expand Up @@ -217,9 +201,7 @@ public function testNonArrayJson(): void
$parser->parseRequest($request);
}

/**
* @dataProvider multipartFormContentTypes
*/
/** @dataProvider multipartFormContentTypes */
public function testMultipartFormRequest(string $contentType): void
{
$file = UploadedFile::fake()->create('image.jpg', 500);
Expand Down Expand Up @@ -260,9 +242,7 @@ public function testMultipartFormRequest(string $contentType): void
self::assertSame($file, $variables['file']);
}

/**
* @return iterable<array{string}>
*/
/** @return iterable<array{string}> */
public static function multipartFormContentTypes(): iterable
{
yield ['multipart/form-data'];
Expand Down

0 comments on commit cf24372

Please sign in to comment.