Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Config/Parser/YamlParser.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not_utf-8: '����'