Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d597aad

Browse filesBrowse files
committed
✅ Test parsing invalid dates
1 parent c582b76 commit d597aad
Copy full SHA for d597aad

File tree

2 files changed

+15
-2
lines changed
Filter options

2 files changed

+15
-2
lines changed

‎src/Symfony/Component/Yaml/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,13 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
700700
case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
701701
return (float) str_replace('_', '', $scalar);
702702
case Parser::preg_match(self::getTimestampRegex(), $scalar):
703-
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
704-
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
703+
try {
704+
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
705+
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
706+
} catch (\Exception $e) {
707+
// Some dates accepted by the regex are not valid dates.
708+
throw new ParseException(\sprintf('The date "%s" could not be parsed as it is an invalid date.', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename, $e);
709+
}
705710

706711
if (Yaml::PARSE_DATETIME & $flags) {
707712
return $time;

‎src/Symfony/Component/Yaml/Tests/InlineTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/InlineTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,14 @@ public function testParseNestedTimestampListAsDateTimeObject(string $yaml, int $
579579
$this->assertEquals($expectedNested, Inline::parse($yamlNested, Yaml::PARSE_DATETIME));
580580
}
581581

582+
public function testParseInvalidDate()
583+
{
584+
$this->expectException(ParseException::class);
585+
$this->expectExceptionMessageMatches('/^The date "2024-50-50" could not be parsed as it is an invalid date.*/');
586+
587+
Inline::parse('2024-50-50', Yaml::PARSE_DATETIME);
588+
}
589+
582590
/**
583591
* @dataProvider getDateTimeDumpTests
584592
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.