Skip to content

Commit eb150fe

Browse files
Merge branch '5.0' into 5.1
* 5.0: fix test Added Unit tests for php 8 union types.
2 parents 8df623e + 723be72 commit eb150fe

File tree

9 files changed

+149
-6
lines changed

9 files changed

+149
-6
lines changed

.github/patch-types.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/ParseError.php'):
2222
case false !== strpos($file, '/src/Symfony/Component/Debug/Tests/Fixtures/'):
2323
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Compiler/OptionalServiceClass.php'):
24+
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php'):
2425
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ParentNotExists.php'):
2526
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/BadClasses/MissingParent.php'):
2627
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/WitherStaticReturnType.php'):

src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ private function createConnectionMock()
8989
{
9090
$connection = $this->createMock(Connection::class);
9191
$driver = $this->createMock(AbstractMySQLDriver::class);
92-
$driver->expects($this->any())
93-
->method('getName')
94-
->willReturn('pdo_mysql');
9592
$connection->expects($this->any())
9693
->method('getDriver')
9794
->willReturn($driver);

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,25 @@ public function testTypeNotGuessableNoServicesFound()
236236
}
237237
}
238238

239+
/**
240+
* @requires PHP 8
241+
*/
242+
public function testTypeNotGuessableUnionType()
243+
{
244+
$this->expectException('Symfony\Component\DependencyInjection\Exception\AutowiringFailedException');
245+
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionA|Symfony\Component\DependencyInjection\Tests\Compiler\CollisionB" but this class was not found.');
246+
$container = new ContainerBuilder();
247+
248+
$container->register(CollisionA::class);
249+
$container->register(CollisionB::class);
250+
251+
$aDefinition = $container->register('a', UnionClasses::class);
252+
$aDefinition->setAutowired(true);
253+
254+
$pass = new AutowirePass();
255+
$pass->process($container);
256+
}
257+
239258
public function testTypeNotGuessableWithTypeSet()
240259
{
241260
$container = new ContainerBuilder();
@@ -319,6 +338,40 @@ public function testOptionalParameter()
319338
$this->assertEquals(Foo::class, $definition->getArgument(2));
320339
}
321340

341+
/**
342+
* @requires PHP 8
343+
*/
344+
public function testParameterWithNullUnionIsSkipped()
345+
{
346+
$container = new ContainerBuilder();
347+
348+
$optDefinition = $container->register('opt', UnionNull::class);
349+
$optDefinition->setAutowired(true);
350+
351+
(new AutowirePass())->process($container);
352+
353+
$definition = $container->getDefinition('opt');
354+
$this->assertNull($definition->getArgument(0));
355+
}
356+
357+
/**
358+
* @requires PHP 8
359+
*/
360+
public function testParameterWithNullUnionIsAutowired()
361+
{
362+
$container = new ContainerBuilder();
363+
364+
$container->register(CollisionInterface::class, CollisionA::class);
365+
366+
$optDefinition = $container->register('opt', UnionNull::class);
367+
$optDefinition->setAutowired(true);
368+
369+
(new AutowirePass())->process($container);
370+
371+
$definition = $container->getDefinition('opt');
372+
$this->assertEquals(CollisionInterface::class, $definition->getArgument(0));
373+
}
374+
322375
public function testDontTriggerAutowiring()
323376
{
324377
$container = new ContainerBuilder();
@@ -435,6 +488,21 @@ public function testScalarArgsCannotBeAutowired()
435488
}
436489
}
437490

491+
/**
492+
* @requires PHP 8
493+
*/
494+
public function testUnionScalarArgsCannotBeAutowired()
495+
{
496+
$this->expectException('Symfony\Component\DependencyInjection\Exception\AutowiringFailedException');
497+
$this->expectExceptionMessage('Cannot autowire service "union_scalars": argument "$timeout" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionScalars::__construct()" is type-hinted "int|float", you should configure its value explicitly.');
498+
$container = new ContainerBuilder();
499+
500+
$container->register('union_scalars', UnionScalars::class)
501+
->setAutowired(true);
502+
503+
(new AutowirePass())->process($container);
504+
}
505+
438506
public function testNoTypeArgsCannotBeAutowired()
439507
{
440508
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Psr\Log\LoggerInterface;
66

7+
if (PHP_VERSION_ID >= 80000) {
8+
require __DIR__.'/uniontype_classes.php';
9+
}
10+
711
class Foo
812
{
913
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
class UnionScalars
6+
{
7+
public function __construct(int|float $timeout)
8+
{
9+
}
10+
}
11+
12+
class UnionClasses
13+
{
14+
public function __construct(CollisionA|CollisionB $collision)
15+
{
16+
}
17+
}
18+
19+
class UnionNull
20+
{
21+
public function __construct(CollisionInterface|null $c)
22+
{
23+
}
24+
}

src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use Doctrine\DBAL\Abstraction\Result;
1515
use Doctrine\DBAL\DBALException;
16+
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
1617
use Doctrine\DBAL\Platforms\AbstractPlatform;
1718
use Doctrine\DBAL\Query\QueryBuilder;
18-
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1919
use Doctrine\DBAL\Schema\Schema;
2020
use Doctrine\DBAL\Schema\SchemaConfig;
2121
use Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer;

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,15 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref
484484

485485
foreach ($reflectionType instanceof \ReflectionUnionType ? $reflectionType->getTypes() : [$reflectionType] as $type) {
486486
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : (string) $type;
487+
if ('null' === $phpTypeOrClass) {
488+
continue;
489+
}
487490

488491
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
489492
$types[] = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
490-
} elseif ('void' === $phpTypeOrClass || 'null' === $phpTypeOrClass) {
493+
} elseif ('void' === $phpTypeOrClass) {
491494
$types[] = new Type(Type::BUILTIN_TYPE_NULL, $nullable);
492-
} elseif ($reflectionType->isBuiltin()) {
495+
} elseif ($type->isBuiltin()) {
493496
$types[] = new Type($phpTypeOrClass, $nullable);
494497
} else {
495498
$types[] = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $declaringClass));

src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ public function php71TypesProvider()
223223
];
224224
}
225225

226+
/**
227+
* * @dataProvider php80TypesProvider
228+
* @requires PHP 8
229+
*/
230+
public function testExtractPhp80Type($property, array $type = null)
231+
{
232+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy', $property, []));
233+
}
234+
235+
public function php80TypesProvider()
236+
{
237+
return [
238+
['foo', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]],
239+
['bar', [new Type(Type::BUILTIN_TYPE_INT, true)]],
240+
['timeout', [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_FLOAT)]],
241+
['optional', [new Type(Type::BUILTIN_TYPE_INT, true), new Type(Type::BUILTIN_TYPE_FLOAT, true)]],
242+
['string', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Stringable'), new Type(Type::BUILTIN_TYPE_STRING)]],
243+
];
244+
}
245+
226246
/**
227247
* @dataProvider defaultValueProvider
228248
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
4+
5+
class Php80Dummy
6+
{
7+
public function getFoo(): array|null
8+
{
9+
}
10+
11+
public function setBar(int|null $bar)
12+
{
13+
}
14+
15+
public function setTimeout(int|float $timeout)
16+
{
17+
}
18+
19+
public function getOptional(): int|float|null
20+
{
21+
}
22+
23+
public function setString(string|\Stringable $string)
24+
{
25+
}
26+
}

0 commit comments

Comments
 (0)