Skip to content

Commit 9de4a4a

Browse files
committed
Merge branch '7.3' into 7.4
* 7.3: [FrameworkBundle] Remove extra argument from ContainerBuilder::willBeAvailable call fix ext-redis 6.2.0 compatibility [CssSelector] Fix incorrect return type for Token::getType() [Validator] Fix call to undefined getParser() in YamlValidator [ObjectMapper] Update Map attribute PHPDoc to match TransformCallableInterface signature [HtmlSanitizer] Remove `srcdoc` from allowed attributes
2 parents ec65739 + 5a2904d commit 9de4a4a

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

Constraints/YamlValidator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ public function validate(mixed $value, Constraint $constraint): void
3939

4040
$value = (string) $value;
4141

42+
$parser = new Parser();
43+
4244
/** @see \Symfony\Component\Yaml\Command\LintCommand::validate() */
43-
$prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) {
45+
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler, $parser) {
4446
if (\E_USER_DEPRECATED === $level) {
45-
throw new ParseException($message, $this->getParser()->getRealCurrentLineNb() + 1);
47+
throw new ParseException($message, $parser->getRealCurrentLineNb() + 1);
4648
}
4749

4850
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
4951
});
5052

5153
try {
52-
(new Parser())->parse($value, $constraint->flags);
54+
$parser->parse($value, $constraint->flags);
5355
} catch (ParseException $e) {
5456
$this->context->buildViolation($constraint->message)
5557
->setParameter('{{ error }}', $e->getMessage())

Tests/Constraints/YamlValidatorTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ public function testInvalidFlags()
6868
->assertRaised();
6969
}
7070

71+
/**
72+
* @dataProvider getDeprecationOnLinesData
73+
*/
74+
public function testDeprecationTriggersParseException(int $yamlLine, string $yamlValue)
75+
{
76+
$lines = explode("\n", $yamlValue);
77+
$errorLine = end($lines);
78+
$expectedError = 'This is a simulated deprecation at line '.$yamlLine.' (near "'.$errorLine.'")';
79+
80+
$constraint = new Yaml(
81+
message: 'myMessageTest',
82+
flags: YamlParser::PARSE_OBJECT,
83+
);
84+
$this->validator->validate($yamlValue, $constraint);
85+
$this->buildViolation('myMessageTest')
86+
->setParameter('{{ error }}', $expectedError)
87+
->setParameter('{{ line }}', $yamlLine)
88+
->setCode(Yaml::INVALID_YAML_ERROR)
89+
->assertRaised();
90+
}
91+
7192
public static function getValidValues()
7293
{
7394
return [
@@ -91,4 +112,34 @@ public static function getInvalidValues(): array
91112
["key:\nvalue", 'Unable to parse at line 2 (near "value").', 2],
92113
];
93114
}
115+
116+
/**
117+
* @return array<string, array{0: int, 1: string}>
118+
*/
119+
public static function getDeprecationOnLinesData(): array
120+
{
121+
$serialized = serialize(new DeprecatedObjectFixture());
122+
123+
return [
124+
'deprecation at line 1' => [1, "object: !php/object '".$serialized."'"],
125+
'deprecation at line 2' => [2, "valid: yaml\nobject: !php/object '".$serialized."'"],
126+
'deprecation at line 5' => [5, "line1: value\nline2: value\nline3: value\nline4: value\nobject: !php/object '".$serialized."'"],
127+
];
128+
}
129+
}
130+
131+
/**
132+
* Fixture class for triggering deprecation during unserialize.
133+
*/
134+
class DeprecatedObjectFixture
135+
{
136+
public function __serialize(): array
137+
{
138+
return [];
139+
}
140+
141+
public function __unserialize(array $data): void
142+
{
143+
@trigger_error('This is a simulated deprecation', \E_USER_DEPRECATED);
144+
}
94145
}

0 commit comments

Comments
 (0)