Skip to content

Commit 469d7a0

Browse files
committed
Add tests for AutowireLocator
1 parent da0d4bd commit 469d7a0

4 files changed

+124
-0
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Testing\RuleTestCase;
99
use function class_exists;
1010
use function interface_exists;
11+
use const PHP_VERSION_ID;
1112

1213
/**
1314
* @extends RuleTestCase<ContainerInterfacePrivateServiceRule>
@@ -78,4 +79,37 @@ public function testGetPrivateServiceInServiceSubscriber(): void
7879
);
7980
}
8081

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

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

+38
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Testing\RuleTestCase;
1010
use function class_exists;
1111
use function interface_exists;
12+
use const PHP_VERSION_ID;
1213

1314
/**
1415
* @extends RuleTestCase<ContainerInterfaceUnknownServiceRule>
@@ -72,6 +73,43 @@ public function testGetPrivateServiceInLegacyServiceSubscriber(): void
7273
);
7374
}
7475

76+
public function testGetPrivateServiceWithoutAutowireLocatorAttribute(): void
77+
{
78+
if (PHP_VERSION_ID < 80000) {
79+
self::markTestSkipped('The test uses PHP Attributes which are available since PHP 8.0.');
80+
}
81+
82+
$this->analyse(
83+
[
84+
__DIR__ . '/ExampleAutowireLocatorEmptyService.php',
85+
],
86+
[
87+
[
88+
'Service "Foo" is not registered in the AutowireLocator.',
89+
21,
90+
],
91+
[
92+
'Service "private" is not registered in the AutowireLocator.',
93+
22,
94+
],
95+
]
96+
);
97+
}
98+
99+
public function testGetPrivateServiceViaAutowireLocatorAttribute(): void
100+
{
101+
if (PHP_VERSION_ID < 80000) {
102+
self::markTestSkipped('The test uses PHP Attributes which are available since PHP 8.0.');
103+
}
104+
105+
$this->analyse(
106+
[
107+
__DIR__ . '/ExampleAutowireLocatorService.php',
108+
],
109+
[]
110+
);
111+
}
112+
75113
public static function getAdditionalConfigFiles(): array
76114
{
77115
return [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 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+
25+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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' => '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+
27+
}

0 commit comments

Comments
 (0)