Skip to content

Commit 9199bbb

Browse files
Merge branch '4.4' into 5.0
* 4.4: fix test Added Unit tests for php 8 union types.
2 parents bfd5797 + 8dbea1d commit 9199bbb

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

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();

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+
}

0 commit comments

Comments
 (0)