|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Doctrine\ORM; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Types\Type; |
| 6 | +use PHPStan\Rules\Rule; |
| 7 | +use PHPStan\Testing\RuleTestCase; |
| 8 | +use PHPStan\Type\Doctrine\DescriptorRegistry; |
| 9 | +use PHPStan\Type\Doctrine\Descriptors\ArrayType; |
| 10 | +use PHPStan\Type\Doctrine\Descriptors\BigIntType; |
| 11 | +use PHPStan\Type\Doctrine\Descriptors\BinaryType; |
| 12 | +use PHPStan\Type\Doctrine\Descriptors\DateTimeImmutableType; |
| 13 | +use PHPStan\Type\Doctrine\Descriptors\DateTimeType; |
| 14 | +use PHPStan\Type\Doctrine\Descriptors\DateType; |
| 15 | +use PHPStan\Type\Doctrine\Descriptors\IntegerType; |
| 16 | +use PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor; |
| 17 | +use PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor; |
| 18 | +use PHPStan\Type\Doctrine\Descriptors\StringType; |
| 19 | +use PHPStan\Type\Doctrine\ObjectMetadataResolver; |
| 20 | +use Ramsey\Uuid\Doctrine\UuidType; |
| 21 | + |
| 22 | +/** |
| 23 | + * @extends RuleTestCase<EntityColumnRule> |
| 24 | + */ |
| 25 | +class EntityEmbeddableRuleTest extends RuleTestCase |
| 26 | +{ |
| 27 | + |
| 28 | + protected function getRule(): Rule |
| 29 | + { |
| 30 | + if (!Type::hasType(CustomType::NAME)) { |
| 31 | + Type::addType(CustomType::NAME, CustomType::class); |
| 32 | + } |
| 33 | + if (!Type::hasType(UuidType::NAME)) { |
| 34 | + Type::addType(UuidType::NAME, UuidType::class); |
| 35 | + } |
| 36 | + |
| 37 | + return new EntityEmbeddableRule( |
| 38 | + new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', null), |
| 39 | + new DescriptorRegistry([ |
| 40 | + new BigIntType(), |
| 41 | + new StringType(), |
| 42 | + new DateTimeType(), |
| 43 | + new DateTimeImmutableType(), |
| 44 | + new BinaryType(), |
| 45 | + new IntegerType(), |
| 46 | + new ReflectionDescriptor(CustomType::class, $this->createBroker()), |
| 47 | + new DateType(), |
| 48 | + new UuidTypeDescriptor(UuidType::class), |
| 49 | + new ArrayType(), |
| 50 | + ]), |
| 51 | + true |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testEmbedded(): void |
| 56 | + { |
| 57 | + $this->analyse([__DIR__ . '/data/EntityWithEmbeddable.php'], []); |
| 58 | + } |
| 59 | + |
| 60 | + public function testEmbeddedWithWrongTypeHint(): void |
| 61 | + { |
| 62 | + $this->analyse([__DIR__ . '/data/EntityWithBrokenEmbeddable.php'], [ |
| 63 | + [ |
| 64 | + 'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenEmbeddable::$embedded type mapping mismatch: mapping specifies PHPStan\Rules\Doctrine\ORM\Embeddable but property expects int.', |
| 65 | + 24, |
| 66 | + ], |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments