Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 64f2805

Browse filesBrowse files
Merge branch '3.4' into 4.4
* 3.4: Added Unit tests for php 8 union types.
2 parents d2efe50 + e707967 commit 64f2805
Copy full SHA for 64f2805

File tree

Expand file treeCollapse file tree

6 files changed

+147
-2
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+147
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+68Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php
+4Lines 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
}
+24Lines 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/PropertyInfo/Extractor/ReflectionExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,15 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref
326326

327327
foreach ($reflectionType instanceof \ReflectionUnionType ? $reflectionType->getTypes() : [$reflectionType] as $type) {
328328
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : (string) $type;
329+
if ('null' === $phpTypeOrClass) {
330+
continue;
331+
}
329332

330333
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
331334
$types[] = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
332-
} elseif ('void' === $phpTypeOrClass || 'null' === $phpTypeOrClass) {
335+
} elseif ('void' === $phpTypeOrClass) {
333336
$types[] = new Type(Type::BUILTIN_TYPE_NULL, $nullable);
334-
} elseif ($reflectionType->isBuiltin()) {
337+
} elseif ($type->isBuiltin()) {
335338
$types[] = new Type($phpTypeOrClass, $nullable);
336339
} else {
337340
$types[] = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $reflectionMethod));

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,26 @@ public function php71TypesProvider()
219219
];
220220
}
221221

222+
/**
223+
* * @dataProvider php80TypesProvider
224+
* @requires PHP 8
225+
*/
226+
public function testExtractPhp80Type($property, array $type = null)
227+
{
228+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy', $property, []));
229+
}
230+
231+
public function php80TypesProvider()
232+
{
233+
return [
234+
['foo', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]],
235+
['bar', [new Type(Type::BUILTIN_TYPE_INT, true)]],
236+
['timeout', [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_FLOAT)]],
237+
['optional', [new Type(Type::BUILTIN_TYPE_INT, true), new Type(Type::BUILTIN_TYPE_FLOAT, true)]],
238+
['string', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Stringable'), new Type(Type::BUILTIN_TYPE_STRING)]],
239+
];
240+
}
241+
222242
/**
223243
* @dataProvider defaultValueProvider
224244
*/
+26Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.