Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Add more parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 committed Feb 24, 2018
1 parent b32d6b6 commit 52d4fc5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Language/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ function blockStringValue(string $rawString): string
}
}

while ($lineCount > 0 && isBlank($lines[0])) {
while (count($lines) > 0 && isBlank($lines[0])) {
array_shift($lines);
}

while ($lineCount > 0 && isBlank($lines[$lineCount - 1])) {
while (($lineCount = count($lines)) > 0 && isBlank($lines[$lineCount - 1])) {
array_pop($lines);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Type/Definition/Behavior/ValueTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Digia\GraphQL\Type\Definition\Behavior;

trait ValueTrait
{

/**
* @var mixed|null
*/
protected $value;

/**
* @return mixed|null
*/
public function getValue()
{
return $this->value;
}
}
2 changes: 1 addition & 1 deletion src/Type/Definition/EnumValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Digia\GraphQL\Type\Definition;

use Digia\GraphQL\Behavior\ValueTrait;
use Digia\GraphQL\ConfigObject;
use Digia\GraphQL\Language\AST\Node\EnumValueDefinitionNode;
use Digia\GraphQL\Language\AST\Node\NodeTrait;
use Digia\GraphQL\Type\Definition\Behavior\DeprecationTrait;
use Digia\GraphQL\Type\Definition\Behavior\DescriptionTrait;
use Digia\GraphQL\Type\Definition\Behavior\NameTrait;
use Digia\GraphQL\Type\Definition\Behavior\ValueTrait;

/**
* Class EnumValue
Expand Down
22 changes: 6 additions & 16 deletions src/Type/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,10 @@ protected function buildTypeMap(): void
$typeMap = [];

// First by deeply visiting all initial types.
$typeMap = array_reduce($initialTypes, function ($map, $type) {
return typeMapReducer($map, $type);
}, $typeMap);
$typeMap = array_reduce($initialTypes, 'Digia\GraphQL\Type\Schema\typeMapReducer', $typeMap);

// Then by deeply visiting all directive types.
$typeMap = array_reduce($this->directives, function ($map, $directive) {
return typeMapDirectiveReducer($map, $directive);
}, $typeMap);
$typeMap = array_reduce($this->directives, 'Digia\GraphQL\Type\Schema\typeMapDirectiveReducer', $typeMap);

// Storing the resulting map for reference by the schema.
$this->_typeMap = $typeMap;
Expand Down Expand Up @@ -330,7 +326,7 @@ protected function setAssumeValid(bool $assumeValid): Schema
*/
function typeMapReducer(array $map, ?TypeInterface $type): array
{
if (!$type) {
if (null === $type) {
return $map;
}

Expand All @@ -357,15 +353,11 @@ function typeMapReducer(array $map, ?TypeInterface $type): array
$reducedMap = $map;

if ($type instanceof UnionType) {
$reducedMap = array_reduce($type->getTypes(), function ($map, $type) {
return typeMapReducer($map, $type);
}, $reducedMap);
$reducedMap = array_reduce($type->getTypes(), 'Digia\GraphQL\Type\Schema\typeMapReducer', $reducedMap);
}

if ($type instanceof ObjectType) {
$reducedMap = array_reduce($type->getInterfaces(), function ($map, $type) {
return typeMapReducer($map, $type);
}, $reducedMap);
$reducedMap = array_reduce($type->getInterfaces(), 'Digia\GraphQL\Type\Schema\typeMapReducer', $reducedMap);
}

if ($type instanceof ObjectType || $type instanceof InterfaceType) {
Expand All @@ -375,9 +367,7 @@ function typeMapReducer(array $map, ?TypeInterface $type): array
return $argument->getType();
}, $field->getArgs());

$reducedMap = array_reduce($fieldArgTypes, function ($map, $type) {
return typeMapReducer($map, $type);
}, $reducedMap);
$reducedMap = array_reduce($fieldArgTypes, 'Digia\GraphQL\Type\Schema\typeMapReducer', $reducedMap);
}

$reducedMap = typeMapReducer($reducedMap, $field->getType());
Expand Down
14 changes: 14 additions & 0 deletions tests/Functional/Language/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Digia\GraphQL\Language\AST\Builder\SelectionSetBuilder;
use Digia\GraphQL\Language\AST\Builder\StringBuilder;
use Digia\GraphQL\Language\AST\Builder\VariableBuilder;
use Digia\GraphQL\Language\AST\Builder\VariableDefinitionBuilder;
use Digia\GraphQL\Language\AST\Node\DocumentNode;
use Digia\GraphQL\Language\AST\Node\NamedTypeNode;
use Digia\GraphQL\Language\AST\Node\NullValueNode;
Expand Down Expand Up @@ -85,6 +86,7 @@ public function setUp()
new SelectionSetBuilder(),
new StringBuilder(),
new VariableBuilder(),
new VariableDefinitionBuilder(),
];

$readers = [
Expand Down Expand Up @@ -207,6 +209,18 @@ public function testParsesMultiByteCharacters()

// TODO: Consider adding test for 'parses kitchen sink'

/**
* @throws \Digia\GraphQL\Error\GraphQLError
* @throws \Exception
*/
public function testParsesKitchenSink()
{
$kitchenSink = mb_convert_encoding(file_get_contents(__DIR__ . '/kitchen-sink.graphql'), 'UTF-8');

$this->parser->parse(new Source($kitchenSink));
$this->addToAssertionCount(1);
}

/**
* @throws \Digia\GraphQL\Error\GraphQLError
* @throws \Exception
Expand Down
59 changes: 59 additions & 0 deletions tests/Functional/Language/kitchen-sink.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) 2015-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

query queryName($foo: ComplexType, $site: Site = MOBILE) {
whoever123is: node(id: [123, 456]) {
id ,
... on User @defer {
field2 {
id ,
alias: field1(first:10, after:$foo,) @include(if: $foo) {
id,
...frag
}
}
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}

mutation likeStory {
like(story: 123) @defer {
story {
id
}
}
}

subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {
storyLikeSubscribe(input: $input) {
story {
likers {
count
}
likeSentence {
text
}
}
}
}

fragment frag on Friend {
foo(size: $size, bar: $b, obj: {key: "value", block: """
block string uses \"""

"""})
}
{
unnamed(truthy: true, falsey: false, nullish: null),
query
}

0 comments on commit 52d4fc5

Please sign in to comment.