From 559231e76a5d23ebb8a183120d45cdb258b89bf9 Mon Sep 17 00:00:00 2001 From: Chelaru Flaviu Date: Tue, 30 Jan 2024 19:55:40 +0200 Subject: [PATCH 1/2] fix issue with parsing invalid files - #1152 --- .../Parser/MetadataParser/MetadataParser.php | 2 ++ tests/Config/Parser/MetadataParserTest.php | 26 +++++++++++++++++++ .../annotations/Invalid/EmptyPhpFile.php | 2 ++ .../annotations/Invalid/HasNoClass.php | 3 +++ .../fixtures/annotations/Invalid/NotAPhpFile | 0 5 files changed, 33 insertions(+) create mode 100644 tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php create mode 100644 tests/Config/Parser/fixtures/annotations/Invalid/HasNoClass.php create mode 100644 tests/Config/Parser/fixtures/annotations/Invalid/NotAPhpFile diff --git a/src/Config/Parser/MetadataParser/MetadataParser.php b/src/Config/Parser/MetadataParser/MetadataParser.php index 473923435..a1e61b31c 100644 --- a/src/Config/Parser/MetadataParser/MetadataParser.php +++ b/src/Config/Parser/MetadataParser/MetadataParser.php @@ -134,6 +134,8 @@ private static function processFile(SplFileInfo $file, ContainerBuilder $contain } return $preProcess ? self::$map->toArray() : $gqlTypes; + } catch (\ReflectionException $e) { + return $gqlTypes; } catch (\InvalidArgumentException $e) { throw new InvalidArgumentException(sprintf('Failed to parse GraphQL metadata from file "%s".', $file), $e->getCode(), $e); } diff --git a/tests/Config/Parser/MetadataParserTest.php b/tests/Config/Parser/MetadataParserTest.php index 9603cd7e8..608dc3f24 100644 --- a/tests/Config/Parser/MetadataParserTest.php +++ b/tests/Config/Parser/MetadataParserTest.php @@ -615,4 +615,30 @@ public function testInvalidProviderMutationOnQuery(): void $this->assertMatchesRegularExpression('/try to add a mutation on type "RootQuery2"/', $e->getPrevious()->getMessage()); } } + + public function testInvalidPhpFiles(): void + { + $files = [ + __DIR__ . '/fixtures/annotations/Invalid/HasNoClass.php', + __DIR__ . '/fixtures/annotations/Invalid/EmptyPhpFile.php', + __DIR__ . '/fixtures/annotations/Invalid/NotAPhpFile', + __DIR__ . '/fixtures/annotations/Type/RootQuery.php', + ]; + $this->parser('reset', $this->parserConfig); + + foreach($files as $file) { + $this->parser('preParse', new SplFileInfo($file), $this->containerBuilder, $this->parserConfig); + } + + $config = []; + foreach($files as $file) { + $config += self::cleanConfig($this->parser('parse', new SplFileInfo($file), $this->containerBuilder, $this->parserConfig)); + } + + $this->assertSame([ + 'RootQuery' => [ + 'type' => 'object' + ] + ], $config); + } } diff --git a/tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php b/tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php new file mode 100644 index 000000000..a4abe2daf --- /dev/null +++ b/tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php @@ -0,0 +1,2 @@ + Date: Tue, 30 Jan 2024 21:13:24 +0200 Subject: [PATCH 2/2] executed php-cs-fixer --- .../Parser/MetadataParser/MetadataParser.php | 2 +- tests/Config/Parser/MetadataParserTest.php | 18 +++++++++--------- .../annotations/Invalid/EmptyPhpFile.php | 1 + .../annotations/Invalid/HasNoClass.php | 4 +++- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Config/Parser/MetadataParser/MetadataParser.php b/src/Config/Parser/MetadataParser/MetadataParser.php index a1e61b31c..6e2878ece 100644 --- a/src/Config/Parser/MetadataParser/MetadataParser.php +++ b/src/Config/Parser/MetadataParser/MetadataParser.php @@ -134,7 +134,7 @@ private static function processFile(SplFileInfo $file, ContainerBuilder $contain } return $preProcess ? self::$map->toArray() : $gqlTypes; - } catch (\ReflectionException $e) { + } catch (ReflectionException $e) { return $gqlTypes; } catch (\InvalidArgumentException $e) { throw new InvalidArgumentException(sprintf('Failed to parse GraphQL metadata from file "%s".', $file), $e->getCode(), $e); diff --git a/tests/Config/Parser/MetadataParserTest.php b/tests/Config/Parser/MetadataParserTest.php index 608dc3f24..a68202f21 100644 --- a/tests/Config/Parser/MetadataParserTest.php +++ b/tests/Config/Parser/MetadataParserTest.php @@ -619,26 +619,26 @@ public function testInvalidProviderMutationOnQuery(): void public function testInvalidPhpFiles(): void { $files = [ - __DIR__ . '/fixtures/annotations/Invalid/HasNoClass.php', - __DIR__ . '/fixtures/annotations/Invalid/EmptyPhpFile.php', - __DIR__ . '/fixtures/annotations/Invalid/NotAPhpFile', - __DIR__ . '/fixtures/annotations/Type/RootQuery.php', + __DIR__.'/fixtures/annotations/Invalid/HasNoClass.php', + __DIR__.'/fixtures/annotations/Invalid/EmptyPhpFile.php', + __DIR__.'/fixtures/annotations/Invalid/NotAPhpFile', + __DIR__.'/fixtures/annotations/Type/RootQuery.php', ]; $this->parser('reset', $this->parserConfig); - foreach($files as $file) { + foreach ($files as $file) { $this->parser('preParse', new SplFileInfo($file), $this->containerBuilder, $this->parserConfig); } $config = []; - foreach($files as $file) { - $config += self::cleanConfig($this->parser('parse', new SplFileInfo($file), $this->containerBuilder, $this->parserConfig)); + foreach ($files as $file) { + $config += self::cleanConfig($this->parser('parse', new SplFileInfo($file), $this->containerBuilder, $this->parserConfig)); } $this->assertSame([ 'RootQuery' => [ - 'type' => 'object' - ] + 'type' => 'object', + ], ], $config); } } diff --git a/tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php b/tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php index a4abe2daf..174d7fd70 100644 --- a/tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php +++ b/tests/Config/Parser/fixtures/annotations/Invalid/EmptyPhpFile.php @@ -1,2 +1,3 @@