8000 Merge branch '5.4' into 6.2 · symfony/symfony@020b689 · GitHub
[go: up one dir, main page]

Skip to content

Commit 020b689

Browse files
committed
Merge branch '5.4' into 6.2
* 5.4: relax assertions for ICU 72.1 [Validator] Fix regression with class metadatada on parent classes [VarExporter] Fix exporting classes with __serialize() but not __unserialize() [HttpFoundation][HttpKernel] Fix deprecations when `Content-Type` is `null`
2 parents a50480f + 75d6ba0 commit 020b689

File tree

16 files changed

+124
-41
lines changed

16 files changed

+124
-41
lines changed

src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
105105
if (isset($mapping['originalClass']) && !str_contains($mapping['declaredField'], '.')) {
106106
$metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
107107
$loaded = true;
108-
} elseif (property_exists($className, $mapping['fieldName'])) {
108+
} elseif (property_exists($className, $mapping['fieldName']) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty($mapping['fieldName'])->isPrivate())) {
109109
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
110110
$loaded = true;
111111
}

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testTransformToDifferentLocale()
124124

125125
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
126126

127-
$this->assertEquals('Feb 3, 2010, 4:05 AM', $transformer->transform($this->dateTime));
127+
$this->assertMatchesRegularExpression('/^Feb 3, 2010, 4:05\s+AM$/u', $transformer->transform($this->dateTime));
128128
}
129129

130130
public function testTransformEmpty()

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ public function testSingleTextWidgetWithCustomNonHtml5Format()
530530
]);
531531
$view = $form->createView();
532532

533-
$this->assertSame('2/13/19, 7:12:13 PM', $view->vars['value']);
533+
$this->assertMatchesRegularExpression('#^2/13/19, 7:12:13\s+PM$#u', $view->vars['value']);
534534
}
535535

536536
public function testDateTypeChoiceErrorsBubbleUp()

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public function prepare(Request $request): static
281281
$charset = $this->charset ?: 'UTF-8';
282282
if (!$headers->has('Content-Type')) {
283283
$headers->set('Content-Type', 'text/html; charset='.$charset);
284-
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
284+
} elseif (0 === stripos($headers->get('Content-Type') ?? '', 'text/') && false === stripos($headers->get('Content-Type') ?? '', 'charset')) {
285285
// add the charset
286286
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
287287
}

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,16 @@ public function testContentTypeCharset()
537537
$this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
538538
}
539539

540+
public function testContentTypeIsNull()
541+
{
542+
$response = new Response('foo');
543+
$response->headers->set('Content-Type', null);
544+
545+
$response->prepare(new Request());
546+
547+
$this->expectNotToPerformAssertions();
548+
}
549+
540550
public function testPrepareDoesNothingIfContentTypeIsSet()
541551
{
542552
$response = new Response('foo');

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ public function collect(Request $request, Response $response, \Throwable $except
108108
if (!$this->requestStack
109109
|| !$response->headers->has('X-Debug-Token')
110110
|| $response->isRedirection()
111-
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type'), 'html'))
111+
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type') ?? '', 'html'))
112112
|| 'html' !== $request->getRequestFormat()
113113
|| false === strripos($response->getContent(), '</body>')
114114
) {
115-
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type'), 'html')) {
115+
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type') ?? '', 'html')) {
116116
$dumper = new HtmlDumper('php://output', $this->charset);
117117
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
118118
} else {

src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 19 additions & 0 deletions
174
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\RequestStack;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
1819
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
@@ -156,4 +157,22 @@ public function testFlushNothingWhenDataDumperIsProvided()
156157
$collector->__destruct();
157158
$this->assertEmpty(ob_get_clean());
158159
}
160+
161+
public function testNullContentTypeWithNoDebugEnv()
162+
{
163+
$request = new Request();
164+
$requestStack = new RequestStack();
165+
$requestStack->push($request);
166+
167+
$response = new Response('<html><head></head><body></body></html>');
168+
$response->headers->set('Content-Type', null);
169+
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
170+
171+
$collector = new DumpDataCollector(null, null, null, $requestStack);
172+
$collector->collect($request, $response);
173+
+
ob_start();
175+
$collector->__destruct();
176+
$this->assertEmpty(ob_get_clean());
177+
}
159178
}

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,11 @@ public function mergeConstraints(self $source)
342342

343343
if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) {
344344
$property = $member->getPropertyName();
345-
$this->members[$property] = [$member];
345+
$this->members[$property][] = $member;
346346

347-
if ($member instanceof PropertyMetadata) {
347+
if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
348348
$this->properties[$property] = $member;
349-
} elseif ($member instanceof GetterMetadata) {
349+
} elseif ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
350350
$this->getters[$property] = $member;
351351
}
352352
} else {

src/Symfony/Component/Validator/Tests/Fixtures/Annotation/Entity.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class Entity extends EntityParent implements EntityInterfaceB
5555
private $internal;
5656
public $data = 'Overridden data';
5757
public $initialized = false;
58+
/**
59+
* @Assert\Type("integer")
60+
*/
61+
protected $other;
5862

5963
public function __construct($internal = null)
6064
{

src/Symfony/Component/Validator/Tests/Fixtures/Attribute/Entity.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Entity extends EntityParent implements EntityInterfaceB
5757
private $internal;
5858
public $data = 'Overridden data';
5959
public $initialized = false;
60+
#[Assert\Type('integer')]
61+
protected $other;
6062

6163
public function __construct($internal = null)
6264
{

src/Symfony/Component/Validator/Tests/Fixtures/NestedAttribute/Entity.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class Entity extends EntityParent implements EntityInterfaceB
7878
private $internal;
7979
public $data = 'Overridden data';
8080
public $initialized = false;
81+
#[Assert\Type('integer')]
82+
protected $other;
8183

8284
public function __construct($internal = null)
8385
{

src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ public function testMergeConstraintsMergesMemberConstraints()
163163
$parent->addPropertyConstraint('firstName', new ConstraintA());
164164
$parent->addPropertyConstraint('firstName', new ConstraintB(['groups' => 'foo']));
165165

166-
$this->metadata->mergeConstraints($parent);
167166
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
167+
$this->metadata->mergeConstraints($parent);
168168

169169
$constraintA1 = new ConstraintA(['groups' => [
170170
'Default',
@@ -179,35 +179,29 @@ public function testMergeConstraintsMergesMemberConstraints()
179179
'groups' => ['foo'],
180180
]);
181181

182-
$constraints = [
183-
$constraintA1,
184-
$constraintB,
185-
$constraintA2,
186-
];
182+
$members = $this->metadata->getPropertyMetadata('firstName');
187183

188-
$constraintsByGroup = [
189-
'Default' => [
190-
$constraintA1,
191-
$constraintA2,
192-
],
193-
'EntityParent' => [
194-
$constraintA1,
195-
],
196-
'Entity' => [
197-
$constraintA1,
198-
$constraintA2,
184+
$this->assertCount(2, $members);
185+
$this->assertEquals(self::CLASSNAME, $members[0]->getClassName());
186+
$this->assertEquals([$constraintA2], $members[0]->getConstraints());
187+
$this->assertEquals(
188+
[
189+
'Default' => [$constraintA2],
190+
'Entity' => [$constraintA2],
199191
],
200-
'foo' => [
201-
$constraintB,
192+
$members[0]->constraintsByGroup
193+
);
194+
$this->assertEquals(self::PARENTCLASS, $members[1]->getClassName());
195+
$this->assertEquals([$constraintA1, $constraintB], $members[1]->getConstraints());
196+
$this->assertEquals(
197+
[
198+
'Default' => [$constraintA1],
199+
'Entity' => [$constraintA1],
200+
'EntityParent' => [$constraintA1],
201+
'foo' => [$constraintB],
202202
],
203-
];
204-
205-
$members = $this->metadata->getPropertyMetadata('firstName');
206-
207-
$this->assertCount(1, $members);
208-
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
209-
$this->assertEquals($constraints, $members[0]->getConstraints());
210-
$this->assertEquals($constraintsByGroup, $members[0]->constraintsByGroup);
203+
$members[1]->constraintsByGroup
204+
);
211205
}
212206

213207
public function testMemberMetadatas()

src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Validator\Constraints\Range;
2828
use Symfony\Component\Validator\Constraints\Required;
2929
use Symfony\Component\Validator\Constraints\Sequentially;
30+
use Symfony\Component\Validator\Constraints\Type;
3031
use Symfony\Component\Validator\Constraints\Valid;
3132
use Symfony\Component\Validator\Mapping\ClassMetadata;
3233
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
@@ -98,6 +99,7 @@ public function testLoadClassMetadata(string $namespace)
9899
$expected->addGetterConstraint('lastName', new NotNull());
99100
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
100101
$expected->addGetterConstraint('permissions', new IsTrue());
102+
$expected->addPropertyConstraint('other', new Type('integer'));
101103

102104
// load reflection class so that the comparison passes
103105
$expected->getReflectionClass();
@@ -139,18 +141,16 @@ public function testLoadClassMetadataAndMerge(string $namespace)
139141
$loader->loadClassMetadata($parent_metadata);
140142

141143
$metadata = new ClassMetadata($namespace.'\Entity');
144+
$loader->loadClassMetadata($metadata);
142145

143146
// Merge parent metaData.
144147
$metadata->mergeConstraints($parent_metadata);
145148

146-
$loader->loadClassMetadata($metadata);
147-
148149
$expected_parent = new ClassMetadata($namespace.'\EntityParent');
149150
$expected_parent->addPropertyConstraint('other', new NotNull());
150151
$expected_parent->getReflectionClass();
151152

152153
$expected = new ClassMetadata($namespace.'\Entity');
153-
$expected->mergeConstraints($expected_parent);
154154

155155
$expected->setGroupSequence(['Foo', 'Entity']);
156156
$expected->addConstraint(new ConstraintA());
@@ -187,11 +187,18 @@ public function testLoadClassMetadataAndMerge(string $namespace)
187187
$expected->addGetterConstraint('lastName', new NotNull());
188188
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
189189
$expected->addGetterConstraint('permissions', new IsTrue());
190+
$expected->addPropertyConstraint('other', new Type('integer'));
190191

191192
// load reflection class so that the comparison passes
192193
$expected->getReflectionClass();
194+
$expected->mergeConstraints($expected_parent);
193195

194196
$this->assertEquals($expected, $metadata);
197+
198+
$otherMetadata = $metadata->getPropertyMetadata('other');
199+
$this->assertCount(2, $otherMetadata);
200+
$this->assertInstanceOf(Type::class, $otherMetadata[0]->getConstraints()[0]);
201+
$this->assertInstanceOf(NotNull::class, $otherMetadata[1]->getConstraints()[0]);
195202
}
196203

197204
/**

src/Symfony/Component/VarExporter/Internal/Exporter.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,29 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
7575

7676
$class = $value::class;
7777
$reflector = Registry::$reflectors[$class] ??= Registry::getClassReflector($class);
78+
$properties = [];
7879

7980
if ($reflector->hasMethod('__serialize')) {
8081
if (!$reflector->getMethod('__serialize')->isPublic()) {
8182
throw new \Error(sprintf('Call to %s method "%s::__serialize()".', $reflector->getMethod('__serialize')->isProtected() ? 'protected' : 'private', $class));
8283
}
8384

84-
if (!\is_array($properties = $value->__serialize())) {
85+
if (!\is_array($serializeProperties = $value->__serialize())) {
8586
throw new \TypeError($class.'::__serialize() must return an array');
8687
}
8788

89+
if ($reflector->hasMethod('__unserialize')) {
90+
$properties = $serializeProperties;
91+
} else {
92+
foreach ($serializeProperties as $n => $v) {
93+
$c = \PHP_VERSION_ID >= 80100 && $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass';
94+
$properties[$c][$n] = $v;
95+
}
96+
}
97+
8898
goto prepare_value;
8999
}
90100

91-
$properties = [];
92101
$sleep = null;
93102
$proto = Registry::$prototypes[$class];
94103

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
4+
$o = [
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\__SerializeButNo__Unserialize'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\__SerializeButNo__Unserialize')),
6+
],
7+
null,
8+
[
9+
'stdClass' => [
10+
'foo' => [
11+
'ccc',
12+
],
13+
],
14+
],
15+
$o[0],
16+
[]
17+
);

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ public static function provideExport()
235235

236236
yield ['__unserialize-but-no-__serialize', new __UnserializeButNo__Serialize()];
237237

238+
yield ['__serialize-but-no-__unserialize', new __SerializeButNo__Unserialize()];
239+
238240
yield ['unit-enum', [FooUnitEnum::Bar], true];
239241
yield ['readonly', new FooReadonly('k', 'v')];
240242
}
@@ -454,3 +456,20 @@ public function __unserialize(array $data): void
454456
$this->foo = $data['foo'];
455457
}
456458
}
459+
460+
class __SerializeButNo__Unserialize
461+
{
462+
public $foo;
463+
464+
public function __construct()
465+
{
466+
$this->foo = 'ccc';
467+
}
468+
469+
public function __serialize(): array
470+
{
471+
return [
472+
'foo' => $this->foo,
473+
];
474+
}
475+
}

0 commit comments

Comments
 (0)
0