From de335c08515f8412c446a0e41d9c2c960d7103bb Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Sat, 31 Dec 2016 09:54:58 +0100 Subject: [PATCH] Add deprecation reason tests (#92) --- Config/EnumTypeDefinition.php | 31 ++++++--- .../doc/definitions/system-types/enum.md | 5 ++ .../doc/definitions/system-types/object.md | 2 + Tests/Functional/TestCase.php | 11 ++- Tests/Functional/Type/DefinitionTest.php | 68 +++++++++++++++++++ .../app/config/definition/config.yml | 15 ++++ .../definition/mapping/deprecated.types.yml | 21 ++++++ 7 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 Tests/Functional/Type/DefinitionTest.php create mode 100644 Tests/Functional/app/config/definition/config.yml create mode 100644 Tests/Functional/app/config/definition/mapping/deprecated.types.yml diff --git a/Config/EnumTypeDefinition.php b/Config/EnumTypeDefinition.php index 21b8ee49b..d0f7c7182 100644 --- a/Config/EnumTypeDefinition.php +++ b/Config/EnumTypeDefinition.php @@ -25,18 +25,31 @@ public function getDefinition() ->append($this->nameSection()) ->arrayNode('values') ->useAttributeAsKey('name') + // if value not define we use name as value + ->beforeNormalization() + ->ifTrue(function ($v) { + return is_array($v); + }) + ->then(function ($v) { + foreach ($v as $name => &$options) { + // short syntax NAME: VALUE + if (!is_null($options) && !is_array($options)) { + $options = ['value' => $options]; + } + + // use name as value if no value given + if (!array_key_exists('value', $options)) { + $options['value'] = $name; + } + } + + return $v; + }) + ->end() ->prototype('array') - ->beforeNormalization() - ->ifTrue(function ($v) { - return !is_null($v) && !is_array($v); - }) - ->then(function ($v) { - return ['value' => $v]; - }) - ->end() ->isRequired() ->children() - ->scalarNode('value')->end() + ->scalarNode('value')->isRequired()->end() ->append($this->descriptionSection()) ->append($this->deprecationReasonSelection()) ->end() diff --git a/Resources/doc/definitions/system-types/enum.md b/Resources/doc/definitions/system-types/enum.md index ee54fe221..fc089a688 100644 --- a/Resources/doc/definitions/system-types/enum.md +++ b/Resources/doc/definitions/system-types/enum.md @@ -14,8 +14,13 @@ Episode: NEWHOPE: value: 4 description: "Released in 1977." + # to deprecate a value, only set the deprecation reason + #deprecationReason: "Just because" EMPIRE: value: 5 description: "Released in 1980." JEDI: 6 # using the short syntax (JEDI value equal to 6) +# in this case FORCEAWAKENS value = FORCEAWAKENS +# FORCEAWAKENS: +# description: "Released in 2015." ``` diff --git a/Resources/doc/definitions/system-types/object.md b/Resources/doc/definitions/system-types/object.md index 1d43584ed..0c87f80e4 100644 --- a/Resources/doc/definitions/system-types/object.md +++ b/Resources/doc/definitions/system-types/object.md @@ -20,6 +20,8 @@ Human: id: type: "String!" description: "The id of the character." + # to deprecate a field, only set the deprecation reason + #deprecationReason: "A terrible reason" name: type: "String" description: "The name of the character." diff --git a/Tests/Functional/TestCase.php b/Tests/Functional/TestCase.php index 57a746629..d5033c25a 100644 --- a/Tests/Functional/TestCase.php +++ b/Tests/Functional/TestCase.php @@ -78,7 +78,7 @@ protected static function createAndBootKernel(array $options = []) { static::bootKernel($options); - static::$kernel->getContainer()->get('overblog_graphql.cache_compiler')->loadClasses(); + static::getContainer()->get('overblog_graphql.cache_compiler')->loadClasses(); } protected static function executeGraphQLRequest($query, $rootValue = [], $throwException = false) @@ -86,8 +86,8 @@ protected static function executeGraphQLRequest($query, $rootValue = [], $throwE $request = new Request(); $request->query->set('query', $query); - $req = static::$kernel->getContainer()->get('overblog_graphql.request_parser')->parse($request); - $executor = static::$kernel->getContainer()->get('overblog_graphql.request_executor'); + $req = static::getContainer()->get('overblog_graphql.request_parser')->parse($request); + $executor = static::getContainer()->get('overblog_graphql.request_executor'); $executor->setThrowException($throwException); $res = $executor->execute($req, $rootValue); @@ -110,4 +110,9 @@ protected static function assertGraphQL($query, array $expectedData = null, arra static::assertEquals($expected, $result, json_encode($result)); } + + protected static function getContainer() + { + return static::$kernel->getContainer(); + } } diff --git a/Tests/Functional/Type/DefinitionTest.php b/Tests/Functional/Type/DefinitionTest.php new file mode 100644 index 000000000..6d58de02c --- /dev/null +++ b/Tests/Functional/Type/DefinitionTest.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Overblog\GraphQLBundle\Tests\Functional\Security; + +use GraphQL\Type\Definition\EnumType; +use GraphQL\Type\Definition\ObjectType; +use GraphQL\Type\Definition\Type; +use Overblog\GraphQLBundle\Tests\Functional\TestCase; + +class DefinitionTest extends TestCase +{ + protected function setUp() + { + parent::setUp(); + + static::createAndBootKernel(['test_case' => 'definition']); + } + + public function testDefinesEnumTypeWithDeprecatedValue() + { + /** @var EnumType $enumTypeWithDeprecatedValue */ + $enumTypeWithDeprecatedValue = $this->getType('EnumWithDeprecatedValue'); + $value = $enumTypeWithDeprecatedValue->getValues()[0]; + $this->assertEquals([ + 'name' => 'foo', + 'description' => null, + 'deprecationReason' => 'Just because', + 'value' => 'foo', + ], (array) $value); + $this->assertEquals(true, $this->isDeprecated($value)); + } + + public function testDefinesAnObjectTypeWithDeprecatedField() + { + /** @var ObjectType $TypeWithDeprecatedField */ + $TypeWithDeprecatedField = $this->getType('ObjectWithDeprecatedField'); + $field = $TypeWithDeprecatedField->getField('bar'); + $this->assertEquals(Type::string(), $field->getType()); + $this->assertEquals(true, $this->isDeprecated($field)); + $this->assertEquals('A terrible reason', $field->deprecationReason); + $this->assertEquals('bar', $field->name); + $this->assertEquals([], $field->args); + } + + private function isDeprecated($node) + { + // TODO(mcg-web) refactor after moving to latest lib version + if (is_callable([$node, 'isDeprecated'])) { + return $node->isDeprecated(); + } + + return (bool) $node->deprecationReason; + } + + private function getType($type) + { + return $this->getContainer()->get('overblog_graphql.type_resolver')->resolve($type); + } +} diff --git a/Tests/Functional/app/config/definition/config.yml b/Tests/Functional/app/config/definition/config.yml new file mode 100644 index 000000000..dddbf36c3 --- /dev/null +++ b/Tests/Functional/app/config/definition/config.yml @@ -0,0 +1,15 @@ +imports: + - { resource: ../config.yml } + +parameters: + overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\Definition\\__DEFINITIONS__" + +overblog_graphql: + definitions: + schema: + query: RootQuery + mappings: + types: + - + type: yml + dir: "%kernel.root_dir%/config/definition/mapping" diff --git a/Tests/Functional/app/config/definition/mapping/deprecated.types.yml b/Tests/Functional/app/config/definition/mapping/deprecated.types.yml new file mode 100644 index 000000000..b4a6577b6 --- /dev/null +++ b/Tests/Functional/app/config/definition/mapping/deprecated.types.yml @@ -0,0 +1,21 @@ +RootQuery: + type: object + config: + fields: + fake: + type: String + +EnumWithDeprecatedValue: + type: enum + config: + values: + foo: + deprecationReason: "Just because" + +ObjectWithDeprecatedField: + type: object + config: + fields: + bar: + type: String + deprecationReason: "A terrible reason"