Skip to content

Commit a380084

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: fix merge [FrameworkBundle] Remove extra argument from ContainerBuilder::willBeAvailable call fix ext-redis 6.2.0 compatibility [CssSelector] Fix incorrect return type for Token::getType() [Workflow] State contamination due to class-based setter cache fix: fixed State contamination in marking stores due to class-based getter cache [Validator] Fix call to undefined getParser() in YamlValidator [ObjectMapper] Update Map attribute PHPDoc to match TransformCallableInterface signature fix tests Fix contentId assignment for inline attachments [HtmlSanitizer] Remove `srcdoc` from allowed attributes [EventDispatcher][FrameworkBundle] Rework union types on `#[AsEventListener]`
2 parents 8cbfeca + 865f7d1 commit a380084

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ public function testInvalidFlags()
6868
->assertRaised();
6969
}
7070

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

0 commit comments

Comments
 (0)