Skip to content

Commit 744b7d2

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: prevent the installation of unstable phpdocumentor/type-resolver releases chore: fix typo 'do' verb [Twig Bridge] Add PHPdoc information to some email methods Amend MoneyType twig to include a space. Convert previously defined date attribute to the expected class [WebProfilerBundle] Remove redundant code from logger template
2 parents c5281e6 + 5594256 commit 744b7d2

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

Controller/ArgumentResolver/DateTimeValueResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
4343
}
4444

4545
$value = $request->attributes->get($argument->getName());
46+
$class = \DateTimeInterface::class === $argument->getType() ? \DateTimeImmutable::class : $argument->getType();
4647

4748
if ($value instanceof \DateTimeInterface) {
48-
return [$value];
49+
return [$value instanceof $class ? $value : $class::createFromInterface($value)];
4950
}
5051

5152
if ($argument->isNullable() && !$value) {
5253
return [null];
5354
}
5455

55-
$class = \DateTimeInterface::class === $argument->getType() ? \DateTimeImmutable::class : $argument->getType();
5656
$format = null;
5757

5858
if ($attributes = $argument->getAttributes(MapDateTime::class, ArgumentMetadata::IS_INSTANCEOF)) {

Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class DateTimeValueResolverTest extends TestCase
2222
{
23-
private $defaultTimezone;
23+
private readonly string $defaultTimezone;
2424

2525
protected function setUp(): void
2626
{
@@ -32,13 +32,20 @@ protected function tearDown(): void
3232
date_default_timezone_set($this->defaultTimezone);
3333
}
3434

35-
public function getTimeZones()
35+
public static function getTimeZones()
3636
{
3737
yield ['UTC'];
3838
yield ['Etc/GMT+9'];
3939
yield ['Etc/GMT-14'];
4040
}
4141

42+
public static function getClasses()
43+
{
44+
yield [\DateTimeInterface::class];
45+
yield [\DateTime::class];
46+
yield [FooDateTime::class];
47+
}
48+
4249
/**
4350
* @group legacy
4451
*/
@@ -137,17 +144,23 @@ public function testNow(string $timezone)
137144
$this->assertSame($timezone, $results[0]->getTimezone()->getName(), 'Default timezone');
138145
}
139146

140-
public function testPreviouslyConvertedAttribute()
147+
/**
148+
* @param class-string<\DateTimeInterface> $class
149+
*
150+
* @dataProvider getClasses
151+
*/
152+
public function testPreviouslyConvertedAttribute(string $class)
141153
{
142154
$resolver = new DateTimeValueResolver();
143155

144-
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null, true);
156+
$argument = new ArgumentMetadata('dummy', $class, false, false, null, true);
145157
$request = self::requestWithAttributes(['dummy' => $datetime = new \DateTimeImmutable()]);
146158

147159
$results = $resolver->resolve($request, $argument);
148160

149161
$this->assertCount(1, $results);
150-
$this->assertSame($datetime, $results[0]);
162+
$this->assertEquals($datetime, $results[0], 'The value is the same, but the class can be modified.');
163+
$this->assertInstanceOf($class, $results[0]);
151164
}
152165

153166
public function testCustomClass()
@@ -205,7 +218,7 @@ public function testWithFormat(string $timezone)
205218
$this->assertEquals('2016-09-08 12:34:56', $results[0]->format('Y-m-d H:i:s'));
206219
}
207220

208-
public function provideInvalidDates()
221+
public static function provideInvalidDates()
209222
{
210223
return [
211224
'invalid date' => [

0 commit comments

Comments
 (0)