10BC0 more verbose deprecation message, add legacy tests · symfony/symfony@65100a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65100a3

Browse files
more verbose deprecation message, add legacy tests
1 parent 7c0b7ba commit 65100a3

13 files changed

+324
-17
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredMethodsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
5757
}
5858
if (false !== $doc = $r->getDocComment()) {
5959
if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
60-
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
60+
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
6161

6262
if ($this->isWither($reflectionMethod, $doc)) {
6363
$withers[] = [$reflectionMethod->name, [], true];

src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredPropertiesPass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
4040
if (!($type = $reflectionProperty->getType()) instanceof \ReflectionNamedType) {
4141
continue;
4242
}
43+
$doc = null;
4344
if (!$reflectionProperty->getAttributes(Required::class)
4445
&& ((false === $doc = $reflectionProperty->getDocComment()) || false === stripos($doc, '@required') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc))
4546
) {
4647
continue;
4748
}
48-
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
49+
// Trigger a deprecation only when PHPDoc has been found.
50+
if ($doc) {
51+
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead. It was found on "%s::%s".', $value::class, $reflectionProperty->getName());
52+
}
4953
if (\array_key_exists($name = $reflectionProperty->getName(), $properties)) {
5054
continue;
5155
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,13 @@ public function testOptionalArgsNoRequiredForCoreClasses()
698698
);
699699
}
700700

701+
/**
702+
* @group legacy
703+
*/
701704
public function testSetterInjection()
702705
{
706+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
707+
703708
$container = new ContainerBuilder();
704709
$container->register(Foo::class);
705710
$container->register(A::class);
@@ -774,7 +779,7 @@ public function testWithNonExistingSetterAndAutowiring()
774779
*/
775780
public function testExplicitMethodInjection()
776781
{
777-
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
782+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
778783

779784
$container = new ContainerBuilder();
780785
$container->register(Foo::class);
@@ -829,8 +834,13 @@ public function testIgnoreServiceWithClassNotExisting()
829834
$this->assertTrue($container->hasDefinition('bar'));
830835
}
831836

837+
/**
838+
* @group legacy
839+
*/
832840
public function testSetterInjectionCollisionThrowsException()
833841
{
842+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
843+
834844
$container = new ContainerBuilder();
835845

836846
$container->register('c1', CollisionA::class);
@@ -921,9 +931,13 @@ public function testWithFactory()
921931

922932
/**
923933
* @dataProvider provideNotWireableCalls
934+
*
935+
* @group legacy
924936
*/
925937
public function testNotWireableCalls($method, $expectedMsg)
926938
{
939+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
940+
927941
$container = new ContainerBuilder();
928942

929943
$foo = $container->register('foo', NotWireable::class)->setAutowired(true)
@@ -956,8 +970,13 @@ public function provideNotWireableCalls()
956970
];
957971
}
958972

973+
/**
974+
* @group legacy
975+
*/
959976
public function testSuggestRegisteredServicesWithSimilarCase()
960977
{
978+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
979+
961980
$container = new ContainerBuilder();
962981

963982
$container->register(LesTilleuls::class, LesTilleuls::class);
@@ -1137,8 +1156,13 @@ public function testErroredServiceLocator()
11371156
$this->assertSame(['Cannot autowire service "some_locator": it has type "Symfony\Component\DependencyInjection\Tests\Compiler\MissingClass" but this class was not found.'], $container->getDefinition('.errored.some_locator.'.MissingClass::class)->getErrors());
11381157
}
11391158

1159+
/**
1160+
* @group legacy
1161+
*/
11401162
public function testNamedArgumentAliasResolveCollisions()
11411163
{
1164+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
1165+
11421166
$container = new ContainerBuilder();
11431167

11441168
$container->register('c1', CollisionA::class);

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

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
1717
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherAnnotationStaticReturnType;
1920
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType;
2021

2122
require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';
@@ -24,10 +25,15 @@ class AutowireRequiredMethodsPassTest extends TestCase
2425
{
2526
use ExpectDeprecationTrait;
2627

28+
/**
29+
* @group legacy
30+
*/
2731
public function testSetterInjection()
2832
{
33+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
34+
2935
$container = new ContainerBuilder();
30-
$container->register(Foo::class);
36+
$container->register(FooAnnotation::class);
3137
$container->register(A::class);
3238
$container->register(CollisionA::class);
3339
$container->register(CollisionB::class);
@@ -76,9 +82,41 @@ public function testSetterInjectionWithAttribute()
7682
/**
7783
* @group legacy
7884
*/
79-
public function testExplicitMethodInjection()
85+
// @deprecated since Symfony 6.3, to be removed in 7.0
86+
public function testExplicitMethodInjectionAnnotation()
8087
{
81-
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
88+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
89+
90+
$container = new ContainerBuilder();
91+
$container->register(FooAnnotation::class);
92+
$container->register(A::class);
93+
$container->register(CollisionA::class);
94+
$container->register(CollisionB::class);
95+
96+
$container
97+
->register('setter_injection', SetterInjection::class)
98+
->setAutowired(true)
99+
->addMethodCall('notASetter', []);
100+
101+
(new ResolveClassPass())->process($container);
102+
(new AutowireRequiredMethodsPass())->process($container);
103+
104+
$methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();
105+
106+
$this->assertEquals(
107+
['notASetter', 'setFoo', 'setDependencies', 'setWithCallsConfigured', 'setChildMethodWithoutDocBlock'],
108+
array_column($methodCalls, 0)
109+
);
110+
$this->assertEquals([], $methodCalls[0][1]);
111+
}
112+
113+
/**
114+
* @group legacy
115+
*/
116+
public function testExplicitMethodInjectionAttribute()
117+
{
118+
// @deprecated since Symfony 6.3, to be removed in 7.0
119+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
82120

83121
$container = new ContainerBuilder();
84122
$container->register(Foo::class);
@@ -103,13 +141,18 @@ public function testExplicitMethodInjection()
103141
$this->assertEquals([], $methodCalls[0][1]);
104142
}
105143

144+
/**
145+
* @group legacy
146+
*/
106147
public function testWitherInjection()
107148
{
149+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
150+
108151
$container = new ContainerBuilder();
109-
$container->register(Foo::class);
152+
$container->register(FooAnnotation::class);
110153

111154
$container
112-
->register('wither', Wither::class)
155+
->register('wither', WitherAnnotation::class)
113156
->setAutowired(true);
114157

115158
(new ResolveClassPass())->process($container);
@@ -125,6 +168,32 @@ public function testWitherInjection()
125168
$this->assertSame($expected, $methodCalls);
126169
}
127170

171+
/**
172+
* @group legacy
173+
*/
174+
public function testWitherAnnotationWithStaticReturnTypeInjection()
175+
{
176+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
177+
178+
$container = new ContainerBuilder();
179+
$container->register(FooAnnotation::class);
180+
181+
$container
182+
->register('wither', WitherAnnotationStaticReturnType::class)
183+
->setAutowired(true);
184+
185+
(new ResolveClassPass())->process($container);
186+
(new AutowireRequiredMethodsPass())->process($container);
187+
188+
$methodCalls = $container->getDefinition('wither')->getMethodCalls();
189+
190+
$expected = [
191+
['withFoo', [], true],
192+
['setFoo', []],
193+
];
194+
$this->assertSame($expected, $methodCalls);
195+
}
196+
128197
public function testWitherWithStaticReturnTypeInjection()
129198
{
130199
$container = new ContainerBuilder();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredPropertiesPass;
1617
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -21,8 +22,15 @@
2122

2223
class AutowireRequiredPropertiesPassTest extends TestCase
2324
{
25+
use ExpectDeprecationTrait;
26+
27+
/**
28+
* @group legacy
29+
*/
2430
public function testInjection()
2531
{
32+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead. It was found on "Symfony\Component\DependencyInjection\Definition::plop".');
33+
2634
$container = new ContainerBuilder();
2735
$container->register(Bar::class);
2836
$container->register(A::class);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
1617
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1718
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
@@ -37,6 +38,8 @@
3738

3839
class ResolveBindingsPassTest extends TestCase
3940
{
41+
use ExpectDeprecationTrait;
42+
4043
public function testProcess()
4144
{
4245
$container = new ContainerBuilder();
@@ -143,8 +146,13 @@ public function testTypedReferenceSupport()
143146
$this->assertEquals([new Reference('bar')], $container->getDefinition('def3')->getArguments());
144147
}
145148

149+
/**
150+
* @group legacy
151+
*/
146152
public function testScalarSetter()
147153
{
154+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
155+
148156
$container = new ContainerBuilder();
149157

150158
$definition = $container->autowire('foo', ScalarSetter::class);

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@
4848
use Symfony\Component\DependencyInjection\Reference;
4949
use Symfony\Component\DependencyInjection\ServiceLocator;
5050
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
51+
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
5152
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
5253
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
5354
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
5455
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
5556
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
5657
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
5758
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;
59+
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherAnnotationStaticReturnType;
5860
use Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType;
5961
use Symfony\Component\DependencyInjection\TypedReference;
6062
use Symfony\Component\ExpressionLanguage\Expression;
@@ -1831,6 +1833,27 @@ public function testLazyWither()
18311833
$this->assertInstanceOf(Wither::class, $wither->withFoo1($wither->foo));
18321834
}
18331835

1836+
/**
1837+
* @group legacy
1838+
*/
1839+
public function testWitherAnnotationWithStaticReturnType()
1840+
{
1841+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
1842+
1843+
$container = new ContainerBuilder();
1844+
$container->register(FooAnnotation::class);
1845+
1846+
$container
1847+
->register('wither', WitherAnnotationStaticReturnType::class)
1848+
->setPublic(true)
1849+
->setAutowired(true);
1850+
1851+
$container->compile();
1852+
1853+
$wither = $container->get('wither');
1854+
$this->assertInstanceOf(FooAnnotation::class, $wither->foo);
1855+
}
1856+
18341857
public function testWitherWithStaticReturnType()
18351858
{
18361859
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
use Symfony\Component\DependencyInjection\Reference;
4242
use Symfony\Component\DependencyInjection\ServiceLocator;
4343
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
44+
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
4445
use Symfony\Component\DependencyInjection\Tests\Compiler\Wither;
46+
use Symfony\Component\DependencyInjection\Tests\Compiler\WitherAnnotation;
4547
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
4648
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
4749
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
@@ -1434,7 +1436,35 @@ public function testAliasCanBeFoundInTheDumpedContainerWhenBothTheAliasAndTheSer
14341436
$this->assertContains('bar', $service_ids);
14351437
}
14361438

1437-
public function testWither()
1439+
/**
1440+
* @group legacy
1441+
*/
1442+
public function testWitherAnnotation()
1443+
{
1444+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: The "@required" annotation on methods is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
1445+
1446+
$container = new ContainerBuilder();
1447+
$container->register(FooAnnotation::class)
1448+
->setAutowired(true);
1449+
1450+
$container
1451+
->register('wither', WitherAnnotation::class)
1452+
->setPublic(true)
1453+
->setAutowired(true);
1454+
1455+
$container->compile();
1456+
$dumper = new PhpDumper($container);
1457+
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_Wither_Annotation']);
1458+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_annotation.php', $dump);
1459+
eval('?>'.$dump);
1460+
1461+
$container = new \Symfony_DI_PhpDumper_Service_Wither_Annotation();
1462+
1463+
$wither = $container->get('wither');
1464+
$this->assertInstanceOf(FooAnnotation::class, $wither->foo);
1465+
}
1466+
1467+
public function testWitherAttribute()
14381468
{
14391469
$container = new ContainerBuilder();
14401470
$container->register(Foo::class)

0 commit comments

Comments
 (0)
0