4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Yaml\Exception\ParseException;
16 use Symfony\Component\Yaml\Inline;
17 use Symfony\Component\Yaml\Yaml;
19 class InlineTest extends TestCase
21 protected function setUp()
23 Inline::initialize(0, 0);
27 * @dataProvider getTestsForParse
29 public function testParse($yaml, $value, $flags = 0)
31 $this->assertSame($value, Inline::parse($yaml, $flags), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
35 * @dataProvider getTestsForParseWithMapObjects
37 public function testParseWithMapObjects($yaml, $value, $flags = Yaml::PARSE_OBJECT_FOR_MAP)
39 $actual = Inline::parse($yaml, $flags);
41 $this->assertSame(serialize($value), serialize($actual));
45 * @dataProvider getTestsForParsePhpConstants
47 public function testParsePhpConstants($yaml, $value)
49 $actual = Inline::parse($yaml, Yaml::PARSE_CONSTANT);
51 $this->assertSame($value, $actual);
54 public function getTestsForParsePhpConstants()
57 array('!php/const Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
58 array('!php/const PHP_INT_MAX', PHP_INT_MAX),
59 array('[!php/const PHP_INT_MAX]', array(PHP_INT_MAX)),
60 array('{ foo: !php/const PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
61 array('!php/const NULL', null),
66 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
67 * @expectedExceptionMessage The constant "WRONG_CONSTANT" is not defined
69 public function testParsePhpConstantThrowsExceptionWhenUndefined()
71 Inline::parse('!php/const WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
75 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
76 * @expectedExceptionMessageRegExp #The string "!php/const PHP_INT_MAX" could not be parsed as a constant.*#
78 public function testParsePhpConstantThrowsExceptionOnInvalidType()
80 Inline::parse('!php/const PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
85 * @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since Symfony 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead on line 1.
86 * @dataProvider getTestsForParseLegacyPhpConstants
88 public function testDeprecatedConstantTag($yaml, $expectedValue)
90 $this->assertSame($expectedValue, Inline::parse($yaml, Yaml::PARSE_CONSTANT));
93 public function getTestsForParseLegacyPhpConstants()
96 array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
97 array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
98 array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
99 array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
100 array('!php/const:NULL', null),
106 * @dataProvider getTestsForParseWithMapObjects
108 public function testParseWithMapObjectsPassingTrue($yaml, $value)
110 $actual = Inline::parse($yaml, false, false, true);
112 $this->assertSame(serialize($value), serialize($actual));
116 * @dataProvider getTestsForDump
118 public function testDump($yaml, $value, $parseFlags = 0)
120 $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
122 $this->assertSame($value, Inline::parse(Inline::dump($value), $parseFlags), 'check consistency');
125 public function testDumpNumericValueWithLocale()
127 $locale = setlocale(LC_NUMERIC, 0);
128 if (false === $locale) {
129 $this->markTestSkipped('Your platform does not support locales.');
133 $requiredLocales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
134 if (false === setlocale(LC_NUMERIC, $requiredLocales)) {
135 $this->markTestSkipped('Could not set any of required locales: '.implode(', ', $requiredLocales));
138 $this->assertEquals('1.2', Inline::dump(1.2));
139 $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
141 setlocale(LC_NUMERIC, $locale);
145 public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
149 $this->assertSame($value, Inline::parse(Inline::dump($value)));
153 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
154 * @expectedExceptionMessage Found unknown escape character "\V".
156 public function testParseScalarWithNonEscapedBlackslashShouldThrowException()
158 Inline::parse('"Foo\Var"');
162 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
164 public function testParseScalarWithNonEscapedBlackslashAtTheEndShouldThrowException()
166 Inline::parse('"Foo\\"');
170 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
172 public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
174 $value = "'don't do somthin' like that'";
175 Inline::parse($value);
179 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
181 public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
183 $value = '"don"t do somthin" like that"';
184 Inline::parse($value);
188 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
190 public function testParseInvalidMappingKeyShouldThrowException()
192 $value = '{ "foo " bar": "bar" }';
193 Inline::parse($value);
198 * @expectedDeprecation Using a colon after an unquoted mapping key that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}") is deprecated since Symfony 3.2 and will throw a ParseException in 4.0 on line 1.
199 * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
201 public function testParseMappingKeyWithColonNotFollowedBySpace()
203 Inline::parse('{1:""}');
207 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
209 public function testParseInvalidMappingShouldThrowException()
211 Inline::parse('[foo] bar');
215 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
217 public function testParseInvalidSequenceShouldThrowException()
219 Inline::parse('{ foo: bar } bar');
222 public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
224 $value = "'don''t do somthin'' like that'";
225 $expect = "don't do somthin' like that";
227 $this->assertSame($expect, Inline::parseScalar($value));
231 * @dataProvider getDataForParseReferences
233 public function testParseReferences($yaml, $expected)
235 $this->assertSame($expected, Inline::parse($yaml, 0, array('var' => 'var-value')));
240 * @dataProvider getDataForParseReferences
242 public function testParseReferencesAsFifthArgument($yaml, $expected)
244 $this->assertSame($expected, Inline::parse($yaml, false, false, false, array('var' => 'var-value')));
247 public function getDataForParseReferences()
250 'scalar' => array('*var', 'var-value'),
251 'list' => array('[ *var ]', array('var-value')),
252 'list-in-list' => array('[[ *var ]]', array(array('var-value'))),
253 'map-in-list' => array('[ { key: *var } ]', array(array('key' => 'var-value'))),
254 'embedded-mapping-in-list' => array('[ key: *var ]', array(array('key' => 'var-value'))),
255 'map' => array('{ key: *var }', array('key' => 'var-value')),
256 'list-in-map' => array('{ key: [*var] }', array('key' => array('var-value'))),
257 'map-in-map' => array('{ foo: { bar: *var } }', array('foo' => array('bar' => 'var-value'))),
261 public function testParseMapReferenceInSequence()
268 $this->assertSame(array($foo), Inline::parse('[*foo]', 0, array('foo' => $foo)));
274 public function testParseMapReferenceInSequenceAsFifthArgument()
281 $this->assertSame(array($foo), Inline::parse('[*foo]', false, false, false, array('foo' => $foo)));
285 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
286 * @expectedExceptionMessage A reference must contain at least one character at line 1.
288 public function testParseUnquotedAsterisk()
290 Inline::parse('{ foo: * }');
294 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
295 * @expectedExceptionMessage A reference must contain at least one character at line 1.
297 public function testParseUnquotedAsteriskFollowedByAComment()
299 Inline::parse('{ foo: * #foo }');
303 * @dataProvider getReservedIndicators
305 public function testParseUnquotedScalarStartingWithReservedIndicator($indicator)
307 if (method_exists($this, 'expectExceptionMessage')) {
308 $this->expectException(ParseException::class);
309 $this->expectExceptionMessage(sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
311 $this->setExpectedException(ParseException::class, sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
314 Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
317 public function getReservedIndicators()
319 return array(array('@'), array('`'));
323 * @dataProvider getScalarIndicators
325 public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
327 if (method_exists($this, 'expectExceptionMessage')) {
328 $this->expectException(ParseException::class);
329 $this->expectExceptionMessage(sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
331 $this->setExpectedException(ParseException::class, sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
334 Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
337 public function getScalarIndicators()
339 return array(array('|'), array('>'));
344 * @expectedDeprecation Not quoting the scalar "%bar " starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0 on line 1.
345 * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
347 public function testParseUnquotedScalarStartingWithPercentCharacter()
349 Inline::parse('{ foo: %bar }');
353 * @dataProvider getDataForIsHash
355 public function testIsHash($array, $expected)
357 $this->assertSame($expected, Inline::isHash($array));
360 public function getDataForIsHash()
363 array(array(), false),
364 array(array(1, 2, 3), false),
365 array(array(2 => 1, 1 => 2, 0 => 3), true),
366 array(array('foo' => 1, 'bar' => 2), true),
370 public function getTestsForParse()
375 array('false', false),
382 array('"quoted string"', 'quoted string'),
383 array("'quoted string'", 'quoted string'),
384 array('12.30e+02', 12.30e+02),
385 array('123.45_67', 123.4567),
386 array('0x4D2', 0x4D2),
387 array('0x_4_D_2_', 0x4D2),
388 array('02333', 02333),
389 array('0_2_3_3_3', 02333),
390 array('.Inf', -log(0)),
391 array('-.Inf', log(0)),
392 array("'686e444'", '686e444'),
393 array('686e444', 646e444),
394 array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
395 array('"foo\r\nbar"', "foo\r\nbar"),
396 array("'foo#bar'", 'foo#bar'),
397 array("'foo # bar'", 'foo # bar'),
398 array("'#cfcfcf'", '#cfcfcf'),
399 array('::form_base.html.twig', '::form_base.html.twig'),
401 // Pre-YAML-1.2 booleans
404 array("'yes'", 'yes'),
407 array("'off'", 'off'),
409 array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
410 array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
411 array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
412 array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
413 array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
415 array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
416 array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
419 // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
420 array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
421 array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
422 array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
425 array('{foo: bar,bar: foo,"false": false, "null": null,integer: 12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
426 array('{ foo : bar, bar : foo, "false" : false, "null" : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
427 array('{foo: \'bar\', bar: \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
428 array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
429 array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
430 array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
431 array('{"foo:bar": "baz"}', array('foo:bar' => 'baz')),
432 array('{"foo":"bar"}', array('foo' => 'bar')),
434 // nested sequences and mappings
435 array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
436 array('[foo, {bar: foo}]', array('foo', array('bar' => 'foo'))),
437 array('{ foo: {bar: foo} }', array('foo' => array('bar' => 'foo'))),
438 array('{ foo: [bar, foo] }', array('foo' => array('bar', 'foo'))),
439 array('{ foo:{bar: foo} }', array('foo' => array('bar' => 'foo'))),
440 array('{ foo:[bar, foo] }', array('foo' => array('bar', 'foo'))),
442 array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
444 array('[{ foo: {bar: foo} }]', array(array('foo' => array('bar' => 'foo')))),
446 array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
448 array('[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
450 array('[foo, bar: { foo: bar }]', array('foo', '1' => array('bar' => array('foo' => 'bar')))),
451 array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
455 public function getTestsForParseWithMapObjects()
460 array('false', false),
464 array('"quoted string"', 'quoted string'),
465 array("'quoted string'", 'quoted string'),
466 array('12.30e+02', 12.30e+02),
467 array('0x4D2', 0x4D2),
468 array('02333', 02333),
469 array('.Inf', -log(0)),
470 array('-.Inf', log(0)),
471 array("'686e444'", '686e444'),
472 array('686e444', 646e444),
473 array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
474 array('"foo\r\nbar"', "foo\r\nbar"),
475 array("'foo#bar'", 'foo#bar'),
476 array("'foo # bar'", 'foo # bar'),
477 array("'#cfcfcf'", '#cfcfcf'),
478 array('::form_base.html.twig', '::form_base.html.twig'),
480 array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
481 array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
482 array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
483 array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
484 array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
486 array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
487 array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
490 // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
491 array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
492 array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
493 array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
496 array('{foo: bar,bar: foo,"false": false,"null": null,integer: 12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_OBJECT_FOR_MAP),
497 array('{ foo : bar, bar : foo, "false" : false, "null" : null, integer : 12 }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_OBJECT_FOR_MAP),
498 array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
499 array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
500 array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
501 array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
502 array('{"foo:bar": "baz"}', (object) array('foo:bar' => 'baz')),
503 array('{"foo":"bar"}', (object) array('foo' => 'bar')),
505 // nested sequences and mappings
506 array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
507 array('[foo, {bar: foo}]', array('foo', (object) array('bar' => 'foo'))),
508 array('{ foo: {bar: foo} }', (object) array('foo' => (object) array('bar' => 'foo'))),
509 array('{ foo: [bar, foo] }', (object) array('foo' => array('bar', 'foo'))),
511 array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
513 array('[{ foo: {bar: foo} }]', array((object) array('foo' => (object) array('bar' => 'foo')))),
515 array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
517 array('[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]', array('foo', (object) array('bar' => 'foo', 'foo' => array('foo', (object) array('bar' => 'foo'))), array('foo', (object) array('bar' => 'foo')))),
519 array('[foo, bar: { foo: bar }]', array('foo', '1' => (object) array('bar' => (object) array('foo' => 'bar')))),
520 array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', (object) array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
522 array('{}', new \stdClass()),
523 array('{ foo : bar, bar : {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
524 array('{ foo : [], bar : {} }', (object) array('foo' => array(), 'bar' => new \stdClass())),
525 array('{foo: \'bar\', bar: {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
526 array('{\'foo\': \'bar\', "bar": {}}', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
527 array('{\'foo\': \'bar\', "bar": \'{}\'}', (object) array('foo' => 'bar', 'bar' => '{}')),
529 array('[foo, [{}, {}]]', array('foo', array(new \stdClass(), new \stdClass()))),
530 array('[foo, [[], {}]]', array('foo', array(array(), new \stdClass()))),
531 array('[foo, [[{}, {}], {}]]', array('foo', array(array(new \stdClass(), new \stdClass()), new \stdClass()))),
532 array('[foo, {bar: {}}]', array('foo', '1' => (object) array('bar' => new \stdClass()))),
536 public function getTestsForDump()
540 array('false', false),
543 array("'1_2'", '1_2'),
545 array("'12_'", '12_'),
546 array("'quoted string'", 'quoted string'),
547 array('!!float 1230', 12.30e+02),
548 array('1234', 0x4D2),
549 array('1243', 02333),
550 array("'0x_4_D_2_'", '0x_4_D_2_'),
551 array("'0_2_3_3_3'", '0_2_3_3_3'),
552 array('.Inf', -log(0)),
553 array('-.Inf', log(0)),
554 array("'686e444'", '686e444'),
555 array('"foo\r\nbar"', "foo\r\nbar"),
556 array("'foo#bar'", 'foo#bar'),
557 array("'foo # bar'", 'foo # bar'),
558 array("'#cfcfcf'", '#cfcfcf'),
560 array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
562 array("'-dash'", '-dash'),
565 // Pre-YAML-1.2 booleans
568 array("'yes'", 'yes'),
571 array("'off'", 'off'),
574 array('[foo, bar, false, null, 12]', array('foo', 'bar', false, null, 12)),
575 array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
578 array('{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
579 array('{ foo: bar, bar: \'foo: bar\' }', array('foo' => 'bar', 'bar' => 'foo: bar')),
581 // nested sequences and mappings
582 array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
584 array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
586 array('{ foo: { bar: foo } }', array('foo' => array('bar' => 'foo'))),
588 array('[foo, { bar: foo }]', array('foo', array('bar' => 'foo'))),
590 array('[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
592 array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
594 array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
599 * @dataProvider getTimestampTests
601 public function testParseTimestampAsUnixTimestampByDefault($yaml, $year, $month, $day, $hour, $minute, $second)
603 $this->assertSame(gmmktime($hour, $minute, $second, $month, $day, $year), Inline::parse($yaml));
607 * @dataProvider getTimestampTests
609 public function testParseTimestampAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second, $timezone)
611 $expected = new \DateTime($yaml);
612 $expected->setTimeZone(new \DateTimeZone('UTC'));
613 $expected->setDate($year, $month, $day);
615 if (\PHP_VERSION_ID >= 70100) {
616 $expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
618 $expected->setTime($hour, $minute, $second);
621 $date = Inline::parse($yaml, Yaml::PARSE_DATETIME);
622 $this->assertEquals($expected, $date);
623 $this->assertSame($timezone, $date->format('O'));
626 public function getTimestampTests()
629 'canonical' => array('2001-12-15T02:59:43.1Z', 2001, 12, 15, 2, 59, 43.1, '+0000'),
630 'ISO-8601' => array('2001-12-15t21:59:43.10-05:00', 2001, 12, 16, 2, 59, 43.1, '-0500'),
631 'spaced' => array('2001-12-15 21:59:43.10 -5', 2001, 12, 16, 2, 59, 43.1, '-0500'),
632 'date' => array('2001-12-15', 2001, 12, 15, 0, 0, 0, '+0000'),
637 * @dataProvider getTimestampTests
639 public function testParseNestedTimestampListAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second)
641 $expected = new \DateTime($yaml);
642 $expected->setTimeZone(new \DateTimeZone('UTC'));
643 $expected->setDate($year, $month, $day);
644 if (\PHP_VERSION_ID >= 70100) {
645 $expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
647 $expected->setTime($hour, $minute, $second);
650 $expectedNested = array('nested' => array($expected));
651 $yamlNested = "{nested: [$yaml]}";
653 $this->assertEquals($expectedNested, Inline::parse($yamlNested, Yaml::PARSE_DATETIME));
657 * @dataProvider getDateTimeDumpTests
659 public function testDumpDateTime($dateTime, $expected)
661 $this->assertSame($expected, Inline::dump($dateTime));
664 public function getDateTimeDumpTests()
668 $dateTime = new \DateTime('2001-12-15 21:59:43', new \DateTimeZone('UTC'));
669 $tests['date-time-utc'] = array($dateTime, '2001-12-15T21:59:43+00:00');
671 $dateTime = new \DateTimeImmutable('2001-07-15 21:59:43', new \DateTimeZone('Europe/Berlin'));
672 $tests['immutable-date-time-europe-berlin'] = array($dateTime, '2001-07-15T21:59:43+02:00');
678 * @dataProvider getBinaryData
680 public function testParseBinaryData($data)
682 $this->assertSame('Hello world', Inline::parse($data));
685 public function getBinaryData()
688 'enclosed with double quotes' => array('!!binary "SGVsbG8gd29ybGQ="'),
689 'enclosed with single quotes' => array("!!binary 'SGVsbG8gd29ybGQ='"),
690 'containing spaces' => array('!!binary "SGVs bG8gd 29ybGQ="'),
695 * @dataProvider getInvalidBinaryData
696 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
698 public function testParseInvalidBinaryData($data, $expectedMessage)
700 if (method_exists($this, 'expectException')) {
701 $this->expectExceptionMessageRegExp($expectedMessage);
703 $this->setExpectedExceptionRegExp(ParseException::class, $expectedMessage);
706 Inline::parse($data);
709 public function getInvalidBinaryData()
712 'length not a multiple of four' => array('!!binary "SGVsbG8d29ybGQ="', '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/'),
713 'invalid characters' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
714 'too many equals characters' => array('!!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
715 'misplaced equals character' => array('!!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
720 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
721 * @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported} at line 1.
723 public function testNotSupportedMissingValue()
725 Inline::parse('{this, is not, supported}');
728 public function testVeryLongQuotedStrings()
730 $longStringWithQuotes = str_repeat("x\r\n\\\"x\"x", 1000);
732 $yamlString = Inline::dump(array('longStringWithQuotes' => $longStringWithQuotes));
733 $arrayFromYaml = Inline::parse($yamlString);
735 $this->assertEquals($longStringWithQuotes, $arrayFromYaml['longStringWithQuotes']);
740 * @expectedDeprecation Omitting the key of a mapping is deprecated and will throw a ParseException in 4.0 on line 1.
742 public function testOmittedMappingKeyIsParsedAsColon()
744 $this->assertSame(array(':' => 'foo'), Inline::parse('{: foo}'));
748 * @dataProvider getTestsForNullValues
750 public function testParseMissingMappingValueAsNull($yaml, $expected)
752 $this->assertSame($expected, Inline::parse($yaml));
755 public function getTestsForNullValues()
758 'null before closing curly brace' => array('{foo:}', array('foo' => null)),
759 'null before comma' => array('{foo:, bar: baz}', array('foo' => null, 'bar' => 'baz')),
763 public function testTheEmptyStringIsAValidMappingKey()
765 $this->assertSame(array('' => 'foo'), Inline::parse('{ "": foo }'));
770 * @expectedDeprecation Implicit casting of incompatible mapping keys to strings is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead on line 1.
771 * @dataProvider getNotPhpCompatibleMappingKeyData
773 public function testImplicitStringCastingOfMappingKeysIsDeprecated($yaml, $expected)
775 $this->assertSame($expected, Inline::parse($yaml));
780 * @expectedDeprecation Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since Symfony 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable instead.
781 * @expectedDeprecation Implicit casting of incompatible mapping keys to strings is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead on line 1.
782 * @dataProvider getNotPhpCompatibleMappingKeyData
784 public function testExplicitStringCastingOfMappingKeys($yaml, $expected)
786 $this->assertSame($expected, Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS));
789 public function getNotPhpCompatibleMappingKeyData()
792 'boolean-true' => array('{true: "foo"}', array('true' => 'foo')),
793 'boolean-false' => array('{false: "foo"}', array('false' => 'foo')),
794 'null' => array('{null: "foo"}', array('null' => 'foo')),
795 'float' => array('{0.25: "foo"}', array('0.25' => 'foo')),
801 * @expectedDeprecation Support for the !str tag is deprecated since Symfony 3.4. Use the !!str tag instead on line 1.
803 public function testDeprecatedStrTag()
805 $this->assertSame(array('foo' => 'bar'), Inline::parse('{ foo: !str bar }'));
809 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
810 * @expectedExceptionMessage Unexpected end of line, expected one of ",}" at line 1 (near "{abc: 'def'").
812 public function testUnfinishedInlineMap()
814 Inline::parse("{abc: 'def'");