From f821f1c830f33ae6225e868f7d1199c66af9fac8 Mon Sep 17 00:00:00 2001 From: AlexGural Date: Thu, 22 Mar 2018 18:05:42 +0200 Subject: [PATCH 1/4] Update YamlParser.php More comfortable to use constant in some configs. Especially in enum type --- Config/Parser/YamlParser.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } From 973fe0a474d9df6f4917c04789a8cf3a9e6adb80 Mon Sep 17 00:00:00 2001 From: Alex_Gural Date: Wed, 6 Jun 2018 09:22:24 +0300 Subject: [PATCH 2/4] Add test for yml constant --- Tests/Config/Parser/Constant.php | 9 ++++ Tests/Config/Parser/YamlParserTest.php | 53 +++++++++++++++++++ Tests/Config/Parser/fixtures/yml/constant.yml | 2 + Tests/Config/Parser/fixtures/yml/empty.yml | 0 Tests/Config/Parser/fixtures/yml/invalid.yml | 1 + 5 files changed, 65 insertions(+) create mode 100644 Tests/Config/Parser/Constant.php create mode 100644 Tests/Config/Parser/YamlParserTest.php create mode 100644 Tests/Config/Parser/fixtures/yml/constant.yml create mode 100644 Tests/Config/Parser/fixtures/yml/empty.yml create mode 100644 Tests/Config/Parser/fixtures/yml/invalid.yml diff --git a/Tests/Config/Parser/Constant.php b/Tests/Config/Parser/Constant.php new file mode 100644 index 000000000..01b55a795 --- /dev/null +++ b/Tests/Config/Parser/Constant.php @@ -0,0 +1,9 @@ +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 From b4093a21f105cd77210e91d0e6cf47c5ea8b5c2f Mon Sep 17 00:00:00 2001 From: AlexGural Date: Wed, 6 Jun 2018 09:25:08 +0300 Subject: [PATCH 3/4] add space --- Tests/Config/Parser/Constant.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Config/Parser/Constant.php b/Tests/Config/Parser/Constant.php index 01b55a795..34b5e8716 100644 --- a/Tests/Config/Parser/Constant.php +++ b/Tests/Config/Parser/Constant.php @@ -6,4 +6,4 @@ class Constant { const CONSTANT = 1; -} \ No newline at end of file +} From eb4bb0cda19d23a02972b3b631540cc8f8437990 Mon Sep 17 00:00:00 2001 From: Alex_Gural Date: Wed, 6 Jun 2018 17:59:57 +0300 Subject: [PATCH 4/4] add to doc --- Resources/doc/definitions/type-system/enum.md | 3 ++- Tests/Config/Parser/Constant.php | 1 - Tests/Config/Parser/YamlParserTest.php | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Resources/doc/definitions/type-system/enum.md b/Resources/doc/definitions/type-system/enum.md index fc089a688..3dc80136c 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 index 34b5e8716..d38f51962 100644 --- a/Tests/Config/Parser/Constant.php +++ b/Tests/Config/Parser/Constant.php @@ -2,7 +2,6 @@ namespace Overblog\GraphQLBundle\Tests\Config\Parser; - class Constant { const CONSTANT = 1; diff --git a/Tests/Config/Parser/YamlParserTest.php b/Tests/Config/Parser/YamlParserTest.php index a7ddf8625..a4ab7735d 100644 --- a/Tests/Config/Parser/YamlParserTest.php +++ b/Tests/Config/Parser/YamlParserTest.php @@ -9,7 +9,6 @@ class YamlParserTest extends TestCase { - /** @var ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject */ private $containerBuilder; @@ -39,7 +38,7 @@ public function testParseInvalidFile() public function testParseConstant() { - $expected = ["values" => ["constant" => Constant::CONSTANT ]]; + $expected = ['values' => ['constant' => Constant::CONSTANT]]; $actual = YamlParser::parse(new \SplFileInfo(__DIR__.'/fixtures/yml/constant.yml'), $this->containerBuilder); $this->assertEquals($expected, $actual); }