Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / yaml / Tests / InlineTest.php
index 7ca692dd1003565761cd8974eecf7032927ecfa2..8074123e6df75617f390b4b9d0229d1b05f5e7ca 100644 (file)
@@ -12,7 +12,9 @@
 namespace Symfony\Component\Yaml\Tests;
 
 use PHPUnit\Framework\TestCase;
+use Symfony\Component\Yaml\Exception\ParseException;
 use Symfony\Component\Yaml\Inline;
+use Symfony\Component\Yaml\Yaml;
 
 class InlineTest extends TestCase
 {
@@ -28,6 +30,55 @@ class InlineTest extends TestCase
      * @dataProvider getTestsForParseWithMapObjects
      */
     public function testParseWithMapObjects($yaml, $value)
+    {
+        $actual = Inline::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP);
+
+        $this->assertSame(serialize($value), serialize($actual));
+    }
+
+    /**
+     * @dataProvider getTestsForParsePhpConstants
+     */
+    public function testParsePhpConstants($yaml, $value)
+    {
+        $actual = Inline::parse($yaml, Yaml::PARSE_CONSTANT);
+
+        $this->assertSame($value, $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)),
+        );
+    }
+
+    /**
+     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+     * @expectedExceptionMessage The constant "WRONG_CONSTANT" is not defined
+     */
+    public function testParsePhpConstantThrowsExceptionWhenUndefined()
+    {
+        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.*#
+     */
+    public function testParsePhpConstantThrowsExceptionOnInvalidType()
+    {
+        Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
+    }
+
+    /**
+     * @group legacy
+     * @dataProvider getTestsForParseWithMapObjects
+     */
+    public function testParseWithMapObjectsPassingTrue($yaml, $value)
     {
         $actual = Inline::parse($yaml, false, false, true);
 
@@ -59,10 +110,8 @@ class InlineTest extends TestCase
 
             $this->assertEquals('1.2', Inline::dump(1.2));
             $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
+        } finally {
             setlocale(LC_NUMERIC, $locale);
-        } catch (\Exception $e) {
-            setlocale(LC_NUMERIC, $locale);
-            throw $e;
         }
     }
 
@@ -74,12 +123,12 @@ class InlineTest extends TestCase
     }
 
     /**
-     * @group legacy
-     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
+     * @expectedException        \Symfony\Component\Yaml\Exception\ParseException
+     * @expectedExceptionMessage Found unknown escape character "\V".
      */
     public function testParseScalarWithNonEscapedBlackslashShouldThrowException()
     {
-        $this->assertSame('Foo\Var', Inline::parse('"Foo\Var"'));
+        Inline::parse('"Foo\Var"');
     }
 
     /**
@@ -117,6 +166,16 @@ class InlineTest extends TestCase
         Inline::parse($value);
     }
 
+    /**
+     * @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.
+     * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
+     */
+    public function testParseMappingKeyWithColonNotFollowedBySpace()
+    {
+        Inline::parse('{1:""}');
+    }
+
     /**
      * @expectedException \Symfony\Component\Yaml\Exception\ParseException
      */
@@ -145,6 +204,15 @@ class InlineTest extends TestCase
      * @dataProvider getDataForParseReferences
      */
     public function testParseReferences($yaml, $expected)
+    {
+        $this->assertSame($expected, Inline::parse($yaml, 0, array('var' => 'var-value')));
+    }
+
+    /**
+     * @group legacy
+     * @dataProvider getDataForParseReferences
+     */
+    public function testParseReferencesAsFifthArgument($yaml, $expected)
     {
         $this->assertSame($expected, Inline::parse($yaml, false, false, false, array('var' => 'var-value')));
     }
@@ -164,6 +232,19 @@ class InlineTest extends TestCase
     }
 
     public function testParseMapReferenceInSequence()
+    {
+        $foo = array(
+            'a' => 'Steve',
+            'b' => 'Clark',
+            'c' => 'Brian',
+        );
+        $this->assertSame(array($foo), Inline::parse('[*foo]', 0, array('foo' => $foo)));
+    }
+
+    /**
+     * @group legacy
+     */
+    public function testParseMapReferenceInSequenceAsFifthArgument()
     {
         $foo = array(
             'a' => 'Steve',
@@ -192,48 +273,43 @@ class InlineTest extends TestCase
     }
 
     /**
-     * @group legacy
-     * @expectedDeprecation Not quoting the scalar "@foo " starting with "@" is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.
-     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
+     * @dataProvider getReservedIndicators
+     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+     * @expectedExceptionMessage cannot start a plain scalar; you need to quote the scalar.
      */
-    public function testParseUnquotedScalarStartingWithReservedAtIndicator()
+    public function testParseUnquotedScalarStartingWithReservedIndicator($indicator)
     {
-        Inline::parse('{ foo: @foo }');
+        Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
     }
 
-    /**
-     * @group legacy
-     * @expectedDeprecation Not quoting the scalar "`foo " starting with "`" is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.
-     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
-     */
-    public function testParseUnquotedScalarStartingWithReservedBacktickIndicator()
+    public function getReservedIndicators()
     {
-        Inline::parse('{ foo: `foo }');
+        return array(array('@'), array('`'));
     }
 
     /**
-     * @group legacy
-     * @expectedDeprecation Not quoting the scalar "|foo " starting with "|" is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.
-     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
+     * @dataProvider getScalarIndicators
+     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+     * @expectedExceptionMessage cannot start a plain scalar; you need to quote the scalar.
      */
-    public function testParseUnquotedScalarStartingWithLiteralStyleIndicator()
+    public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
     {
-        Inline::parse('{ foo: |foo }');
+        Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
     }
 
-    /**
-     * @group legacy
-     * @expectedDeprecation Not quoting the scalar ">foo " starting with ">" is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.
-     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
-     */
-    public function testParseUnquotedScalarStartingWithFoldedStyleIndicator()
+    public function getScalarIndicators()
     {
-        Inline::parse('{ foo: >foo }');
+        return array(array('|'), array('>'));
     }
 
-    public function getScalarIndicators()
+    /**
+     * @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.
+     * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
+     */
+    public function testParseUnquotedScalarStartingWithPercentCharacter()
     {
-        return array(array('|'), array('>'));
+        Inline::parse('{ foo: %bar }');
     }
 
     /**
@@ -263,11 +339,17 @@ class InlineTest extends TestCase
             array('true', true),
             array('12', 12),
             array('-12', -12),
+            array('1_2', 12),
+            array('_12', '_12'),
+            array('12_', 12),
             array('"quoted string"', 'quoted string'),
             array("'quoted string'", 'quoted string'),
             array('12.30e+02', 12.30e+02),
+            array('123.45_67', 123.4567),
             array('0x4D2', 0x4D2),
+            array('0x_4_D_2_', 0x4D2),
             array('02333', 02333),
+            array('0_2_3_3_3', 02333),
             array('.Inf', -log(0)),
             array('-.Inf', log(0)),
             array("'686e444'", '686e444'),
@@ -303,18 +385,22 @@ class InlineTest extends TestCase
             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: 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\', "bar: ": \'foo: bar\'}', array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
+            array('{"foo:bar": "baz"}', array('foo:bar' => 'baz')),
+            array('{"foo":"bar"}', array('foo' => 'bar')),
 
             // nested sequences and mappings
             array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
             array('[foo, {bar: foo}]', array('foo', array('bar' => 'foo'))),
             array('{ foo: {bar: foo} }', array('foo' => array('bar' => 'foo'))),
             array('{ foo: [bar, foo] }', array('foo' => array('bar', 'foo'))),
+            array('{ foo:{bar: foo} }', array('foo' => array('bar' => 'foo'))),
+            array('{ foo:[bar, foo] }', array('foo' => array('bar', 'foo'))),
 
             array('[  foo, [  bar, foo  ]  ]', array('foo', array('bar', 'foo'))),
 
@@ -370,12 +456,14 @@ class InlineTest extends TestCase
             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)),
             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')),
             array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
+            array('{"foo:bar": "baz"}', (object) array('foo:bar' => 'baz')),
+            array('{"foo":"bar"}', (object) array('foo' => 'bar')),
 
             // nested sequences and mappings
             array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
@@ -415,10 +503,15 @@ class InlineTest extends TestCase
             array('false', false),
             array('true', true),
             array('12', 12),
+            array("'1_2'", '1_2'),
+            array('_12', '_12'),
+            array("'12_'", '12_'),
             array("'quoted string'", 'quoted string'),
             array('!!float 1230', 12.30e+02),
             array('1234', 0x4D2),
             array('1243', 02333),
+            array("'0x_4_D_2_'", '0x_4_D_2_'),
+            array("'0_2_3_3_3'", '0_2_3_3_3'),
             array('.Inf', -log(0)),
             array('-.Inf', log(0)),
             array("'686e444'", '686e444'),
@@ -465,6 +558,127 @@ class InlineTest extends TestCase
         );
     }
 
+    /**
+     * @dataProvider getTimestampTests
+     */
+    public function testParseTimestampAsUnixTimestampByDefault($yaml, $year, $month, $day, $hour, $minute, $second)
+    {
+        $this->assertSame(gmmktime($hour, $minute, $second, $month, $day, $year), Inline::parse($yaml));
+    }
+
+    /**
+     * @dataProvider getTimestampTests
+     */
+    public function testParseTimestampAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second, $timezone)
+    {
+        $expected = new \DateTime($yaml);
+        $expected->setTimeZone(new \DateTimeZone('UTC'));
+        $expected->setDate($year, $month, $day);
+
+        if (\PHP_VERSION_ID >= 70100) {
+            $expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
+        } else {
+            $expected->setTime($hour, $minute, $second);
+        }
+
+        $date = Inline::parse($yaml, Yaml::PARSE_DATETIME);
+        $this->assertEquals($expected, $date);
+        $this->assertSame($timezone, $date->format('O'));
+    }
+
+    public function getTimestampTests()
+    {
+        return array(
+            'canonical' => array('2001-12-15T02:59:43.1Z', 2001, 12, 15, 2, 59, 43.1, '+0000'),
+            'ISO-8601' => array('2001-12-15t21:59:43.10-05:00', 2001, 12, 16, 2, 59, 43.1, '-0500'),
+            'spaced' => array('2001-12-15 21:59:43.10 -5', 2001, 12, 16, 2, 59, 43.1, '-0500'),
+            'date' => array('2001-12-15', 2001, 12, 15, 0, 0, 0, '+0000'),
+        );
+    }
+
+    /**
+     * @dataProvider getTimestampTests
+     */
+    public function testParseNestedTimestampListAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second)
+    {
+        $expected = new \DateTime($yaml);
+        $expected->setTimeZone(new \DateTimeZone('UTC'));
+        $expected->setDate($year, $month, $day);
+        if (\PHP_VERSION_ID >= 70100) {
+            $expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
+        } else {
+            $expected->setTime($hour, $minute, $second);
+        }
+
+        $expectedNested = array('nested' => array($expected));
+        $yamlNested = "{nested: [$yaml]}";
+
+        $this->assertEquals($expectedNested, Inline::parse($yamlNested, Yaml::PARSE_DATETIME));
+    }
+
+    /**
+     * @dataProvider getDateTimeDumpTests
+     */
+    public function testDumpDateTime($dateTime, $expected)
+    {
+        $this->assertSame($expected, Inline::dump($dateTime));
+    }
+
+    public function getDateTimeDumpTests()
+    {
+        $tests = array();
+
+        $dateTime = new \DateTime('2001-12-15 21:59:43', new \DateTimeZone('UTC'));
+        $tests['date-time-utc'] = array($dateTime, '2001-12-15T21:59:43+00:00');
+
+        $dateTime = new \DateTimeImmutable('2001-07-15 21:59:43', new \DateTimeZone('Europe/Berlin'));
+        $tests['immutable-date-time-europe-berlin'] = array($dateTime, '2001-07-15T21:59:43+02:00');
+
+        return $tests;
+    }
+
+    /**
+     * @dataProvider getBinaryData
+     */
+    public function testParseBinaryData($data)
+    {
+        $this->assertSame('Hello world', Inline::parse($data));
+    }
+
+    public function getBinaryData()
+    {
+        return array(
+            'enclosed with double quotes' => array('!!binary "SGVsbG8gd29ybGQ="'),
+            'enclosed with single quotes' => array("!!binary 'SGVsbG8gd29ybGQ='"),
+            'containing spaces' => array('!!binary  "SGVs bG8gd 29ybGQ="'),
+        );
+    }
+
+    /**
+     * @dataProvider getInvalidBinaryData
+     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
+     */
+    public function testParseInvalidBinaryData($data, $expectedMessage)
+    {
+        if (method_exists($this, 'expectException')) {
+            $this->expectExceptionMessageRegExp($expectedMessage);
+        } else {
+            $this->setExpectedExceptionRegExp(ParseException::class, $expectedMessage);
+        }
+
+        Inline::parse($data);
+    }
+
+    public function getInvalidBinaryData()
+    {
+        return array(
+            '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\)/'),
+            'invalid characters' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
+            'too many equals characters' => array('!!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
+            'misplaced equals character' => array('!!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
+        );
+    }
+
     /**
      * @expectedException \Symfony\Component\Yaml\Exception\ParseException
      * @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported}.
@@ -484,6 +698,11 @@ class InlineTest extends TestCase
         $this->assertEquals($longStringWithQuotes, $arrayFromYaml['longStringWithQuotes']);
     }
 
+    public function testOmittedMappingKeyIsParsedAsColon()
+    {
+        $this->assertSame(array(':' => 'foo'), Inline::parse('{: foo}'));
+    }
+
     public function testBooleanMappingKeysAreConvertedToStrings()
     {
         $this->assertSame(array('false' => 'foo'), Inline::parse('{false: foo}'));