Skip to content

Commit

Permalink
Add Symfony Yaml PHP constants support integration
Browse files Browse the repository at this point in the history
Ref: http://symfony.com/blog/new-in-symfony-3-2-php-constants-in-yaml-files

This addition is backward compatible as the Yaml flag will be used only
if it is defined.
  • Loading branch information
soullivaneuh committed Oct 31, 2017
1 parent 9eb98ad commit 59ad107
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Nelmio/Alice/Fixtures/Parser/Methods/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class Yaml extends Base
public function parse($file)
{
$yaml = $this->compilePhp($file);
$data = YamlParser::parse($yaml);
$data = defined('Symfony\\Component\\Yaml\\Yaml::PARSE_CONSTANT')
? YamlParser::parse($yaml, YamlParser::PARSE_CONSTANT)
: YamlParser::parse($yaml);

if (!is_array($data)) {
throw new \UnexpectedValueException('Yaml files must parse to an array of data');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# This file is part of the Alice package.
#
# (c) Nelmio <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#

max_int: !php/const:PHP_INT_MAX
15 changes: 15 additions & 0 deletions tests/Nelmio/Alice/Fixtures/Parser/Methods/YamlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ public function testParseReturnsAYamlArray()
);
}

public function testParseReturnsInterpretedConstants()
{
if (!defined('Symfony\\Component\\Yaml\\Yaml::PARSE_CONSTANT')) {
$this->markTestSkipped('This test needs symfony/yaml v3.2 or higher.');
}
$data = $this->parser->parse(self::$dir.'/file_with_constants.yml');

$this->assertSame(
[
'max_int' => PHP_INT_MAX,
],
$data
);
}

/**
* @group legacy
*/
Expand Down

0 comments on commit 59ad107

Please sign in to comment.