|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Doctrine\ORM; |
| 4 | + |
| 5 | +use Iterator; |
| 6 | +use PHPStan\Rules\Rule; |
| 7 | +use PHPStan\Testing\RuleTestCase; |
| 8 | +use PHPStan\Type\Doctrine\ObjectMetadataResolver; |
| 9 | + |
| 10 | +/** |
| 11 | + * @extends RuleTestCase<EntityConstructorNotFinalRule> |
| 12 | + */ |
| 13 | +class EntityConstructorNotFinalRuleTest extends RuleTestCase |
| 14 | +{ |
| 15 | + |
| 16 | + /** @var string|null */ |
| 17 | + private $objectManagerLoader; |
| 18 | + |
| 19 | + protected function getRule(): Rule |
| 20 | + { |
| 21 | + return new EntityConstructorNotFinalRule( |
| 22 | + new ObjectMetadataResolver($this->objectManagerLoader) |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @dataProvider ruleProvider |
| 28 | + * @param list<array{0: string, 1: int, 2?: string}> $expectedErrors |
| 29 | + */ |
| 30 | + public function testRule(string $file, array $expectedErrors): void |
| 31 | + { |
| 32 | + $this->objectManagerLoader = __DIR__ . '/entity-manager.php'; |
| 33 | + $this->analyse([$file], $expectedErrors); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @dataProvider ruleProvider |
| 38 | + * @param list<array{0: string, 1: int, 2?: string}> $expectedErrors |
| 39 | + */ |
| 40 | + public function testRuleWithoutObjectManagerLoader(string $file, array $expectedErrors): void |
| 41 | + { |
| 42 | + $this->objectManagerLoader = null; |
| 43 | + $this->analyse([$file], $expectedErrors); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return Iterator<mixed[]> |
| 48 | + */ |
| 49 | + public function ruleProvider(): Iterator |
| 50 | + { |
| 51 | + yield 'entity final constructor' => [ |
| 52 | + __DIR__ . '/data/EntityFinalConstructor.php', |
| 53 | + [ |
| 54 | + [ |
| 55 | + 'Constructor of class PHPStan\Rules\Doctrine\ORM\EntityFinalConstructor is final which can cause problems with proxies.', |
| 56 | + 12, |
| 57 | + ], |
| 58 | + ], |
| 59 | + ]; |
| 60 | + |
| 61 | + yield 'entity non-final constructor' => [ |
| 62 | + __DIR__ . '/data/EntityNonFinalConstructor.php', |
| 63 | + [], |
| 64 | + ]; |
| 65 | + |
| 66 | + yield 'correct entity' => [ |
| 67 | + __DIR__ . '/data/MyEntity.php', |
| 68 | + [], |
| 69 | + ]; |
| 70 | + |
| 71 | + yield 'non-entity final constructor' => [ |
| 72 | + __DIR__ . '/data/NonEntityFinalConstructor.php', |
| 73 | + [], |
| 74 | + ]; |
| 75 | + |
| 76 | + yield 'final embeddable' => [ |
| 77 | + __DIR__ . '/data/FinalEmbeddable.php', |
| 78 | + [], |
| 79 | + ]; |
| 80 | + |
| 81 | + yield 'non final embeddable' => [ |
| 82 | + __DIR__ . '/data/MyEmbeddable.php', |
| 83 | + [], |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments