Skip to content

Commit

Permalink
Add Symfony Yaml PHP constants support integration (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh authored and theofidry committed Oct 31, 2017
1 parent fc726e4 commit bd42498
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
12 changes: 12 additions & 0 deletions fixtures/Parser/files/yaml/constants.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# This file is part of the Alice package.
#
# (c) Nelmio <hello@nelm.io>
#
# 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
5 changes: 4 additions & 1 deletion src/Parser/Chainable/YamlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 30 additions & 3 deletions tests/Parser/Chainable/YamlParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -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()
Expand All @@ -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();
Expand All @@ -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();

Expand Down

0 comments on commit bd42498

Please sign in to comment.