Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasruunu committed Mar 5, 2024
1 parent 4c42270 commit 39f4e34
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 88 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
php-version:
- '8.0'
- '8.1'
- '8.2'
symfony-version:
- '5.4.*'
- '6.0.*'
Expand Down
19 changes: 5 additions & 14 deletions src/Config/Parser/GraphQLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function call_user_func;
use function explode;
use function file_get_contents;
use function get_class;
use function in_array;
use function preg_replace;
use function sprintf;
Expand Down Expand Up @@ -62,12 +61,10 @@ public static function parse(SplFileInfo $file, ContainerBuilder $container, arr
* @var ObjectTypeDefinitionNode|InputObjectTypeDefinitionNode|EnumTypeDefinitionNode|DirectiveDefinitionNode $typeDef
*/
if (isset($typeDef->kind) && in_array($typeDef->kind, array_keys(self::DEFINITION_TYPE_MAPPING))) {
/**
* @var class-string<NodeInterface> $class
*/
/** @var class-string<NodeInterface> $class */
$class = sprintf('\\%s\\GraphQL\\ASTConverter\\%sNode', __NAMESPACE__, ucfirst(self::DEFINITION_TYPE_MAPPING[$typeDef->kind]));
$typesConfig[$typeDef->name->value] = call_user_func([$class, 'toConfig'], $typeDef);
} else if (self::isAllowedDirectiveDefinition($typeDef)) {
} elseif (self::isAllowedDirectiveDefinition($typeDef)) {

Check failure on line 67 in src/Config/Parser/GraphQLParser.php

View workflow job for this annotation

GitHub Actions / Static analysis

Parameter #1 $typeDef of static method Redeye\GraphQLBundle\Config\Parser\GraphQLParser::isAllowedDirectiveDefinition() expects GraphQL\Language\AST\EnumTypeDefinitionNode|GraphQL\Language\AST\InputObjectTypeDefinitionNode|GraphQL\Language\AST\ObjectTypeDefinitionNode, GraphQL\Language\AST\DirectiveDefinitionNode|GraphQL\Language\AST\EnumTypeDefinitionNode|GraphQL\Language\AST\InputObjectTypeDefinitionNode|GraphQL\Language\AST\ObjectTypeDefinitionNode given.
// Allow a directive named resolve
} else {
self::throwUnsupportedDefinitionNode($typeDef);
Expand All @@ -82,7 +79,7 @@ public static function parse(SplFileInfo $file, ContainerBuilder $container, arr
*/
private static function isAllowedDirectiveDefinition($typeDef): bool
{
if (!(isset($typeDef->kind) && $typeDef->kind == NodeKind::DIRECTIVE_DEFINITION && isset($typeDef->name))) {
if (!(isset($typeDef->kind) && NodeKind::DIRECTIVE_DEFINITION == $typeDef->kind && isset($typeDef->name))) {
return false;
}

Expand All @@ -101,13 +98,7 @@ private static function isAllowedDirectiveDefinition($typeDef): bool

private static function throwUnsupportedDefinitionNode(DefinitionNode $typeDef): void
{
$path = explode('\\', get_class($typeDef));
throw new InvalidArgumentException(
sprintf(
'%s definition is not supported right now, attempting to define %s.',
preg_replace('@DefinitionNode$@', '', array_pop($path)),
$typeDef->name
)
);
$path = explode('\\', $typeDef::class);
throw new InvalidArgumentException(sprintf('%s definition is not supported right now.', preg_replace('@DefinitionNode$@', '', array_pop($path))));
}
}
92 changes: 79 additions & 13 deletions tests/DependencyInjection/Compiler/ConfigParserPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Redeye\GraphQLBundle\Tests\DependencyInjection\Compiler;

use GraphQL\Error\UserError;
use PHPUnit\Framework\TestCase;
use Redeye\GraphQLBundle\Config\Processor\InheritanceProcessor;
use Redeye\GraphQLBundle\DependencyInjection\Compiler\ConfigParserPass;
use Redeye\GraphQLBundle\DependencyInjection\RedeyeGraphQLExtension;
Expand All @@ -16,7 +17,6 @@
use Redeye\GraphQLBundle\Tests\DependencyInjection\Builder\PagerArgs;
use Redeye\GraphQLBundle\Tests\DependencyInjection\Builder\RawIdField;
use Redeye\GraphQLBundle\Tests\DependencyInjection\Builder\TimestampFields;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -46,7 +46,7 @@ public function tearDown(): void
public function testBrokenYmlOnPrepend(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#The file "(.*)'.preg_quote(DIRECTORY_SEPARATOR).'broken.types.yml" does not contain valid YAML\.#');
$this->expectExceptionMessageMatches('#The file "(.*)' . preg_quote(DIRECTORY_SEPARATOR) . 'broken.types.yml" does not contain valid YAML\.#');
$this->processCompilerPass($this->getMappingConfig('yaml'));
}

Expand All @@ -66,7 +66,7 @@ public function testPreparseOnPrepend(): void
public function testInternalConfigKeysShouldNotBeUsed(string $internalConfigKey): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Don\'t use internal config keys _object_config, _enum_config, _interface_config, _union_config, _input_object_config, _custom_scalar_config, replace it by "config" instead.');
$this->expectExceptionMessage('Don\'t use internal config keys _object_config, _entity_object_config, _entity_ref_object_config, _enum_config, _interface_config, _union_config, _input_object_config, _custom_scalar_config, replace it by "config" instead.');
$configs = [
['bar' => [$internalConfigKey => []]],
];
Expand Down Expand Up @@ -244,21 +244,29 @@ public function testCustomBuilders(): void
'description' => 'The creation date of the object',
'type' => 'Int!',
'resolve' => '@=value.createdAt',
'shareable' => false,
'external' => false,
],
'updatedAt' => [
'description' => 'The update date of the object',
'type' => 'Int!',
'resolve' => '@=value.updatedAt',
'shareable' => false,
'external' => false,
],
'rawIDWithDescriptionOverride' => [
'description' => 'rawIDWithDescriptionOverride description',
'type' => 'Int!',
'resolve' => '@=value.id',
'shareable' => false,
'external' => false,
],
'rawID' => [
'description' => 'The raw ID of an object',
'type' => 'Int!',
'resolve' => '@=value.id',
'shareable' => false,
'external' => false,
],
'rawIDs' => [
'type' => '[RawID!]!',
Expand All @@ -272,6 +280,8 @@ public function testCustomBuilders(): void
'defaultValue' => 0,
],
],
'shareable' => false,
'external' => false,
],
'categories' => [
'type' => '[String!]!',
Expand All @@ -285,6 +295,8 @@ public function testCustomBuilders(): void
'defaultValue' => 0,
],
],
'shareable' => false,
'external' => false,
],
'categories2' => [
'type' => '[String!]!',
Expand All @@ -298,10 +310,14 @@ public function testCustomBuilders(): void
'defaultValue' => 0,
],
],
'shareable' => false,
'external' => false,
],
],
'name' => 'foo',
'builders' => [],
'shareable' => false,
'external' => false,
'interfaces' => [],
],
],
Expand All @@ -312,11 +328,21 @@ public function testCustomBuilders(): void
'decorator' => false,
'config' => [
'fields' => [
'foo' => ['type' => 'FooBox!'],
'bar' => ['type' => 'BarBox!'],
'foo' => [
'type' => 'FooBox!',
'shareable' => false,
'external' => false,
],
'bar' => [
'type' => 'BarBox!',
'shareable' => false,
'external' => false,
],
],
'name' => 'Boxes',
'builders' => [],
'shareable' => false,
'external' => false,
'interfaces' => [],
],
],
Expand All @@ -333,10 +359,14 @@ public function testCustomBuilders(): void
'args' => [
'input' => ['type' => 'FooInput!'],
],
'shareable' => false,
'external' => false,
],
],
'name' => 'Mutation',
'builders' => [],
'shareable' => false,
'external' => false,
'interfaces' => [],
],
],
Expand All @@ -347,11 +377,21 @@ public function testCustomBuilders(): void
'decorator' => false,
'config' => [
'fields' => [
'isEmpty' => ['type' => 'Boolean!'],
'item' => ['type' => 'Foo'],
'isEmpty' => [
'type' => 'Boolean!',
'shareable' => false,
'external' => false,
],
'item' => [
'type' => 'Foo',
'shareable' => false,
'external' => false,
],
],
'name' => 'FooBox',
'builders' => [],
'shareable' => false,
'external' => false,
'interfaces' => [],
],
],
Expand All @@ -362,11 +402,21 @@ public function testCustomBuilders(): void
'decorator' => false,
'config' => [
'fields' => [
'isEmpty' => ['type' => 'Boolean!'],
'item' => ['type' => 'Bar'],
'isEmpty' => [
'type' => 'Boolean!',
'shareable' => false,
'external' => false,
],
'item' => [
'type' => 'Bar',
'shareable' => false,
'external' => false,
],
],
'name' => 'BarBox',
'builders' => [],
'shareable' => false,
'external' => false,
'interfaces' => [],
],
],
Expand Down Expand Up @@ -400,10 +450,16 @@ public function testCustomBuilders(): void
'decorator' => false,
'config' => [
'fields' => [
'fooString' => ['type' => 'String!'],
'fooString' => [
'type' => 'String!',
'shareable' => false,
'external' => false,
],
],
'name' => 'FooSuccessPayload',
'builders' => [],
'shareable' => false,
'external' => false,
'interfaces' => [],
],
],
Expand All @@ -414,11 +470,21 @@ public function testCustomBuilders(): void
'decorator' => false,
'config' => [
'fields' => [
'_error' => ['type' => 'String'],
'bar' => ['type' => 'String'],
'_error' => [
'type' => 'String',
'shareable' => false,
'external' => false,
],
'bar' => [
'type' => 'String',
'shareable' => false,
'external' => false,
],
],
'name' => 'FooFailurePayload',
'builders' => [],
'shareable' => false,
'external' => false,
'interfaces' => [],
],
],
Expand Down Expand Up @@ -446,7 +512,7 @@ private function getMappingConfig(string $type): array
'types' => [
[
'types' => [$type],
'dir' => __DIR__.'/../mapping/'.$type,
'dir' => __DIR__ . '/../mapping/' . $type,
],
],
],
Expand Down
15 changes: 6 additions & 9 deletions tests/Functional/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use Symfony\Component\Console\Tester\CommandTester;
use function file_get_contents;
use function sprintf;
use function str_replace;
use function trim;
use const PHP_EOL;

class DebugCommandTest extends TestCase
{
Expand All @@ -30,12 +27,12 @@ public function setUp(): void
foreach (DebugCommand::getCategories() as $category) {
$content = (string) file_get_contents(
sprintf(
__DIR__.'/fixtures/debug/debug-%s.txt',
__DIR__ . '/fixtures/debug/debug-%s.txt',
$category
)
);

$this->logs[$category] = str_replace("\n", PHP_EOL, trim($content));
$this->logs[$category] = $content;
}
}

Expand All @@ -51,12 +48,12 @@ public function testProcess(array $categories): void
$this->commandTester->execute(['--category' => $categories]);
$this->assertSame(0, $this->commandTester->getStatusCode());

$expected = PHP_EOL;
foreach ($categories as $category) {
$expected .= $this->logs[$category].' '.PHP_EOL.PHP_EOL."\n\n";
$this->assertStringContainsString(
str_replace('\n', '', $this->logs[$category]),
$this->commandTester->getDisplay(),
);
}

$this->assertStringContainsString($expected, $this->commandTester->getDisplay());
}

public function testInvalidFormat(): void
Expand Down
22 changes: 9 additions & 13 deletions tests/Functional/Command/fixtures/debug/debug-mutation.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@

GraphQL Mutation Services
=========================

----------------------------------------------------------------- -----------------------------------
solution id aliases
----------------------------------------------------------------- -----------------------------------
redeye_graphql.test.simple_mutation_with_thunk_fields::mutate simple_mutation_with_thunk_fields
redeye_graphql.test.simple_promise_mutation::mutate simple_promise_mutation
----------------------------------------------------------------- -----------------------------------



GraphQL Mutation Services\n
=========================\n
\n
--------------------------------------------------------------- ----------------------------------- \n
solution id aliases \n
--------------------------------------------------------------- ----------------------------------- \n
redeye_graphql.test.simple_mutation_with_thunk_fields::mutate simple_mutation_with_thunk_fields \n
redeye_graphql.test.simple_promise_mutation::mutate simple_promise_mutation \n
--------------------------------------------------------------- ----------------------------------- \n
26 changes: 11 additions & 15 deletions tests/Functional/Command/fixtures/debug/debug-query.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@

GraphQL Query Services
======================

--------------------------------------------------------------------------- --------------------------------
solution id aliases
--------------------------------------------------------------------------- --------------------------------
Redeye\GraphQLBundle\GraphQL\Relay\Mutation\MutationFieldQuery relay_mutation_field
Redeye\GraphQLBundle\GraphQL\Relay\Node\GlobalIdFieldQuery relay_globalid_field
Redeye\GraphQLBundle\GraphQL\Relay\Node\NodeFieldQuery relay_node_field
Redeye\GraphQLBundle\GraphQL\Relay\Node\PluralIdentifyingRootFieldQuery relay_plural_identifying_field
--------------------------------------------------------------------------- --------------------------------



GraphQL Query Services\n
======================\n
\n
------------------------------------------------------------------------- -------------------------------- \n
solution id aliases \n
------------------------------------------------------------------------- -------------------------------- \n
Redeye\GraphQLBundle\GraphQL\Relay\Mutation\MutationFieldQuery relay_mutation_field \n
Redeye\GraphQLBundle\GraphQL\Relay\Node\GlobalIdFieldQuery relay_globalid_field \n
Redeye\GraphQLBundle\GraphQL\Relay\Node\NodeFieldQuery relay_node_field \n
Redeye\GraphQLBundle\GraphQL\Relay\Node\PluralIdentifyingRootFieldQuery relay_plural_identifying_field \n
------------------------------------------------------------------------- -------------------------------- \n
Loading

0 comments on commit 39f4e34

Please sign in to comment.