View source
<?php
namespace Symfony\Component\Yaml\Tests;
use Symfony\Component\Yaml\Inline;
class InlineTest extends \PHPUnit_Framework_TestCase {
public function testParse() {
foreach ($this
->getTestsForParse() as $yaml => $value) {
$this
->assertSame($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
}
}
public function testDump() {
$testsForDump = $this
->getTestsForDump();
foreach ($testsForDump as $yaml => $value) {
$this
->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
}
foreach ($this
->getTestsForParse() as $value) {
$this
->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
}
foreach ($testsForDump as $value) {
$this
->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
}
}
public function testDumpNumericValueWithLocale() {
$locale = setlocale(LC_NUMERIC, 0);
if (false === $locale) {
$this
->markTestSkipped('Your platform does not support locales.');
}
try {
$requiredLocales = array(
'fr_FR.UTF-8',
'fr_FR.UTF8',
'fr_FR.utf-8',
'fr_FR.utf8',
'French_France.1252',
);
if (false === setlocale(LC_NUMERIC, $requiredLocales)) {
$this
->markTestSkipped('Could not set any of required locales: ' . implode(', ', $requiredLocales));
}
$this
->assertEquals('1.2', Inline::dump(1.2));
$this
->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
setlocale(LC_NUMERIC, $locale);
} catch (\Exception $e) {
setlocale(LC_NUMERIC, $locale);
throw $e;
}
}
public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF() {
$value = '686e444';
$this
->assertSame($value, Inline::parse(Inline::dump($value)));
}
public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException() {
$value = "'don't do somthin' like that'";
Inline::parse($value);
}
public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException() {
$value = '"don"t do somthin" like that"';
Inline::parse($value);
}
public function testParseInvalidMappingKeyShouldThrowException() {
$value = '{ "foo " bar": "bar" }';
Inline::parse($value);
}
public function testParseInvalidMappingShouldThrowException() {
Inline::parse('[foo] bar');
}
public function testParseInvalidSequenceShouldThrowException() {
Inline::parse('{ foo: bar } bar');
}
public function testParseScalarWithCorrectlyQuotedStringShouldReturnString() {
$value = "'don''t do somthin'' like that'";
$expect = "don't do somthin' like that";
$this
->assertSame($expect, Inline::parseScalar($value));
}
public function testParseReferences($yaml, $expected) {
$this
->assertSame($expected, Inline::parse($yaml, false, false, array(
'var' => 'var-value',
)));
}
public function getDataForParseReferences() {
return array(
'scalar' => array(
'*var',
'var-value',
),
'list' => array(
'[ *var ]',
array(
'var-value',
),
),
'list-in-list' => array(
'[[ *var ]]',
array(
array(
'var-value',
),
),
),
'map-in-list' => array(
'[ { key: *var } ]',
array(
array(
'key' => 'var-value',
),
),
),
'embedded-mapping-in-list' => array(
'[ key: *var ]',
array(
array(
'key' => 'var-value',
),
),
),
'map' => array(
'{ key: *var }',
array(
'key' => 'var-value',
),
),
'list-in-map' => array(
'{ key: [*var] }',
array(
'key' => array(
'var-value',
),
),
),
'map-in-map' => array(
'{ foo: { bar: *var } }',
array(
'foo' => array(
'bar' => 'var-value',
),
),
),
);
}
public function testParseMapReferenceInSequence() {
$foo = array(
'a' => 'Steve',
'b' => 'Clark',
'c' => 'Brian',
);
$this
->assertSame(array(
$foo,
), Inline::parse('[*foo]', false, false, array(
'foo' => $foo,
)));
}
public function testParseUnquotedAsterisk() {
Inline::parse('{ foo: * }');
}
public function testParseUnquotedAsteriskFollowedByAComment() {
Inline::parse('{ foo: * #foo }');
}
public function testIsHash($array, $expected) {
$this
->assertSame($expected, Inline::isHash($array));
}
public function getDataForIsHash() {
return array(
array(
array(),
false,
),
array(
array(
1,
2,
3,
),
false,
),
array(
array(
2 => 1,
1 => 2,
0 => 3,
),
true,
),
array(
array(
'foo' => 1,
'bar' => 2,
),
true,
),
);
}
protected function getTestsForParse() {
return array(
'' => '',
'null' => null,
'false' => false,
'true' => true,
'12' => 12,
'-12' => -12,
'"quoted string"' => 'quoted string',
"'quoted string'" => 'quoted string',
'12.30e+02' => 1230.0,
'0x4D2' => 0x4d2,
'02333' => 02333,
'.Inf' => -log(0),
'-.Inf' => log(0),
"'686e444'" => '686e444',
'686e444' => \INF,
'123456789123456789123456789123456789' => '123456789123456789123456789123456789',
'"foo\\r\\nbar"' => "foo\r\nbar",
"'foo#bar'" => 'foo#bar',
"'foo # bar'" => 'foo # bar',
"'#cfcfcf'" => '#cfcfcf',
'::form_base.html.twig' => '::form_base.html.twig',
"'y'" => 'y',
"'n'" => 'n',
"'yes'" => 'yes',
"'no'" => 'no',
"'on'" => 'on',
"'off'" => 'off',
'2007-10-30' => gmmktime(0, 0, 0, 10, 30, 2007),
'2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
'2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
'1960-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 1960),
'1730-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 1730),
'"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
"'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
'[foo, http://urls.are/no/mappings, false, null, 12]' => array(
'foo',
'http://urls.are/no/mappings',
false,
null,
12,
),
'[ foo , bar , false , null , 12 ]' => array(
'foo',
'bar',
false,
null,
12,
),
'[\'foo,bar\', \'foo bar\']' => array(
'foo,bar',
'foo bar',
),
'{foo:bar,bar:foo,false:false,null:null,integer:12}' => array(
'foo' => 'bar',
'bar' => 'foo',
'false' => false,
'null' => null,
'integer' => 12,
),
'{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array(
'foo' => 'bar',
'bar' => 'foo',
'false' => false,
'null' => null,
'integer' => 12,
),
'{foo: \'bar\', bar: \'foo: bar\'}' => array(
'foo' => 'bar',
'bar' => 'foo: bar',
),
'{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array(
'foo' => 'bar',
'bar' => 'foo: bar',
),
'{\'foo\'\'\': \'bar\', "bar\\"": \'foo: bar\'}' => array(
'foo\'' => 'bar',
'bar"' => 'foo: bar',
),
'{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array(
'foo: ' => 'bar',
'bar: ' => 'foo: bar',
),
'[foo, [bar, foo]]' => array(
'foo',
array(
'bar',
'foo',
),
),
'[foo, {bar: foo}]' => array(
'foo',
array(
'bar' => 'foo',
),
),
'{ foo: {bar: foo} }' => array(
'foo' => array(
'bar' => 'foo',
),
),
'{ foo: [bar, foo] }' => array(
'foo' => array(
'bar',
'foo',
),
),
'[ foo, [ bar, foo ] ]' => array(
'foo',
array(
'bar',
'foo',
),
),
'[{ foo: {bar: foo} }]' => array(
array(
'foo' => array(
'bar' => 'foo',
),
),
),
'[foo, [bar, [foo, [bar, foo]], foo]]' => array(
'foo',
array(
'bar',
array(
'foo',
array(
'bar',
'foo',
),
),
'foo',
),
),
'[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',
),
),
),
'[foo, bar: { foo: bar }]' => array(
'foo',
'1' => array(
'bar' => array(
'foo' => 'bar',
),
),
),
'[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',
),
);
}
protected function getTestsForDump() {
return array(
'null' => null,
'false' => false,
'true' => true,
'12' => 12,
"'quoted string'" => 'quoted string',
'12.30e+02' => 1230.0,
'1234' => 0x4d2,
'1243' => 02333,
'-.Inf' => log(0),
"'686e444'" => '686e444',
'.Inf' => \INF,
'"foo\\r\\nbar"' => "foo\r\nbar",
"'foo#bar'" => 'foo#bar',
"'foo # bar'" => 'foo # bar',
"'#cfcfcf'" => '#cfcfcf',
"'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
"'-dash'" => '-dash',
"'-'" => '-',
"'y'" => 'y',
"'n'" => 'n',
"'yes'" => 'yes',
"'no'" => 'no',
"'on'" => 'on',
"'off'" => 'off',
'[foo, bar, false, null, 12]' => array(
'foo',
'bar',
false,
null,
12,
),
'[\'foo,bar\', \'foo bar\']' => array(
'foo,bar',
'foo bar',
),
'{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array(
'foo' => 'bar',
'bar' => 'foo',
'false' => false,
'null' => null,
'integer' => 12,
),
'{ foo: bar, bar: \'foo: bar\' }' => array(
'foo' => 'bar',
'bar' => 'foo: bar',
),
'[foo, [bar, foo]]' => array(
'foo',
array(
'bar',
'foo',
),
),
'[foo, [bar, [foo, [bar, foo]], foo]]' => array(
'foo',
array(
'bar',
array(
'foo',
array(
'bar',
'foo',
),
),
'foo',
),
),
'{ foo: { bar: foo } }' => array(
'foo' => array(
'bar' => 'foo',
),
),
'[foo, { bar: foo }]' => array(
'foo',
array(
'bar' => 'foo',
),
),
'[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',
),
),
),
'[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',
),
'{ foo: { bar: { 1: 2, baz: 3 } } }' => array(
'foo' => array(
'bar' => array(
1 => 2,
'baz' => 3,
),
),
),
);
}
}