diff --git a/Config/Parser/YamlParser.php b/Config/Parser/YamlParser.php index 7b4f73dcd..3b43b8277 100644 --- a/Config/Parser/YamlParser.php +++ b/Config/Parser/YamlParser.php @@ -7,6 +7,7 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Parser; +use Symfony\Component\Yaml\Yaml; class YamlParser implements ParserInterface { @@ -24,7 +25,7 @@ public static function parse(\SplFileInfo $file, ContainerBuilder $container) $container->addResource(new FileResource($file->getRealPath())); try { - $typesConfig = self::$yamlParser->parse(file_get_contents($file->getPathname())); + $typesConfig = self::$yamlParser->parse(file_get_contents($file->getPathname()), Yaml::PARSE_CONSTANT); } catch (ParseException $e) { throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); } diff --git a/Resources/doc/definitions/type-system/enum.md b/Resources/doc/definitions/type-system/enum.md index 66d1d3172..5cfe26ab3 100644 --- a/Resources/doc/definitions/type-system/enum.md +++ b/Resources/doc/definitions/type-system/enum.md @@ -12,7 +12,8 @@ Episode: description: "One of the films in the Star Wars Trilogy" values: NEWHOPE: - value: 4 + # We can use a PHP constant + value: !php/const App\StarWars\Movies::MOVIE_NEWHOPE description: "Released in 1977." # to deprecate a value, only set the deprecation reason #deprecationReason: "Just because" diff --git a/Tests/Config/Parser/Constant.php b/Tests/Config/Parser/Constant.php new file mode 100644 index 000000000..d38f51962 --- /dev/null +++ b/Tests/Config/Parser/Constant.php @@ -0,0 +1,8 @@ +containerBuilder = $this->getMockBuilder(ContainerBuilder::class)->setMethods(['addResource'])->getMock(); + } + + public function testParseEmptyFile() + { + $fileName = __DIR__.'/fixtures/yml/empty.yml'; + + $this->assertContainerAddFileToResources($fileName); + + $config = YamlParser::parse(new \SplFileInfo($fileName), $this->containerBuilder); + $this->assertEquals([], $config); + } + + public function testParseInvalidFile() + { + $fileName = __DIR__.'/fixtures/yml/invalid.yml'; + file_put_contents($fileName, iconv('UTF-8', 'ISO-8859-1', "not_utf-8: 'äöüß'")); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage(sprintf('The file "%s" does not contain valid YAML.', $fileName)); + YamlParser::parse(new \SplFileInfo($fileName), $this->containerBuilder); + } + + public function testParseConstant() + { + $expected = ['values' => ['constant' => Constant::CONSTANT]]; + $actual = YamlParser::parse(new \SplFileInfo(__DIR__.'/fixtures/yml/constant.yml'), $this->containerBuilder); + $this->assertEquals($expected, $actual); + } + + private function assertContainerAddFileToResources($fileName) + { + $this->containerBuilder->expects($this->once()) + ->method('addResource') + ->with($fileName); + } +} diff --git a/Tests/Config/Parser/fixtures/yml/constant.yml b/Tests/Config/Parser/fixtures/yml/constant.yml new file mode 100644 index 000000000..4971b7e2d --- /dev/null +++ b/Tests/Config/Parser/fixtures/yml/constant.yml @@ -0,0 +1,2 @@ +values: + constant: !php/const Overblog\GraphQLBundle\Tests\Config\Parser\Constant::CONSTANT \ No newline at end of file diff --git a/Tests/Config/Parser/fixtures/yml/empty.yml b/Tests/Config/Parser/fixtures/yml/empty.yml new file mode 100644 index 000000000..e69de29bb diff --git a/Tests/Config/Parser/fixtures/yml/invalid.yml b/Tests/Config/Parser/fixtures/yml/invalid.yml new file mode 100644 index 000000000..1f7c66feb --- /dev/null +++ b/Tests/Config/Parser/fixtures/yml/invalid.yml @@ -0,0 +1 @@ +not_utf-8: 'äöüß' \ No newline at end of file