Skip to content

Commit

Permalink
added some test, fragment type is validated before its children
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Nov 10, 2020
1 parent 6f96b74 commit 26f254d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 50 deletions.
20 changes: 0 additions & 20 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,6 @@
"minCoveredMsi": 91,
"mutators": {
"@default": true,
"TrueValue": {
"ignore": [
"Graphpinator\\Exception\\*"
]
},
"FalseValue": {
"ignore": [
"Graphpinator\\Exception\\*"
]
},
"ProtectedVisibility": {
"ignore": [
"Graphpinator\\Exception\\*"
]
},
"Throw_": {
"ignore": [
"Graphpinator\\*"
]
},
"global-ignoreSourceCodeByRegex": [
"\\\\assert\\(.*\\);"
]
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/Value/ValueError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

abstract class ValueError extends \Graphpinator\Exception\GraphpinatorBase
{
public function isOutputable() : bool
protected function isOutputable() : bool
{
return true;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Normalizer/FragmentSpread/FragmentSpread.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ final class FragmentSpread
public function __construct(
\Graphpinator\Normalizer\FieldSet $fields,
\Graphpinator\Normalizer\Directive\DirectiveSet $directives,
?\Graphpinator\Type\Contract\NamedDefinition $typeCond
?\Graphpinator\Type\Contract\TypeConditionable $typeCond
)
{
if ($typeCond instanceof \Graphpinator\Type\Contract\NamedDefinition &&
!$typeCond instanceof \Graphpinator\Type\Contract\TypeConditionable) {
throw new \Graphpinator\Exception\Normalizer\TypeConditionOutputable();
}

$this->children = $fields;
$this->directives = $directives;
$this->typeCond = $typeCond;
Expand Down
11 changes: 8 additions & 3 deletions src/Parser/FragmentSpread/InlineFragmentSpread.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ public function normalize(
\Graphpinator\Parser\Fragment\FragmentSet $fragmentDefinitions
) : \Graphpinator\Normalizer\FragmentSpread\FragmentSpread
{
$type = $this->typeCond instanceof \Graphpinator\Parser\TypeRef\NamedTypeRef
$typeCond = $this->typeCond instanceof \Graphpinator\Parser\TypeRef\NamedTypeRef
? $this->typeCond->normalize($typeContainer)
: null;

if ($typeCond instanceof \Graphpinator\Type\Contract\NamedDefinition &&
!$typeCond instanceof \Graphpinator\Type\Contract\TypeConditionable) {
throw new \Graphpinator\Exception\Normalizer\TypeConditionOutputable();
}

return new \Graphpinator\Normalizer\FragmentSpread\FragmentSpread(
$this->fields->normalize($type
$this->fields->normalize($typeCond
?? $parentType, $typeContainer, $fragmentDefinitions),
$this->directives->normalize($typeContainer),
$type,
$typeCond,
);
}
}
4 changes: 4 additions & 0 deletions src/Parser/FragmentSpread/NamedFragmentSpread.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function normalize(
$fragment = $fragmentDefinitions->offsetGet($this->name);
$typeCond = $fragment->getTypeCond()->normalize($typeContainer);

if (!$typeCond instanceof \Graphpinator\Type\Contract\TypeConditionable) {
throw new \Graphpinator\Exception\Normalizer\TypeConditionOutputable();
}

return new \Graphpinator\Normalizer\FragmentSpread\FragmentSpread(
$fragment->getFields()->normalize($typeCond, $typeContainer, $fragmentDefinitions),
$this->directives->normalize($typeContainer),
Expand Down
130 changes: 111 additions & 19 deletions tests/Spec/ErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,43 +113,78 @@ public function normalizerDataProvider() : array
{
return [
[
\Graphpinator\Json::fromObject((object) []),
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldAbc @blaDirective() { fieldXyz { name } } }',
'errors' => [
['message' => 'Invalid request - "query" key not found in request body JSON.'],
],
]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => '{ fieldAbc @blaDirective() { fieldXyz { name } } }',
]),
\Graphpinator\Json::fromObject((object) ['errors' => [['message' => 'Unknown directive "blaDirective".']]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldAbc { fieldXyz @testDirective(if: true) { name } } }',
'query' => '{ fieldAbc { ... on BlaType { fieldXyz { name } } } }',
]),
\Graphpinator\Json::fromObject((object) ['errors' => [['message' => 'Unknown type "BlaType".']]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => '{ fieldAbc { ... on String { fieldXyz { name } } } }',
]),
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Unknown argument "if" provided for "@testDirective".'],
['message' => 'Fragment type condition must be outputable composite type.'],
],
]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldUnion @invalidDirective() { ... on Abc { fieldXyz { name } } } }',
'query' => '{ fieldAbc { ... on SimpleInput { fieldXyz { name } } } }',
]),
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Fragment type condition must be outputable composite type.'],
],
]),
\Graphpinator\Json::fromObject((object) ['errors' => [['message' => 'Server responded with unknown error.']]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldThrow { fieldXyz { name } } }',
'query' => '{ fieldAbc { fieldXyz @testDirective(if: true) { name } } }',
]),
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Unknown argument "if" provided for "@testDirective".'],
],
]),
\Graphpinator\Json::fromObject((object) ['errors' => [['message' => 'Server responded with unknown error.']]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldInvalidInput { fieldName fieldNumber fieldBool notDefinedField } }',
'query' => '{ fieldInvalidInput { fieldName fieldNumber fieldBool notDefinedField } }',
]),
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Unknown field "notDefinedField" requested for type "SimpleType".'],
],
]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldFragment { ... interfaceEfgFragment } }
fragment interfaceEfgFragment on InterfaceEfg {
... on InterfaceAbc { name }
}',
]),
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Invalid fragment type condition. ("InterfaceAbc" is not instance of "InterfaceEfg").'],
],
]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName {
Expand All @@ -163,20 +198,21 @@ public function normalizerDataProvider() : array
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName {
fieldFragment {
... interfaceEfgFragment
}
}
fragment interfaceEfgFragment on InterfaceEfg {
... on InterfaceAbc { name }
}',
'query' => '{ fieldUnion @invalidDirective() { ... on Abc { fieldXyz { name } } } }',
]),
\Graphpinator\Json::fromObject((object) ['errors' => [['message' => 'Server responded with unknown error.']]]),
],
[
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Invalid fragment type condition. ("InterfaceAbc" is not instance of "InterfaceEfg").'],
],
'query' => '{ fieldThrow { fieldXyz { name } } }',
]),
\Graphpinator\Json::fromObject((object) ['errors' => [['message' => 'Server responded with unknown error.']]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => '{ fieldInvalidType { } }',
]),
\Graphpinator\Json::fromObject((object) ['errors' => [['message' => 'Server responded with unknown error.']]]),
],
];
}
Expand All @@ -195,4 +231,60 @@ public function testSimple(\Graphpinator\Json $request, \Graphpinator\Json $expe

self::assertSame($expected->toString(), $result->toString());
}

public function psrDataProvider() : array
{
$httpRequest = $this->createStub(\Psr\Http\Message\ServerRequestInterface::class);
$httpRequest->method('getMethod')->willReturn('PUT');

$httpRequest2 = $this->createStub(\Psr\Http\Message\ServerRequestInterface::class);
$httpRequest2->method('getParsedBody')->willReturn([]);
$httpRequest2->method('getHeader')->willReturn(['multipart/form-data; boundary=-------9051914041544843365972754266']);
$httpRequest2->method('getMethod')->willReturn('POST');

$httpRequest3 = $this->createStub(\Psr\Http\Message\ServerRequestInterface::class);
$httpRequest3->method('getParsedBody')->willReturn(['operations' => '{}']);
$httpRequest3->method('getHeader')->willReturn(['multipart/form-data; boundary=-------9051914041544843365972754266']);
$httpRequest3->method('getMethod')->willReturn('GET');

return [
[
$httpRequest,
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Invalid request - only GET and POST methods are supported.'],
],
]),
],
[
$httpRequest2,
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Invalid multipart request - request must be POST and contain "operations" data.'],
],
]),
],
[
$httpRequest3,
\Graphpinator\Json::fromObject((object) [
'errors' => [
['message' => 'Invalid multipart request - request must be POST and contain "operations" data.'],
],
]),
],
];
}

/**
* @dataProvider psrDataProvider
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Graphpinator\Json $expected
*/
public function testPsrRequest(\Psr\Http\Message\ServerRequestInterface $request, \Graphpinator\Json $expected) : void
{
$graphpinator = new \Graphpinator\Graphpinator(TestSchema::getSchema(), true);
$result = $graphpinator->run(new \Graphpinator\Request\PsrRequestFactory($request));

self::assertSame($expected->toString(), $result->toString());
}
}
20 changes: 19 additions & 1 deletion tests/Spec/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function testHttpGraphQlBody(\Graphpinator\Json $request, \Graphpinator\J
* @param \Graphpinator\Json $request
* @param \Graphpinator\Json $expected
*/
public function testHttpParamsBody(\Graphpinator\Json $request, \Graphpinator\Json $expected) : void
public function testHttpQueryParams(\Graphpinator\Json $request, \Graphpinator\Json $expected) : void
{
$params = (array) $request->toObject();

Expand All @@ -265,6 +265,24 @@ public function testHttpParamsBody(\Graphpinator\Json $request, \Graphpinator\Js
self::assertSame($expected->toString(), $result->toString());
}

/**
* @dataProvider simpleDataProvider
* @param \Graphpinator\Json $request
* @param \Graphpinator\Json $expected
*/
public function testHttpMultipartBody(\Graphpinator\Json $request, \Graphpinator\Json $expected) : void
{
$httpRequest = $this->createStub(\Psr\Http\Message\ServerRequestInterface::class);
$httpRequest->method('getHeader')->willReturn(['multipart/form-data; boundary=-------9051914041544843365972754266']);
$httpRequest->method('getMethod')->willReturn('POST');
$httpRequest->method('getParsedBody')->willReturn(['operations' => $request->toString()]);

$graphpinator = new \Graphpinator\Graphpinator(TestSchema::getSchema());
$result = $graphpinator->run(new \Graphpinator\Request\PsrRequestFactory($httpRequest));

self::assertSame($expected->toString(), $result->toString());
}

public function invalidDataProvider() : array
{
return [
Expand Down

0 comments on commit 26f254d

Please sign in to comment.