class InlineTest extends TestCase
{
+ protected function setUp()
+ {
+ Inline::initialize(0, 0);
+ }
+
/**
* @dataProvider getTestsForParse
*/
- public function testParse($yaml, $value)
+ public function testParse($yaml, $value, $flags = 0)
{
- $this->assertSame($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
+ $this->assertSame($value, Inline::parse($yaml, $flags), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
}
/**
* @dataProvider getTestsForParseWithMapObjects
*/
- public function testParseWithMapObjects($yaml, $value)
+ public function testParseWithMapObjects($yaml, $value, $flags = Yaml::PARSE_OBJECT_FOR_MAP)
{
- $actual = Inline::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP);
+ $actual = Inline::parse($yaml, $flags);
$this->assertSame(serialize($value), serialize($actual));
}
public function getTestsForParsePhpConstants()
{
return array(
- array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
- array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
- array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
- array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
+ array('!php/const Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
+ array('!php/const PHP_INT_MAX', PHP_INT_MAX),
+ array('[!php/const PHP_INT_MAX]', array(PHP_INT_MAX)),
+ array('{ foo: !php/const PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
+ array('!php/const NULL', null),
);
}
*/
public function testParsePhpConstantThrowsExceptionWhenUndefined()
{
- Inline::parse('!php/const:WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
+ Inline::parse('!php/const WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
}
/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessageRegExp #The string "!php/const:PHP_INT_MAX" could not be parsed as a constant.*#
+ * @expectedExceptionMessageRegExp #The string "!php/const PHP_INT_MAX" could not be parsed as a constant.*#
*/
public function testParsePhpConstantThrowsExceptionOnInvalidType()
{
- Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
+ Inline::parse('!php/const PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
+ }
+
+ /**
+ * @group legacy
+ * @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.
+ * @dataProvider getTestsForParseLegacyPhpConstants
+ */
+ public function testDeprecatedConstantTag($yaml, $expectedValue)
+ {
+ $this->assertSame($expectedValue, Inline::parse($yaml, Yaml::PARSE_CONSTANT));
+ }
+
+ public function getTestsForParseLegacyPhpConstants()
+ {
+ return array(
+ array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
+ array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
+ array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
+ array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
+ array('!php/const:NULL', null),
+ );
}
/**
/**
* @dataProvider getTestsForDump
*/
- public function testDump($yaml, $value)
+ public function testDump($yaml, $value, $parseFlags = 0)
{
$this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
- $this->assertSame($value, Inline::parse(Inline::dump($value)), 'check consistency');
+ $this->assertSame($value, Inline::parse(Inline::dump($value), $parseFlags), 'check consistency');
}
public function testDumpNumericValueWithLocale()
/**
* @group legacy
- * @expectedDeprecation Using a colon after an unquoted mapping key that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}") is deprecated since version 3.2 and will throw a ParseException in 4.0.
+ * @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.
* throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
*/
public function testParseMappingKeyWithColonNotFollowedBySpace()
/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessage A reference must contain at least one character.
+ * @expectedExceptionMessage A reference must contain at least one character at line 1.
*/
public function testParseUnquotedAsterisk()
{
/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessage A reference must contain at least one character.
+ * @expectedExceptionMessage A reference must contain at least one character at line 1.
*/
public function testParseUnquotedAsteriskFollowedByAComment()
{
/**
* @dataProvider getReservedIndicators
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessage cannot start a plain scalar; you need to quote the scalar.
*/
public function testParseUnquotedScalarStartingWithReservedIndicator($indicator)
{
+ if (method_exists($this, 'expectExceptionMessage')) {
+ $this->expectException(ParseException::class);
+ $this->expectExceptionMessage(sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
+ } else {
+ $this->setExpectedException(ParseException::class, sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
+ }
+
Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
}
/**
* @dataProvider getScalarIndicators
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessage cannot start a plain scalar; you need to quote the scalar.
*/
public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
{
+ if (method_exists($this, 'expectExceptionMessage')) {
+ $this->expectException(ParseException::class);
+ $this->expectExceptionMessage(sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
+ } else {
+ $this->setExpectedException(ParseException::class, sprintf('cannot start a plain scalar; you need to quote the scalar at line 1 (near "%sfoo ").', $indicator));
+ }
+
Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
}
/**
* @group legacy
- * @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.
+ * @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.
* throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
*/
public function testParseUnquotedScalarStartingWithPercentCharacter()
array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
// mappings
- array('{foo: bar,bar: foo,false: false,null: null,integer: 12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
- array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
+ array('{foo: bar,bar: foo,"false": false, "null": null,integer: 12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
+ array('{ foo : bar, bar : foo, "false" : false, "null" : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
array('{foo: \'bar\', bar: \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
// mappings
- array('{foo: bar,bar: foo,false: false,null: null,integer: 12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
- array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
+ 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),
+ 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),
array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported}.
+ * @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported} at line 1.
*/
public function testNotSupportedMissingValue()
{
$this->assertEquals($longStringWithQuotes, $arrayFromYaml['longStringWithQuotes']);
}
+ /**
+ * @group legacy
+ * @expectedDeprecation Omitting the key of a mapping is deprecated and will throw a ParseException in 4.0 on line 1.
+ */
public function testOmittedMappingKeyIsParsedAsColon()
{
$this->assertSame(array(':' => 'foo'), Inline::parse('{: foo}'));
}
- public function testBooleanMappingKeysAreConvertedToStrings()
+ /**
+ * @dataProvider getTestsForNullValues
+ */
+ public function testParseMissingMappingValueAsNull($yaml, $expected)
{
- $this->assertSame(array('false' => 'foo'), Inline::parse('{false: foo}'));
- $this->assertSame(array('true' => 'foo'), Inline::parse('{true: foo}'));
+ $this->assertSame($expected, Inline::parse($yaml));
+ }
+
+ public function getTestsForNullValues()
+ {
+ return array(
+ 'null before closing curly brace' => array('{foo:}', array('foo' => null)),
+ 'null before comma' => array('{foo:, bar: baz}', array('foo' => null, 'bar' => 'baz')),
+ );
}
public function testTheEmptyStringIsAValidMappingKey()
{
$this->assertSame(array('' => 'foo'), Inline::parse('{ "": foo }'));
}
+
+ /**
+ * @group legacy
+ * @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.
+ * @dataProvider getNotPhpCompatibleMappingKeyData
+ */
+ public function testImplicitStringCastingOfMappingKeysIsDeprecated($yaml, $expected)
+ {
+ $this->assertSame($expected, Inline::parse($yaml));
+ }
+
+ /**
+ * @group legacy
+ * @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.
+ * @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.
+ * @dataProvider getNotPhpCompatibleMappingKeyData
+ */
+ public function testExplicitStringCastingOfMappingKeys($yaml, $expected)
+ {
+ $this->assertSame($expected, Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS));
+ }
+
+ public function getNotPhpCompatibleMappingKeyData()
+ {
+ return array(
+ 'boolean-true' => array('{true: "foo"}', array('true' => 'foo')),
+ 'boolean-false' => array('{false: "foo"}', array('false' => 'foo')),
+ 'null' => array('{null: "foo"}', array('null' => 'foo')),
+ 'float' => array('{0.25: "foo"}', array('0.25' => 'foo')),
+ );
+ }
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Support for the !str tag is deprecated since Symfony 3.4. Use the !!str tag instead on line 1.
+ */
+ public function testDeprecatedStrTag()
+ {
+ $this->assertSame(array('foo' => 'bar'), Inline::parse('{ foo: !str bar }'));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+ * @expectedExceptionMessage Unexpected end of line, expected one of ",}" at line 1 (near "{abc: 'def'").
+ */
+ public function testUnfinishedInlineMap()
+ {
+ Inline::parse("{abc: 'def'");
+ }
}