From bd42498862813b1d0b4de8c43877a0a33611f2b4 Mon Sep 17 00:00:00 2001 From: Sullivan SENECHAL Date: Tue, 31 Oct 2017 12:42:23 +0100 Subject: [PATCH] Add Symfony Yaml PHP constants support integration (#829) --- fixtures/Parser/files/yaml/constants.yml | 12 +++++++++ src/Parser/Chainable/YamlParser.php | 5 +++- tests/Parser/Chainable/YamlParserTest.php | 33 ++++++++++++++++++++--- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 fixtures/Parser/files/yaml/constants.yml diff --git a/fixtures/Parser/files/yaml/constants.yml b/fixtures/Parser/files/yaml/constants.yml new file mode 100644 index 000000000..6c04d2b48 --- /dev/null +++ b/fixtures/Parser/files/yaml/constants.yml @@ -0,0 +1,12 @@ +# +# This file is part of the Alice package. +# +# (c) Nelmio +# +# For the full copyright and license information, please view the LICENSE +# file that was distributed with this source code. +# + +Nelmio\Alice\Model\User: + user0: + max_int: !php/const:PHP_INT_MAX diff --git a/src/Parser/Chainable/YamlParser.php b/src/Parser/Chainable/YamlParser.php index 63cfc1a59..aa210a264 100644 --- a/src/Parser/Chainable/YamlParser.php +++ b/src/Parser/Chainable/YamlParser.php @@ -20,6 +20,7 @@ use Nelmio\Alice\Throwable\Exception\Parser\ParseExceptionFactory; use Symfony\Component\Yaml\Exception\ParseException as SymfonyParseException; use Symfony\Component\Yaml\Parser as SymfonyYamlParser; +use Symfony\Component\Yaml\Yaml; final class YamlParser implements ChainableParserInterface { @@ -64,7 +65,9 @@ public function parse(string $file): array } try { - $data = $this->yamlParser->parse(file_get_contents($file)); + $data = defined('Symfony\\Component\\Yaml\\Yaml::PARSE_CONSTANT') + ? $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT) + : $this->yamlParser->parse(file_get_contents($file)); // $data is null only if the YAML file was empty; otherwise an exception is thrown return (null === $data) ? [] : $data; diff --git a/tests/Parser/Chainable/YamlParserTest.php b/tests/Parser/Chainable/YamlParserTest.php index 84fc5f697..81c863a88 100644 --- a/tests/Parser/Chainable/YamlParserTest.php +++ b/tests/Parser/Chainable/YamlParserTest.php @@ -21,6 +21,7 @@ use ReflectionClass; use Symfony\Component\Yaml\Exception\ParseException as SymfonyParseException; use Symfony\Component\Yaml\Parser as SymfonyYamlParser; +use Symfony\Component\Yaml\Yaml; /** * @covers \Nelmio\Alice\Parser\Chainable\YamlParser @@ -141,7 +142,11 @@ public function testUseSymfonyParserToParseFile() $expected = [new \stdClass()]; $symfonyYamlParserProphecy = $this->prophesize(SymfonyYamlParser::class); - $symfonyYamlParserProphecy->parse($fileContent)->willReturn($expected); + if (defined('Symfony\\Component\\Yaml\\Yaml::PARSE_CONSTANT')) { + $symfonyYamlParserProphecy->parse($fileContent, Yaml::PARSE_CONSTANT)->willReturn($expected); + } else { + $symfonyYamlParserProphecy->parse($fileContent)->willReturn($expected); + } /* @var SymfonyYamlParser $symfonyYamlParser */ $symfonyYamlParser = $symfonyYamlParserProphecy->reveal(); @@ -150,7 +155,7 @@ public function testUseSymfonyParserToParseFile() $this->assertSame($expected, $actual); - $symfonyYamlParserProphecy->parse(Argument::any())->shouldBeCalledTimes(1); + $symfonyYamlParserProphecy->parse(Argument::cetera())->shouldBeCalledTimes(1); } public function testReturnsParsedFileContent() @@ -172,6 +177,28 @@ public function testReturnsParsedFileContent() ); } + public function testParseReturnsInterpretedConstants() + { + if (!defined('Symfony\\Component\\Yaml\\Yaml::PARSE_CONSTANT')) { + $this->markTestSkipped('This test needs symfony/yaml v3.2 or higher.'); + } + $symfonyParser = new SymfonyYamlParser(); + + $parser = new YamlParser($symfonyParser); + $actual = $parser->parse(self::$dir.'/constants.yml'); + + $this->assertSame( + [ + 'Nelmio\Alice\Model\User' => [ + 'user0' => [ + 'max_int' => PHP_INT_MAX, + ], + ], + ], + $actual + ); + } + public function testParsingEmptyFileResultsInEmptySet() { $symfonyParser = new SymfonyYamlParser(); @@ -188,7 +215,7 @@ public function testThrowsAnExceptionIfFileNotParsable() $file = self::$dir.'/basic.yml'; $symfonyYamlParserProphecy = $this->prophesize(SymfonyYamlParser::class); - $symfonyYamlParserProphecy->parse(Argument::any())->willThrow(SymfonyParseException::class); + $symfonyYamlParserProphecy->parse(Argument::cetera())->willThrow(SymfonyParseException::class); /* @var SymfonyYamlParser $symfonyYamlParser */ $symfonyYamlParser = $symfonyYamlParserProphecy->reveal();