Skip to content

Commit d3fd0e6

Browse files
committed
[TASK] Add test for PrototypeServiceDefinitionChecker
1 parent b35dbea commit d3fd0e6

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Fixtures;
4+
5+
final class NonPrototypeClass
6+
{
7+
8+
public function __construct(string $requiredString)
9+
{
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Fixtures;
4+
5+
final class PrototypeClass
6+
{
7+
8+
public function __construct(string $nonRequiredString = '')
9+
{
10+
}
11+
12+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Service;
4+
5+
use PhpParser\BuilderFactory;
6+
use PhpParser\Node\Expr\StaticCall;
7+
use PHPUnit\Framework\TestCase;
8+
use SaschaEgerer\PhpstanTypo3\Service\PrototypeServiceDefinitionChecker;
9+
use SaschaEgerer\PhpstanTypo3\Service\ServiceDefinition;
10+
use SaschaEgerer\PhpstanTypo3\Tests\Unit\Fixtures\NonPrototypeClass;
11+
use SaschaEgerer\PhpstanTypo3\Tests\Unit\Fixtures\PrototypeClass;
12+
13+
final class PrototypeServiceDefinitionCheckerTest extends TestCase
14+
{
15+
16+
private PrototypeServiceDefinitionChecker $subject;
17+
18+
public static function provideNonPrototypes(): \Generator
19+
{
20+
$builderFactory = new BuilderFactory();
21+
$prototypeClass = $builderFactory->classConstFetch(PrototypeClass::class, 'class');
22+
$nonPrototypeClass = $builderFactory->classConstFetch(NonPrototypeClass::class, 'class');
23+
24+
yield 'Service definition has tags' => [
25+
$builderFactory->staticCall('Foo', 'foo', [$prototypeClass]),
26+
new ServiceDefinition('foo', 'bar', false, false, null, false, false, true),
27+
];
28+
29+
yield 'Service definition has method calls' => [
30+
$builderFactory->staticCall('Foo', 'foo', [$prototypeClass]),
31+
new ServiceDefinition('foo', 'bar', false, false, null, false, true, false),
32+
];
33+
34+
yield 'Service definition has non prototype class' => [
35+
$builderFactory->staticCall('Foo', 'foo', [$nonPrototypeClass]),
36+
new ServiceDefinition('foo', 'bar', false, false, null, false, false, false),
37+
];
38+
}
39+
40+
public static function providePrototypes(): \Generator
41+
{
42+
$builderFactory = new BuilderFactory();
43+
$prototypeClass = $builderFactory->classConstFetch(PrototypeClass::class, 'class');
44+
45+
yield 'Service definition has no tags, no method calls and class has no required constructor arguments' => [
46+
$builderFactory->staticCall('Foo', 'foo', [$prototypeClass]),
47+
new ServiceDefinition('foo', 'bar', false, false, null, false, false, false),
48+
];
49+
}
50+
51+
protected function setUp(): void
52+
{
53+
$this->subject = new PrototypeServiceDefinitionChecker();
54+
}
55+
56+
/**
57+
* @dataProvider providePrototypes
58+
*/
59+
public function testIsPrototypeIsTrue(StaticCall $node, ServiceDefinition $serviceDefinition): void
60+
{
61+
self::assertTrue($this->subject->isPrototype($serviceDefinition, $node));
62+
}
63+
64+
/**
65+
* @dataProvider provideNonPrototypes
66+
*/
67+
public function testIsPrototypeIsFalse(StaticCall $node, ServiceDefinition $serviceDefinition): void
68+
{
69+
self::assertFalse($this->subject->isPrototype($serviceDefinition, $node));
70+
}
71+
72+
}

0 commit comments

Comments
 (0)