2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-03-18 07:48:03 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Yaml\Tests ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\Yaml\Inline ;
2010-03-18 07:48:03 +00:00
class InlineTest extends \PHPUnit_Framework_TestCase
{
2011-06-14 11:19:35 +01:00
public function testParse ()
2010-03-18 07:48:03 +00:00
{
2011-06-14 11:19:35 +01:00
foreach ( $this -> getTestsForParse () as $yaml => $value ) {
2013-01-17 23:32:48 +00:00
$this -> assertSame ( $value , Inline :: parse ( $yaml ), sprintf ( '::parse() converts an inline YAML to a PHP structure (%s)' , $yaml ));
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testDump ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$testsForDump = $this -> getTestsForDump ();
2010-05-07 15:09:11 +01:00
foreach ( $testsForDump as $yaml => $value ) {
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( $yaml , Inline :: dump ( $value ), sprintf ( '::dump() converts a PHP structure to an inline YAML (%s)' , $yaml ));
}
2013-10-10 23:23:27 +01:00
foreach ( $this -> getTestsForParse () as $value ) {
2011-06-14 11:19:35 +01:00
$this -> assertEquals ( $value , Inline :: parse ( Inline :: dump ( $value )), 'check consistency' );
2010-05-06 12:25:53 +01:00
}
2013-10-10 23:23:27 +01:00
foreach ( $testsForDump as $value ) {
2011-06-14 11:19:35 +01:00
$this -> assertEquals ( $value , Inline :: parse ( Inline :: dump ( $value )), 'check consistency' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
2011-11-04 07:17:10 +00:00
public function testDumpNumericValueWithLocale ()
{
$locale = setlocale ( LC_NUMERIC , 0 );
if ( false === $locale ) {
$this -> markTestSkipped ( 'Your platform does not support locales.' );
}
2011-11-29 20:13:34 +00:00
$required_locales = array ( 'fr_FR.UTF-8' , 'fr_FR.UTF8' , 'fr_FR.utf-8' , 'fr_FR.utf8' , 'French_France.1252' );
if ( false === setlocale ( LC_ALL , $required_locales )) {
2013-04-02 10:39:57 +01:00
$this -> markTestSkipped ( 'Could not set any of required locales: ' . implode ( " , " , $required_locales ));
2011-11-29 20:13:34 +00:00
}
2011-11-04 07:17:10 +00:00
$this -> assertEquals ( '1.2' , Inline :: dump ( 1.2 ));
2011-11-10 11:50:53 +00:00
$this -> assertContains ( 'fr' , strtolower ( setlocale ( LC_NUMERIC , 0 )));
2011-11-04 07:17:10 +00:00
setlocale ( LC_ALL , $locale );
}
2011-07-13 21:58:57 +01:00
public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF ()
{
$value = '686e444' ;
$this -> assertSame ( $value , Inline :: parse ( Inline :: dump ( $value )));
}
2012-04-20 16:15:12 +01:00
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException ()
{
$value = " 'don't do somthin' like that' " ;
2012-06-13 13:57:32 +01:00
Inline :: parse ( $value );
2012-04-20 16:15:12 +01:00
}
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException ()
{
$value = '"don"t do somthin" like that"' ;
2012-06-13 13:57:32 +01:00
Inline :: parse ( $value );
}
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseInvalidMappingKeyShouldThrowException ()
{
$value = '{ "foo " bar": "bar" }' ;
Inline :: parse ( $value );
2012-04-20 16:15:12 +01:00
}
2012-06-13 13:57:32 +01:00
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseInvalidMappingShouldThrowException ()
{
Inline :: parse ( '[foo] bar' );
}
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseInvalidSequenceShouldThrowException ()
{
Inline :: parse ( '{ foo: bar } bar' );
}
2012-04-20 16:15:12 +01:00
public function testParseScalarWithCorrectlyQuotedStringShouldReturnString ()
{
$value = " 'don''t do somthin'' like that' " ;
$expect = " don't do somthin' like that " ;
$this -> assertSame ( $expect , Inline :: parseScalar ( $value ));
}
2014-08-15 13:16:41 +01:00
/**
* @ dataProvider getDataForParseReferences
*/
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 )));
}
2014-09-03 18:52:14 +01:00
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
* @ expectedExceptionMessage A reference must contain at least one character .
*/
public function testParseUnquotedAsterisk ()
{
Inline :: parse ( '{ foo: * }' );
}
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
* @ expectedExceptionMessage A reference must contain at least one character .
*/
public function testParseUnquotedAsteriskFollowedByAComment ()
{
Inline :: parse ( '{ foo: * #foo }' );
}
2011-06-14 11:19:35 +01:00
protected function getTestsForParse ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
return array (
'' => '' ,
'null' => null ,
'false' => false ,
'true' => true ,
'12' => 12 ,
2013-01-17 23:32:48 +00:00
'-12' => - 12 ,
2010-05-06 12:25:53 +01:00
'"quoted string"' => 'quoted string' ,
" 'quoted string' " => 'quoted string' ,
'12.30e+02' => 12.30e+02 ,
'0x4D2' => 0x4D2 ,
'02333' => 02333 ,
'.Inf' => - log ( 0 ),
'-.Inf' => log ( 0 ),
2011-07-13 21:58:57 +01:00
" '686e444' " => '686e444' ,
'686e444' => 646e444 ,
2013-01-17 23:32:48 +00:00
'123456789123456789123456789123456789' => '123456789123456789123456789123456789' ,
2010-05-06 12:25:53 +01:00
'"foo\r\nbar"' => " foo \r \n bar " ,
" 'foo#bar' " => 'foo#bar' ,
" 'foo # bar' " => 'foo # bar' ,
" '#cfcfcf' " => '#cfcfcf' ,
2012-06-12 23:07:25 +01:00
'::form_base.html.twig' => '::form_base.html.twig' ,
2010-05-06 12:25:53 +01:00
'2007-10-30' => mktime ( 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 ),
2012-12-12 10:51:43 +00:00
'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 ),
2010-05-06 12:25:53 +01:00
'"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'' ,
" 'a \" string \" with ''quoted strings inside''' " => 'a "string" with \'quoted strings inside\'' ,
// sequences
// urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
'[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' ),
// mappings
'{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' ),
// nested sequences and mappings
'[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' ))),
2012-04-20 16:15:12 +01:00
'[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' ,),
2010-05-06 12:25:53 +01:00
);
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
protected function getTestsForDump ()
{
return array (
'null' => null ,
'false' => false ,
'true' => true ,
'12' => 12 ,
" 'quoted string' " => 'quoted string' ,
'12.30e+02' => 12.30e+02 ,
'1234' => 0x4D2 ,
'1243' => 02333 ,
'.Inf' => - log ( 0 ),
'-.Inf' => log ( 0 ),
2011-07-13 21:58:57 +01:00
" '686e444' " => '686e444' ,
'.Inf' => 646e444 ,
2010-05-06 12:25:53 +01:00
'"foo\r\nbar"' => " foo \r \n bar " ,
" 'foo#bar' " => 'foo#bar' ,
" 'foo # bar' " => 'foo # bar' ,
" '#cfcfcf' " => '#cfcfcf' ,
" 'a \" string \" with ''quoted strings inside''' " => 'a "string" with \'quoted strings inside\'' ,
2013-10-10 23:23:27 +01:00
" '-dash' " => '-dash' ,
" '-' " => '-' ,
2010-05-06 12:25:53 +01:00
// sequences
'[foo, bar, false, null, 12]' => array ( 'foo' , 'bar' , false , null , 12 ),
'[\'foo,bar\', \'foo bar\']' => array ( 'foo,bar' , 'foo bar' ),
// mappings
'{ 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' ),
// nested sequences and mappings
'[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' ))),
2012-04-20 16:15:12 +01:00
'[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' ,),
2010-05-06 12:25:53 +01:00
);
2010-03-18 07:48:03 +00:00
}
}