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

Sign up
Appearance settings

Commit ec48673

Browse files
committed
Add tests for AutowireLocator
1 parent da0d4bd commit ec48673

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

‎tests/Rules/Symfony/ContainerInterfacePrivateServiceRuleTest.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Symfony\Configuration;
77
use PHPStan\Symfony\XmlServiceMapFactory;
88
use PHPStan\Testing\RuleTestCase;
9+
use const PHP_VERSION_ID;
910
use function class_exists;
1011
use function interface_exists;
1112

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

‎tests/Rules/Symfony/ContainerInterfaceUnknownServiceRuleTest.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Symfony\Configuration;
88
use PHPStan\Symfony\XmlServiceMapFactory;
99
use PHPStan\Testing\RuleTestCase;
10+
use const PHP_VERSION_ID;
1011
use function class_exists;
1112
use function interface_exists;
1213

@@ -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 [
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
25+
}
Lines changed: 27 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /