Skip to content

Commit 2b587f4

Browse files
committed
Add tests for AutowireLocator
1 parent da0d4bd commit 2b587f4

4 files changed

+120
-0
lines changed

Diff for: tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,37 @@ public function testGetPrivateServiceInServiceSubscriber(): void
7878
);
7979
}
8080

81+
public function testGetPrivateServiceWithoutAutowireLocatorAttribute(): void
82+
{
83+
if (PHP_VERSION_ID < 80000) {
84+
self::markTestSkipped('The test uses PHP Attributes which are available since PHP 8.0.');
85+
}
86+
87+
$this->analyse(
88+
[
89+
__DIR__ . '/ExampleAutowireLocatorEmptyService.php',
90+
],
91+
[
92+
[
93+
'Service "private" is private.',
94+
22
95+
]
96+
]
97+
);
98+
}
99+
100+
public function testGetPrivateServiceViaAutowireLocatorAttribute(): void
101+
{
102+
if (PHP_VERSION_ID < 80000) {
103+
self::markTestSkipped('The test uses PHP Attributes which are available since PHP 8.0.');
104+
}
105+
106+
$this->analyse(
107+
[
108+
__DIR__ . '/ExampleAutowireLocatorService.php',
109+
],
110+
[]
111+
);
112+
}
113+
81114
}

Diff for: tests/Rules/Symfony/ContainerInterfaceUnknownServiceRuleTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,43 @@ public function testGetPrivateServiceInLegacyServiceSubscriber(): void
7272
);
7373
}
7474

75+
public function testGetPrivateServiceWithoutAutowireLocatorAttribute(): void
76+
{
77+
if (PHP_VERSION_ID < 80000) {
78+
self::markTestSkipped('The test uses PHP Attributes which are available since PHP 8.0.');
79+
}
80+
81+
$this->analyse(
82+
[
83+
__DIR__ . '/ExampleAutowireLocatorEmptyService.php',
84+
],
85+
[
86+
[
87+
'Service "Foo" is not registered in the AutowireLocator.',
88+
21
89+
],
90+
[
91+
'Service "private" is not registered in the AutowireLocator.',
92+
22
93+
]
94+
]
95+
);
96+
}
97+
98+
public function testGetPrivateServiceViaAutowireLocatorAttribute(): void
99+
{
100+
if (PHP_VERSION_ID < 80000) {
101+
self::markTestSkipped('The test uses PHP Attributes which are available since PHP 8.0.');
102+
}
103+
104+
$this->analyse(
105+
[
106+
__DIR__ . '/ExampleAutowireLocatorService.php',
107+
],
108+
[]
109+
);
110+
}
111+
75112
public static function getAdditionalConfigFiles(): array
76113
{
77114
return [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Rules\Symfony;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
7+
8+
final class ExampleAutowireLocatorEmptyService
9+
{
10+
11+
public function __construct(
12+
#[AutowireLocator([])]
13+
private ContainerInterface $locator
14+
)
15+
{
16+
$this->locator = $locator;
17+
}
18+
19+
public function privateServiceInLocator(): void
20+
{
21+
$this->locator->get('Foo');
22+
$this->locator->get('private');
23+
}
24+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Symfony;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
7+
8+
final class ExampleAutowireLocatorService
9+
{
10+
11+
public function __construct(
12+
#[AutowireLocator([
13+
'Foo',
14+
'private' => 'Foo',
15+
])]
16+
private ContainerInterface $locator
17+
)
18+
{
19+
}
20+
21+
public function privateServiceInLocator(): void
22+
{
23+
$this->locator->get('Foo');
24+
$this->locator->get('private');
25+
}
26+
}

0 commit comments

Comments
 (0)