@@ -21,17 +21,17 @@ class InlineTest extends TestCase
21
21
/**
22
22
* @dataProvider getTestsForParse
23
23
*/
24
- public function testParse ($ yaml , $ value )
24
+ public function testParse ($ yaml , $ value, $ flags = 0 )
25
25
{
26
- $ this ->assertSame ($ value , Inline::parse ($ yaml ), sprintf ('::parse() converts an inline YAML to a PHP structure (%s) ' , $ yaml ));
26
+ $ this ->assertSame ($ value , Inline::parse ($ yaml, $ flags ), sprintf ('::parse() converts an inline YAML to a PHP structure (%s) ' , $ yaml ));
27
27
}
28
28
29
29
/**
30
30
* @dataProvider getTestsForParseWithMapObjects
31
31
*/
32
- public function testParseWithMapObjects ($ yaml , $ value )
32
+ public function testParseWithMapObjects ($ yaml , $ value, $ flags = Yaml:: PARSE_OBJECT_FOR_MAP )
33
33
{
34
- $ actual = Inline::parse ($ yaml , Yaml:: PARSE_OBJECT_FOR_MAP );
34
+ $ actual = Inline::parse ($ yaml , $ flags );
35
35
36
36
$ this ->assertSame (serialize ($ value ), serialize ($ actual ));
37
37
}
@@ -88,11 +88,11 @@ public function testParseWithMapObjectsPassingTrue($yaml, $value)
88
88
/**
89
89
* @dataProvider getTestsForDump
90
90
*/
91
- public function testDump ($ yaml , $ value )
91
+ public function testDump ($ yaml , $ value, $ parseFlags = 0 )
92
92
{
93
93
$ this ->assertEquals ($ yaml , Inline::dump ($ value ), sprintf ('::dump() converts a PHP structure to an inline YAML (%s) ' , $ yaml ));
94
94
95
- $ this ->assertSame ($ value , Inline::parse (Inline::dump ($ value )), 'check consistency ' );
95
+ $ this ->assertSame ($ value , Inline::parse (Inline::dump ($ value ), $ parseFlags ), 'check consistency ' );
96
96
}
97
97
98
98
public function testDumpNumericValueWithLocale ()
@@ -385,8 +385,8 @@ public function getTestsForParse()
385
385
array ('[ \'foo,bar \', \'foo bar \'] ' , array ('foo,bar ' , 'foo bar ' )),
386
386
387
387
// mappings
388
- array ('{foo: bar,bar: foo,false: false,null: null,integer: 12} ' , array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 )),
389
- array ('{ foo : bar, bar : foo, false : false, null : null, integer : 12 } ' , array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 )),
388
+ array ('{foo: bar,bar: foo,false: false,null: null,integer: 12} ' , array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 ), Yaml:: PARSE_KEYS_AS_STRING ),
389
+ array ('{ foo : bar, bar : foo, false : false, null : null, integer : 12 } ' , array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 ), Yaml:: PARSE_KEYS_AS_STRING ),
390
390
array ('{foo: \'bar \', bar: \'foo: bar \'} ' , array ('foo ' => 'bar ' , 'bar ' => 'foo: bar ' )),
391
391
array ('{ \'foo \': \'bar \', "bar": \'foo: bar \'} ' , array ('foo ' => 'bar ' , 'bar ' => 'foo: bar ' )),
392
392
array ('{ \'foo \'\'\': \'bar \', "bar\"": \'foo: bar \'} ' , array ('foo \'' => 'bar ' , 'bar" ' => 'foo: bar ' )),
@@ -454,8 +454,8 @@ public function getTestsForParseWithMapObjects()
454
454
array ('[ \'foo,bar \', \'foo bar \'] ' , array ('foo,bar ' , 'foo bar ' )),
455
455
456
456
// mappings
457
- array ('{foo: bar,bar: foo,false: false,null: null,integer: 12} ' , (object ) array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 )),
458
- array ('{ foo : bar, bar : foo, false : false, null : null, integer : 12 } ' , (object ) array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 )),
457
+ 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 | Yaml:: PARSE_KEYS_AS_STRING ),
458
+ 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 | Yaml:: PARSE_KEYS_AS_STRING ),
459
459
array ('{foo: \'bar \', bar: \'foo: bar \'} ' , (object ) array ('foo ' => 'bar ' , 'bar ' => 'foo: bar ' )),
460
460
array ('{ \'foo \': \'bar \', "bar": \'foo: bar \'} ' , (object ) array ('foo ' => 'bar ' , 'bar ' => 'foo: bar ' )),
461
461
array ('{ \'foo \'\'\': \'bar \', "bar\"": \'foo: bar \'} ' , (object ) array ('foo \'' => 'bar ' , 'bar" ' => 'foo: bar ' )),
@@ -534,7 +534,7 @@ public function getTestsForDump()
534
534
array ('[ \'foo,bar \', \'foo bar \'] ' , array ('foo,bar ' , 'foo bar ' )),
535
535
536
536
// mappings
537
- array ('{ foo: bar, bar: foo, \'false \': false, \'null \': null, integer: 12 } ' , array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 )),
537
+ array ('{ foo: bar, bar: foo, \'false \': false, \'null \': null, integer: 12 } ' , array ('foo ' => 'bar ' , 'bar ' => 'foo ' , 'false ' => false , 'null ' => null , 'integer ' => 12 ), Yaml:: PARSE_KEYS_AS_STRING ),
538
538
array ('{ foo: bar, bar: \'foo: bar \' } ' , array ('foo ' => 'bar ' , 'bar ' => 'foo: bar ' )),
539
539
540
540
// nested sequences and mappings
@@ -702,4 +702,32 @@ public function testOmittedMappingKeyIsParsedAsColon()
702
702
{
703
703
$ this ->assertSame (array (': ' => 'foo ' ), Inline::parse ('{: foo} ' ));
704
704
}
705
+
706
+ /**
707
+ * @group legacy
708
+ * @expectedDeprecation Implicit casting of incompatible mapping keys to strings is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Pass the PARSE_KEYS_AS_STRING flag to explicitly enable the type casts.
709
+ * @dataProvider getNotPhpCompatibleMappingKeyData
710
+ */
711
+ public function testImplicitStringCastingOfMappingKeysIsDeprecated ($ yaml , $ expected )
712
+ {
713
+ $ this ->assertSame ($ expected , Inline::parse ($ yaml ));
714
+ }
715
+
716
+ /**
717
+ * @dataProvider getNotPhpCompatibleMappingKeyData
718
+ */
719
+ public function testExplicitStringCastingOfMappingKeys ($ yaml , $ expected )
720
+ {
721
+ $ this ->assertSame ($ expected , Inline::parse ($ yaml , Yaml::PARSE_KEYS_AS_STRING ));
722
+ }
723
+
724
+ public function getNotPhpCompatibleMappingKeyData ()
725
+ {
726
+ return array (
727
+ 'boolean-true ' => array ('{true: "foo"} ' , array ('true ' => 'foo ' )),
728
+ 'boolean-false ' => array ('{false: "foo"} ' , array ('false ' => 'foo ' )),
729
+ 'null ' => array ('{null: "foo"} ' , array ('null ' => 'foo ' )),
730
+ 'float ' => array ('{0.25: "foo"} ' , array ('0.25 ' => 'foo ' )),
731
+ );
732
+ }
705
733
}
0 commit comments