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