+ /**
+ * @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/'),
+ );
+ }
+