namespace Symfony\Component\Yaml\Tests;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Parser;
/**
* @dataProvider getDataFormSpecifications
*/
- public function testSpecifications($file, $expected, $yaml, $comment)
+ public function testSpecifications($file, $expected, $yaml, $comment, $deprecated)
{
+ $deprecations = array();
+
+ if ($deprecated) {
+ set_error_handler(function ($type, $msg) use (&$deprecations) {
+ if (E_USER_DEPRECATED !== $type) {
+ restore_error_handler();
+
+ if (class_exists('PHPUnit_Util_ErrorHandler')) {
+ return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
+ }
+
+ return call_user_func_array('PHPUnit\Util\ErrorHandler::handleError', func_get_args());
+ }
+
+ $deprecations[] = $msg;
+ });
+ }
+
$this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
+
+ if ($deprecated) {
+ restore_error_handler();
+
+ $this->assertCount(1, $deprecations);
+ $this->assertContains('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', $deprecations[0]);
+ }
}
public function getDataFormSpecifications()
} else {
eval('$expected = '.trim($test['php']).';');
- $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']);
+ $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test'], isset($test['deprecated']) ? $test['deprecated'] : false);
}
}
}
public function testObjectSupportEnabled()
{
$input = <<<'EOF'
-foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
+foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
bar: 1
EOF;
- $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
+ $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
+ }
+ /**
+ * @group legacy
+ */
+ public function testObjectSupportEnabledPassingTrue()
+ {
$input = <<<'EOF'
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
bar: 1
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
}
+ /**
+ * @group legacy
+ */
+ public function testObjectSupportEnabledWithDeprecatedTag()
+ {
+ $input = <<<'EOF'
+foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
+bar: 1
+EOF;
+ $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
+ }
+
/**
* @dataProvider invalidDumpedObjectProvider
*/
* @dataProvider getObjectForMapTests
*/
public function testObjectForMap($yaml, $expected)
+ {
+ $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
+ }
+
+ /**
+ * @group legacy
+ * @dataProvider getObjectForMapTests
+ */
+ public function testObjectForMapEnabledWithMappingUsingBooleanToggles($yaml, $expected)
{
$this->assertEquals($expected, $this->parser->parse($yaml, false, false, true));
}
*/
public function testObjectsSupportDisabledWithExceptions($yaml)
{
- $this->parser->parse($yaml, true, false);
+ $this->parser->parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
+ }
+
+ /**
+ * @group legacy
+ * @dataProvider invalidDumpedObjectProvider
+ * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+ */
+ public function testObjectsSupportDisabledWithExceptionsUsingBooleanToggles($yaml)
+ {
+ $this->parser->parse($yaml, true);
}
public function invalidDumpedObjectProvider()
*
* @see http://yaml.org/spec/1.2/spec.html#id2759572
* @see http://yaml.org/spec/1.1/#id932806
+ * @group legacy
*/
public function testMappingDuplicateKeyBlock()
{
$this->assertSame($expected, Yaml::parse($input));
}
+ /**
+ * @group legacy
+ */
public function testMappingDuplicateKeyFlow()
{
$input = <<<'EOD'
$this->assertSame($expected, Yaml::parse($input));
}
+ /**
+ * @group legacy
+ * @dataProvider getParseExceptionOnDuplicateData
+ * @expectedDeprecation Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated %s.
+ * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
+ */
+ public function testParseExceptionOnDuplicate($input, $duplicateKey, $lineNumber)
+ {
+ Yaml::parse($input);
+ }
+
+ public function getParseExceptionOnDuplicateData()
+ {
+ $tests = array();
+
+ $yaml = <<<EOD
+parent: { child: first, child: duplicate }
+EOD;
+ $tests[] = array($yaml, 'child', 1);
+
+ $yaml = <<<EOD
+parent:
+ child: first,
+ child: duplicate
+EOD;
+ $tests[] = array($yaml, 'child', 3);
+
+ $yaml = <<<EOD
+parent: { child: foo }
+parent: { child: bar }
+EOD;
+ $tests[] = array($yaml, 'parent', 2);
+
+ $yaml = <<<EOD
+parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
+EOD;
+ $tests[] = array($yaml, 'child_mapping', 1);
+
+ $yaml = <<<EOD
+parent:
+ child_mapping:
+ value: bar
+ child_mapping:
+ value: bar
+EOD;
+ $tests[] = array($yaml, 'child_mapping', 4);
+
+ $yaml = <<<EOD
+parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
+EOD;
+ $tests[] = array($yaml, 'child_sequence', 1);
+
+ $yaml = <<<EOD
+parent:
+ child_sequence:
+ - key1
+ - key2
+ - key3
+ child_sequence:
+ - key1
+ - key2
+ - key3
+EOD;
+ $tests[] = array($yaml, 'child_sequence', 6);
+
+ return $tests;
+ }
+
public function testEmptyValue()
{
$input = <<<'EOF'
}
/**
- * @group legacy
- * @expectedDeprecation Using a colon in the unquoted mapping value "bar: baz" in line 1 is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.
- * throw ParseException in Symfony 3.0
+ * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+ * @expectedExceptionMessage A colon cannot be used in an unquoted mapping value
*/
public function testColonInMappingValueException()
{
);
}
+ /**
+ * @dataProvider getBinaryData
+ */
+ public function testParseBinaryData($data)
+ {
+ $this->assertSame(array('data' => 'Hello world'), $this->parser->parse($data));
+ }
+
+ public function getBinaryData()
+ {
+ return array(
+ 'enclosed with double quotes' => array('data: !!binary "SGVsbG8gd29ybGQ="'),
+ 'enclosed with single quotes' => array("data: !!binary 'SGVsbG8gd29ybGQ='"),
+ 'containing spaces' => array('data: !!binary "SGVs bG8gd 29ybGQ="'),
+ 'in block scalar' => array(
+ <<<'EOT'
+data: !!binary |
+ SGVsbG8gd29ybGQ=
+EOT
+ ),
+ 'containing spaces in block scalar' => array(
+ <<<'EOT'
+data: !!binary |
+ SGVs bG8gd 29ybGQ=
+EOT
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider getInvalidBinaryData
+ * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+ */
+ public function testParseInvalidBinaryData($data, $expectedMessage)
+ {
+ if (method_exists($this, 'expectException')) {
+ $this->expectExceptionMessageRegExp($expectedMessage);
+ } else {
+ $this->setExpectedExceptionRegExp(ParseException::class, $expectedMessage);
+ }
+
+ $this->parser->parse($data);
+ }
+
+ public function getInvalidBinaryData()
+ {
+ return array(
+ 'length not a multiple of four' => array('data: !!binary "SGVsbG8d29ybGQ="', '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/'),
+ 'invalid characters' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
+ 'too many equals characters' => array('data: !!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
+ 'misplaced equals character' => array('data: !!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
+ 'length not a multiple of four in block scalar' => array(
+ <<<'EOT'
+data: !!binary |
+ SGVsbG8d29ybGQ=
+EOT
+ ,
+ '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/',
+ ),
+ 'invalid characters in block scalar' => array(
+ <<<'EOT'
+data: !!binary |
+ SGVsbG8#d29ybGQ=
+EOT
+ ,
+ '/The base64 encoded data \(.*\) contains invalid characters/',
+ ),
+ 'too many equals characters in block scalar' => array(
+ <<<'EOT'
+data: !!binary |
+ SGVsbG8gd29yb===
+EOT
+ ,
+ '/The base64 encoded data \(.*\) contains invalid characters/',
+ ),
+ 'misplaced equals character in block scalar' => array(
+ <<<'EOT'
+data: !!binary |
+ SGVsbG8gd29ybG=Q
+EOT
+ ,
+ '/The base64 encoded data \(.*\) contains invalid characters/',
+ ),
+ );
+ }
+
+ public function testParseDateAsMappingValue()
+ {
+ $yaml = <<<'EOT'
+date: 2002-12-14
+EOT;
+ $expectedDate = new \DateTime();
+ $expectedDate->setTimeZone(new \DateTimeZone('UTC'));
+ $expectedDate->setDate(2002, 12, 14);
+ $expectedDate->setTime(0, 0, 0);
+
+ $this->assertEquals(array('date' => $expectedDate), $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
+ }
+
/**
* @param $lineNumber
* @param $yaml
);
}
+ public function testParseMultiLineQuotedString()
+ {
+ $yaml = <<<EOT
+foo: "bar
+ baz
+ foobar
+foo"
+bar: baz
+EOT;
+
+ $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
+ }
+
+ public function testParseMultiLineUnquotedString()
+ {
+ $yaml = <<<EOT
+foo: bar
+ baz
+ foobar
+ foo
+bar: baz
+EOT;
+
+ $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
+ }
+
public function testCanParseVeryLongValue()
{
$longStringWithSpaces = str_repeat('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ', 20000);