Skip to content

Commit

Permalink
Merge 9ad0d11 into 9109b9d
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGural committed Jun 8, 2018
2 parents 9109b9d + 9ad0d11 commit e7d7e04
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Config/Parser/YamlParser.php
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/definitions/type-system/enum.md
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions Tests/Config/Parser/Constant.php
@@ -0,0 +1,8 @@
<?php

namespace Overblog\GraphQLBundle\Tests\Config\Parser;

class Constant
{
const CONSTANT = 1;
}
52 changes: 52 additions & 0 deletions Tests/Config/Parser/YamlParserTest.php
@@ -0,0 +1,52 @@
<?php

namespace Overblog\GraphQLBundle\Tests\Config\Parser;

use Overblog\GraphQLBundle\Config\Parser\YamlParser;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

class YamlParserTest extends TestCase
{
/** @var ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject */
private $containerBuilder;

public function setUp()
{
$this->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);
}
}
2 changes: 2 additions & 0 deletions Tests/Config/Parser/fixtures/yml/constant.yml
@@ -0,0 +1,2 @@
values:
constant: !php/const Overblog\GraphQLBundle\Tests\Config\Parser\Constant::CONSTANT
Empty file.
1 change: 1 addition & 0 deletions Tests/Config/Parser/fixtures/yml/invalid.yml
@@ -0,0 +1 @@
not_utf-8: '����'

0 comments on commit e7d7e04

Please sign in to comment.