From fd159e9b7f8d23f1f62539e1f2d21d1c43a73dcd Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 20 Nov 2026 21:06:12 +0400 Subject: [PATCH 1/5] test: cover annotation defaults, flags and load methods Assisted-By: Claude Opus 5.5 --- tests/Unit/Attribute/AnnotationsTest.php | 79 ++++++++++++++++++++++++ tests/Unit/Attribute/ColumnTest.php | 16 +++++ tests/Unit/Utils/EntityUtilsTest.php | 12 ++++ 3 files changed, 107 insertions(+) create mode 100644 tests/Unit/Attribute/AnnotationsTest.php diff --git a/tests/Unit/Attribute/AnnotationsTest.php b/tests/Unit/Attribute/AnnotationsTest.php new file mode 100644 index 00000000..34d2b596 --- /dev/null +++ b/tests/Unit/Attribute/AnnotationsTest.php @@ -0,0 +1,79 @@ +getTypecast(), ['Foo', 'Bar']); + } + + public function entityUnwrapsASingleTypecastHandler(): void + { + $entity = new Entity(typecast: ['Foo']); + + Assert::same($entity->getTypecast(), 'Foo'); + } + + #[DataSet([false, false, false, null], 'nothing generated')] + #[DataSet([true, false, false, GeneratedField::BEFORE_INSERT], 'before insert')] + #[DataSet([false, true, false, GeneratedField::ON_INSERT], 'on insert')] + #[DataSet([false, false, true, GeneratedField::BEFORE_UPDATE], 'before update')] + #[DataSet([true, true, true, GeneratedField::BEFORE_INSERT | GeneratedField::ON_INSERT | GeneratedField::BEFORE_UPDATE], 'everything')] + public function generatedValueCombinesFlags(bool $beforeInsert, bool $onInsert, bool $beforeUpdate, ?int $flags): void + { + $generated = new GeneratedValue($beforeInsert, $onInsert, $beforeUpdate); + + Assert::same($generated->getFlags(), $flags); + } + + public function joinedTableHasItsType(): void + { + Assert::same((new JoinedTable())->getType(), 'joined'); + } + + public function singleTableHasItsType(): void + { + Assert::same((new SingleTable())->getType(), 'single'); + } + + #[DataSet(['eager', Relation::LOAD_EAGER])] + #[DataSet([Relation::LOAD_EAGER, Relation::LOAD_EAGER])] + #[DataSet(['promise', Relation::LOAD_PROMISE])] + #[DataSet(['lazy', Relation::LOAD_PROMISE])] + #[DataSet([Relation::LOAD_PROMISE, Relation::LOAD_PROMISE])] + #[DataSet([null, null])] + #[DataSet(['unknown', null])] + public function inverseResolvesTheLoadMethod(string|int|null $load, ?int $method): void + { + $inverse = new Inverse(as: 'posts', type: 'hasMany', load: $load); + + Assert::same($inverse->getLoadMethod(), $method); + } + + public function primaryKeyIsAUniqueIndex(): void + { + $key = new PrimaryKey(['id', 'type']); + + Assert::true($key->isUnique()); + Assert::same($key->getColumns(), ['id', 'type']); + } +} diff --git a/tests/Unit/Attribute/ColumnTest.php b/tests/Unit/Attribute/ColumnTest.php index 1f1ec71b..8c43f88c 100644 --- a/tests/Unit/Attribute/ColumnTest.php +++ b/tests/Unit/Attribute/ColumnTest.php @@ -5,6 +5,7 @@ namespace Cycle\Annotated\Tests\Unit\Attribute; use Cycle\Annotated\Annotation\Column; +use Cycle\Annotated\Tests\Unit\Attribute\SingleTable\StringableObject; use Cycle\Annotated\Tests\Unit\Attribute\SingleTable\StringEnum; use Testo\Assert; use Testo\Test; @@ -152,6 +153,21 @@ public function testCharsetAndCollationAttributes(): void ]); } + public function testValuesOfANonEnumColumnStayAnAttribute(): void + { + $column = new Column(type: 'string', values: ['a', 'b']); + + Assert::same($column->getType(), 'string'); + Assert::same($column->getAttributes(), ['values' => ['a', 'b']]); + } + + public function testEnumValuesThatAreNotScalarAreLeftEmpty(): void + { + $column = new Column(type: 'enum', values: ['a', new StringableObject('b'), 1]); + + Assert::same($column->getType(), 'enum(a,,1)'); + } + private function getColumn(string $field): Column { $ref = new \ReflectionClass(self::class); diff --git a/tests/Unit/Utils/EntityUtilsTest.php b/tests/Unit/Utils/EntityUtilsTest.php index c7cde20f..dd8cd834 100644 --- a/tests/Unit/Utils/EntityUtilsTest.php +++ b/tests/Unit/Utils/EntityUtilsTest.php @@ -4,6 +4,7 @@ namespace Cycle\Annotated\Tests\Unit\Utils; +use Cycle\Annotated\Entities; use Cycle\Annotated\Exception\AnnotationException; use Cycle\Annotated\Tests\Fixtures\Fixtures22\Attributed\Person; use Cycle\Annotated\Utils\EntityUtils; @@ -12,6 +13,7 @@ use Spiral\Attributes\ReaderInterface; use Testo\Assert; use Testo\Data\DataProvider; +use Testo\Data\DataSet; use Testo\Expect; use Testo\Test; @@ -63,6 +65,16 @@ public function testFindParent( Assert::equals($actualParent, $expectedParent); } + #[DataSet([Entities::TABLE_NAMING_PLURAL, 'blog_posts'])] + #[DataSet([Entities::TABLE_NAMING_SINGULAR, 'blog_post'])] + #[DataSet([Entities::TABLE_NAMING_NONE, 'blog_posts'])] + public function testTableName(int $strategy, string $table): void + { + $entityUtils = new EntityUtils(new AttributeReader()); + + Assert::same($entityUtils->tableName('blogPosts', $strategy), $table); + } + public function testGetParentException(): void { $entityUtils = new EntityUtils(new AttributeReader()); From eb113f80d2270fe6e60ff113359238465359a1d6 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 20 Nov 2026 21:06:13 +0400 Subject: [PATCH 2/5] test: cover the configurator and embeddings Assisted-By: Claude Opus 5.5 --- tests/Fixtures/Configurator/Address.php | 15 ++ tests/Fixtures/Configurator/AddressMapper.php | 7 + tests/Fixtures/Configurator/Author.php | 7 + tests/Fixtures/Configurator/Comment.php | 7 + tests/Fixtures/Configurator/Post.php | 26 +++ tests/Fixtures/Configurator/Sub/Comments.php | 7 + .../Fixtures/Configurator/Sub/Typecaster.php | 7 + tests/Fixtures/Configurator/Typecaster.php | 7 + tests/Fixtures/Fixtures29/Address.php | 19 ++ tests/Unit/ConfiguratorTest.php | 221 ++++++++++++++++++ tests/Unit/EmbeddingsTest.php | 32 +++ ...notationRequiredArgumentsExceptionTest.php | 47 ++++ .../Locator/TokenizerEmbeddingLocatorTest.php | 17 ++ 13 files changed, 419 insertions(+) create mode 100644 tests/Fixtures/Configurator/Address.php create mode 100644 tests/Fixtures/Configurator/AddressMapper.php create mode 100644 tests/Fixtures/Configurator/Author.php create mode 100644 tests/Fixtures/Configurator/Comment.php create mode 100644 tests/Fixtures/Configurator/Post.php create mode 100644 tests/Fixtures/Configurator/Sub/Comments.php create mode 100644 tests/Fixtures/Configurator/Sub/Typecaster.php create mode 100644 tests/Fixtures/Configurator/Typecaster.php create mode 100644 tests/Fixtures/Fixtures29/Address.php create mode 100644 tests/Unit/ConfiguratorTest.php create mode 100644 tests/Unit/EmbeddingsTest.php create mode 100644 tests/Unit/Exception/AnnotationRequiredArgumentsExceptionTest.php diff --git a/tests/Fixtures/Configurator/Address.php b/tests/Fixtures/Configurator/Address.php new file mode 100644 index 00000000..c9085242 --- /dev/null +++ b/tests/Fixtures/Configurator/Address.php @@ -0,0 +1,15 @@ +configurator()->initEntity(new Entity(typecast: ['Typecaster', 'Sub/Typecaster']), $class); + + Assert::same($entity->getTypecast(), [Typecaster::class, SubTypecaster::class]); + } + + public function configuresAnEmbeddingFromItsAttribute(): void + { + $class = new \ReflectionClass(Address::class); + $embeddable = $class->getAttributes(Embeddable::class)[0]->newInstance(); + + $entity = $this->configurator()->initEmbedding($embeddable, $class); + + Assert::same($entity->getRole(), 'postal'); + Assert::same($entity->getMapper(), AddressMapper::class); + Assert::same($entity->getTypecast(), [Typecaster::class, SubTypecaster::class]); + } + + public function prefixesTheColumnsOfAnEmbedding(): void + { + $entity = new EntitySchema(); + + $this->configurator()->initFields($entity, new \ReflectionClass(Address::class), 'address_'); + + Assert::same($entity->getFields()->get('street')->getColumn(), 'address_street'); + } + + public function wrapsAnExceptionOfTheReader(): never + { + $reader = $this->throwingReader(new \RuntimeException('broken', 42)); + + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(42); + + (new Configurator($reader))->initFields(new EntitySchema(), new \ReflectionClass(Address::class)); + } + + public function reportsAnArgumentOfAWrongType(): never + { + $reader = $this->throwingReader(new \TypeError('not a string')); + + Expect::exception(AnnotationWrongTypeArgumentException::class) + ->withMessageContaining(Address::class . '.street') + ->withMessageContaining('not a string'); + + (new Configurator($reader))->initFields(new EntitySchema(), new \ReflectionClass(Address::class)); + } + + public function prefersTheInverseGivenToTheRelation(): void + { + $entity = new EntitySchema(); + $entity->setClass(Post::class); + + $this->configurator()->initRelations($entity, new \ReflectionClass(Post::class)); + + Assert::same($entity->getRelations()->get('comments')->getInverseName(), 'post'); + } + + public function resolvesTheCollectionClassOfARelation(): void + { + $entity = new EntitySchema(); + $entity->setClass(Post::class); + + $this->configurator()->initRelations($entity, new \ReflectionClass(Post::class)); + + Assert::same($entity->getRelations()->get('comments')->getOptions()->get('collection'), Comments::class); + } + + public function keepsTheInnerKeyOfAPropertyForeignKey(): void + { + $entity = new EntitySchema(); + $entity->setClass(Post::class); + + $this->configurator()->initForeignKeys(new Entity(), $entity, new \ReflectionClass(Post::class)); + + $keys = \iterator_to_array($entity->getForeignKeys(), false); + Assert::count($keys, 1); + Assert::same($keys[0]->getTarget(), Author::class); + Assert::same($keys[0]->getInnerColumns(), ['author_ref']); + } + + public function makesANullableColumnDefaultToNull(): void + { + $field = $this->field(new Column(type: 'string', nullable: true)); + + Assert::true($field->getOptions()->get(ColumnOption::OPT_NULLABLE)); + Assert::true($field->getOptions()->has(ColumnOption::OPT_DEFAULT)); + Assert::null($field->getOptions()->get(ColumnOption::OPT_DEFAULT)); + } + + public function leavesANotNullableColumnWithoutDefault(): void + { + $field = $this->field(new Column(type: 'string')); + + Assert::false($field->getOptions()->has(ColumnOption::OPT_NULLABLE)); + Assert::false($field->getOptions()->has(ColumnOption::OPT_DEFAULT)); + } + + public function castsTheDefaultWhenAsked(): void + { + $field = $this->field(new Column(type: 'string', default: 'x', castDefault: true)); + + Assert::true($field->getOptions()->get(ColumnOption::OPT_CAST_DEFAULT)); + } + + public function leavesTheDefaultUncastByDefault(): void + { + $field = $this->field(new Column(type: 'string', default: 'x')); + + Assert::false($field->getOptions()->has(ColumnOption::OPT_CAST_DEFAULT)); + } + + public function copiesTheDatabaseSpecificAttributes(): void + { + $field = $this->field(new Column(type: 'integer', unsigned: true, zerofill: true)); + + Assert::same(\iterator_to_array($field->getAttributes()), ['unsigned' => true, 'zerofill' => true]); + } + + #[DataSet(['serial', GeneratedField::ON_INSERT])] + #[DataSet(['bigserial', GeneratedField::ON_INSERT])] + #[DataSet(['smallserial', GeneratedField::ON_INSERT])] + #[DataSet(['integer', null])] + public function generatesSerialColumnsOnInsert(string $type, ?int $generated): void + { + $field = $this->field(new Column(type: $type)); + + Assert::same($field->getGenerated(), $generated); + } + + public function resolvesANameWrittenAsAPath(): void + { + $name = $this->configurator()->resolveName('/Sub/Comments', new \ReflectionClass(Post::class)); + + Assert::same($name, Comments::class); + } + + public function resolvesANameRelativeToTheGlobalNamespace(): void + { + $name = $this->configurator()->resolveName('/ArrayObject', new \ReflectionClass(\stdClass::class)); + + Assert::same($name, 'ArrayObject'); + } + + private function field(Column $column): Field + { + return $this->configurator()->initField('value', $column, new \ReflectionClass(Post::class), ''); + } + + private function throwingReader(\Throwable $error): Reader + { + return new class($error) extends Reader { + public function __construct(private readonly \Throwable $error) {} + + public function getClassMetadata(\ReflectionClass $class, ?string $name = null): iterable + { + return []; + } + + public function getFunctionMetadata(\ReflectionFunctionAbstract $function, ?string $name = null): iterable + { + return []; + } + + public function getPropertyMetadata(\ReflectionProperty $property, ?string $name = null): iterable + { + throw $this->error; + } + + public function getConstantMetadata(\ReflectionClassConstant $constant, ?string $name = null): iterable + { + return []; + } + + public function getParameterMetadata(\ReflectionParameter $parameter, ?string $name = null): iterable + { + return []; + } + }; + } + + private function configurator(): Configurator + { + return new Configurator(new AttributeReader()); + } +} diff --git a/tests/Unit/EmbeddingsTest.php b/tests/Unit/EmbeddingsTest.php new file mode 100644 index 00000000..443bf8dd --- /dev/null +++ b/tests/Unit/EmbeddingsTest.php @@ -0,0 +1,32 @@ +withMessage(\sprintf('Relations are not allowed within embeddable entities in `%s`', Address::class)); + + $embeddings->run(new Registry(new DatabaseManager(new DatabaseConfig()))); + } +} diff --git a/tests/Unit/Exception/AnnotationRequiredArgumentsExceptionTest.php b/tests/Unit/Exception/AnnotationRequiredArgumentsExceptionTest.php new file mode 100644 index 00000000..3d278990 --- /dev/null +++ b/tests/Unit/Exception/AnnotationRequiredArgumentsExceptionTest.php @@ -0,0 +1,47 @@ +getMessage(), + \sprintf('Some of required arguments [`as`, `type`] is missed on `%s.street.`', Address::class), + ); + Assert::same($exception->getCode(), 3); + Assert::same($exception->getPrevious(), $previous); + } + + public function acceptsAnAnnotationWithoutConstructor(): void + { + $exception = AnnotationRequiredArgumentsException::createFor( + new \ReflectionProperty(Address::class, 'street'), + \stdClass::class, + new \ArgumentCountError(), + ); + + Assert::same( + $exception->getMessage(), + \sprintf('Some of required arguments [``] is missed on `%s.street.`', Address::class), + ); + } +} diff --git a/tests/Unit/Locator/TokenizerEmbeddingLocatorTest.php b/tests/Unit/Locator/TokenizerEmbeddingLocatorTest.php index 3b6e5240..94a57e5d 100644 --- a/tests/Unit/Locator/TokenizerEmbeddingLocatorTest.php +++ b/tests/Unit/Locator/TokenizerEmbeddingLocatorTest.php @@ -7,6 +7,7 @@ use Cycle\Annotated\Annotation\Embeddable; use Cycle\Annotated\Locator\Embedding; use Cycle\Annotated\Locator\TokenizerEmbeddingLocator; +use Cycle\Annotated\Tests\Fixtures\Configurator\Address as PostalAddress; use Cycle\Annotated\Tests\Fixtures\Fixtures1\Child; use Cycle\Annotated\Tests\Fixtures\Fixtures7\Address; use Cycle\Testing\ORM\Schema\ClassList; @@ -33,6 +34,22 @@ public static function classesDataProvider(): \Traversable Child::class => new \ReflectionClass(Child::class), ], ]; + yield [ + [ + new Embedding( + new Embeddable(role: 'address', columnPrefix: 'address_'), + new \ReflectionClass(Address::class), + ), + new Embedding( + new Embeddable(role: 'postal', mapper: 'AddressMapper', typecast: ['Typecaster', 'Sub/Typecaster']), + new \ReflectionClass(PostalAddress::class), + ), + ], + [ + Address::class => new \ReflectionClass(Address::class), + PostalAddress::class => new \ReflectionClass(PostalAddress::class), + ], + ]; } #[DataProvider('classesDataProvider')] From 018ae656b98d7784c829f648a384a1ae1e0e158e Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 20 Nov 2026 21:06:14 +0400 Subject: [PATCH 3/5] test: cover entity name resolution and table inheritance Assisted-By: Claude Opus 5.5 --- tests/Feature/Common/EntitiesTest.php | 87 +++++++++++++++++++ .../Inheritance/TableInheritanceTest.php | 55 ++++++++++++ tests/Fixtures/Fixtures29/Animal.php | 15 ++++ tests/Fixtures/Fixtures29/Author.php | 18 ++++ tests/Fixtures/Fixtures29/Book.php | 23 +++++ tests/Fixtures/Fixtures29/Car.php | 12 +++ tests/Fixtures/Fixtures29/Dog.php | 12 +++ tests/Fixtures/Fixtures29/DogOwner.php | 19 ++++ tests/Fixtures/Fixtures29/Review.php | 17 ++++ tests/Fixtures/Fixtures29/UnicornOwner.php | 19 ++++ tests/Fixtures/Fixtures29/Vehicle.php | 15 ++++ 11 files changed, 292 insertions(+) create mode 100644 tests/Feature/Common/EntitiesTest.php create mode 100644 tests/Feature/Common/Inheritance/TableInheritanceTest.php create mode 100644 tests/Fixtures/Fixtures29/Animal.php create mode 100644 tests/Fixtures/Fixtures29/Author.php create mode 100644 tests/Fixtures/Fixtures29/Book.php create mode 100644 tests/Fixtures/Fixtures29/Car.php create mode 100644 tests/Fixtures/Fixtures29/Dog.php create mode 100644 tests/Fixtures/Fixtures29/DogOwner.php create mode 100644 tests/Fixtures/Fixtures29/Review.php create mode 100644 tests/Fixtures/Fixtures29/UnicornOwner.php create mode 100644 tests/Fixtures/Fixtures29/Vehicle.php diff --git a/tests/Feature/Common/EntitiesTest.php b/tests/Feature/Common/EntitiesTest.php new file mode 100644 index 00000000..09d58d2c --- /dev/null +++ b/tests/Feature/Common/EntitiesTest.php @@ -0,0 +1,87 @@ +dbal); + + (new Entities(new TokenizerEntityLocator($this->locator, $reader), $reader))->run($registry); + + $options = $registry->getEntity('withTable')->getRelations()->get('tags')->getOptions(); + Assert::same($options->get('throughInnerKey'), ['withTable_id']); + Assert::same($options->get('throughOuterKey'), ['tag_id']); + } + + public function resolvesTheTargetAndColumnsOfAForeignKey(): void + { + $registry = $this->register(Author::class, Book::class); + + $keys = \iterator_to_array($registry->getEntity('book')->getForeignKeys(), false); + Assert::count($keys, 1); + Assert::same($keys[0]->getTarget(), 'author'); + Assert::same($keys[0]->getInnerColumns(), ['author_id', 'author_kind']); + Assert::same($keys[0]->getOuterColumns(), ['id', 'kind']); + } + + public function rejectsAForeignKeyOnAnUnknownColumn(): never + { + Expect::exception(AnnotationException::class)->withMessage('Unable to resolve column name.'); + + $this->register(Author::class, Review::class); + } + + public function pointsARelationToAnInheritanceChildAtItsParent(): void + { + $registry = $this->register(Animal::class, Dog::class, DogOwner::class); + + Assert::same($registry->getEntity('dogOwner')->getRelations()->get('dog')->getTarget(), 'animal'); + } + + public function rejectsARelationToAnUnknownTarget(): never + { + Expect::exception(RelationException::class)->withMessageContaining('`unicornOwner`.`unicorn`'); + + $this->register(Animal::class, Dog::class, UnicornOwner::class); + } + + /** + * @param class-string ...$classes + */ + private function register(string ...$classes): Registry + { + $reader = new AttributeReader(); + $registry = new Registry($this->dbal); + + (new Entities(new TokenizerEntityLocator(new ClassList(...$classes), $reader), $reader))->run($registry); + + return $registry; + } +} diff --git a/tests/Feature/Common/Inheritance/TableInheritanceTest.php b/tests/Feature/Common/Inheritance/TableInheritanceTest.php new file mode 100644 index 00000000..130c5d4f --- /dev/null +++ b/tests/Feature/Common/Inheritance/TableInheritanceTest.php @@ -0,0 +1,55 @@ +register(Person::class, Employee::class, Executive::class, Tool::class); + + $id = $registry->getEntity('executive')->getFields()->get('foo_id'); + Assert::same($id->getType(), 'integer'); + Assert::true($id->isPrimary()); + } + + public function placesASingleTableChildIntoTheDatabaseOfItsParent(): void + { + $registry = $this->register(Vehicle::class, Car::class); + + Assert::same($registry->getDatabase($registry->getEntity('car')), 'secondary'); + } + + /** + * @param class-string ...$classes + */ + private function register(string ...$classes): Registry + { + $reader = new AttributeReader(); + $registry = new Registry($this->dbal); + + (new Entities(new TokenizerEntityLocator(new ClassList(...$classes), $reader), $reader))->run($registry); + (new TableInheritance($reader))->run($registry); + + return $registry; + } +} diff --git a/tests/Fixtures/Fixtures29/Animal.php b/tests/Fixtures/Fixtures29/Animal.php new file mode 100644 index 00000000..e49eaf6a --- /dev/null +++ b/tests/Fixtures/Fixtures29/Animal.php @@ -0,0 +1,15 @@ + Date: Fri, 20 Nov 2026 21:06:15 +0400 Subject: [PATCH 4/5] test: cover merging columns and indexes from related classes Assisted-By: Claude Opus 5.5 --- tests/Feature/Common/MergeColumnsTest.php | 98 +++++++++++++++++++ tests/Feature/Common/MergeIndexesTest.php | 83 ++++++++++++++++ tests/Fixtures/Fixtures30/ChildEntity.php | 12 +++ tests/Fixtures/Fixtures30/CompositeKey.php | 10 ++ .../Fixtures/Fixtures30/GrandParentEntity.php | 12 +++ tests/Fixtures/Fixtures30/Named.php | 10 ++ tests/Fixtures/Fixtures30/ParentEntity.php | 12 +++ tests/Fixtures/Fixtures30/Subject.php | 7 ++ tests/Fixtures/Fixtures30/SubjectMapper.php | 12 +++ .../Fixtures/Fixtures30/SubjectRepository.php | 12 +++ tests/Fixtures/Fixtures30/SubjectScope.php | 12 +++ tests/Fixtures/Fixtures30/SubjectSource.php | 12 +++ 12 files changed, 292 insertions(+) create mode 100644 tests/Feature/Common/MergeColumnsTest.php create mode 100644 tests/Feature/Common/MergeIndexesTest.php create mode 100644 tests/Fixtures/Fixtures30/ChildEntity.php create mode 100644 tests/Fixtures/Fixtures30/CompositeKey.php create mode 100644 tests/Fixtures/Fixtures30/GrandParentEntity.php create mode 100644 tests/Fixtures/Fixtures30/Named.php create mode 100644 tests/Fixtures/Fixtures30/ParentEntity.php create mode 100644 tests/Fixtures/Fixtures30/Subject.php create mode 100644 tests/Fixtures/Fixtures30/SubjectMapper.php create mode 100644 tests/Fixtures/Fixtures30/SubjectRepository.php create mode 100644 tests/Fixtures/Fixtures30/SubjectScope.php create mode 100644 tests/Fixtures/Fixtures30/SubjectSource.php diff --git a/tests/Feature/Common/MergeColumnsTest.php b/tests/Feature/Common/MergeColumnsTest.php new file mode 100644 index 00000000..c9eb2db0 --- /dev/null +++ b/tests/Feature/Common/MergeColumnsTest.php @@ -0,0 +1,98 @@ +dbal); + $entity = $this->entity($registry, 'subject', Subject::class, table: 'subjects'); + $entity->setMapper(SubjectMapper::class); + $entity->setRepository(SubjectRepository::class); + $entity->setSource(SubjectSource::class); + $entity->setScope(SubjectScope::class); + + (new MergeColumns(new AttributeReader()))->run($registry); + + Assert::same($entity->getFields()->getNames(), ['fromMapper', 'fromRepository', 'fromSource', 'fromScope']); + } + + public function copiesColumnsDeclaredOnEveryParentEntity(): void + { + $registry = new Registry($this->dbal); + $entity = $this->entity($registry, 'child', ChildEntity::class, table: 'children'); + + (new MergeColumns(new AttributeReader()))->run($registry); + + Assert::array($entity->getFields()->getNames())->sameElementsAs(['fromParent', 'fromGrandParent']); + } + + public function findsParentsWithTheGivenUtils(): void + { + $registry = new Registry($this->dbal); + $entity = $this->entity($registry, 'child', ChildEntity::class, table: 'children'); + + (new MergeColumns(new AttributeReader(), new EntityUtils(new AnnotationReader())))->run($registry); + + Assert::same($entity->getFields()->getNames(), []); + } + + public function keepsAFieldTheEntityAlreadyHas(): void + { + $registry = new Registry($this->dbal); + $entity = $this->entity($registry, 'named', Named::class, table: 'named'); + $entity->getFields()->set('name', (new Field())->setType('string')->setColumn('name_column')); + + (new MergeColumns(new AttributeReader()))->run($registry); + + Assert::same($entity->getFields()->get('name')->getColumn(), 'name_column'); + } + + public function skipsEntitiesWithoutTable(): void + { + $registry = new Registry($this->dbal); + $withoutTable = $this->entity($registry, 'withoutTable', Named::class, table: null); + $withTable = $this->entity($registry, 'withTable', Named::class, table: 'named'); + + (new MergeColumns(new AttributeReader()))->run($registry); + + Assert::same($withoutTable->getFields()->getNames(), []); + Assert::same($withTable->getFields()->getNames(), ['name']); + } + + /** + * @param class-string $class + */ + private function entity(Registry $registry, string $role, string $class, ?string $table): Entity + { + $entity = (new Entity())->setRole($role)->setClass($class); + $registry->register($entity); + if ($table !== null) { + $registry->linkTable($entity, null, $table); + } + + return $entity; + } +} diff --git a/tests/Feature/Common/MergeIndexesTest.php b/tests/Feature/Common/MergeIndexesTest.php new file mode 100644 index 00000000..2fe3b803 --- /dev/null +++ b/tests/Feature/Common/MergeIndexesTest.php @@ -0,0 +1,83 @@ +dbal); + $entity = $this->entity($registry, 'subject', Subject::class); + $entity->setMapper(SubjectMapper::class); + $entity->setRepository(SubjectRepository::class); + $entity->setSource(SubjectSource::class); + $entity->setScope(SubjectScope::class); + foreach (['mapper', 'repository', 'source', 'scope'] as $name) { + $registry->getTableSchema($entity)->string($name . '_column'); + } + + (new MergeIndexes(new AttributeReader()))->run($registry); + + $table = $registry->getTableSchema($entity); + Assert::true($table->hasIndex(['mapper_column'])); + Assert::true($table->hasIndex(['repository_column'])); + Assert::true($table->hasIndex(['source_column'])); + Assert::true($table->hasIndex(['scope_column'])); + } + + public function rendersIndexesOfAChildIntoTheParentTable(): void + { + $registry = new Registry($this->dbal); + $parent = $this->entity($registry, 'subject', Subject::class); + $registry->registerChildWithoutMerge($parent, (new Entity())->setRole('child')->setClass(ChildEntity::class)); + $registry->getTableSchema($parent)->string('child_column'); + + (new MergeIndexes(new AttributeReader()))->run($registry); + + Assert::true($registry->getTableSchema($parent)->hasIndex(['child_column'])); + } + + public function setsThePrimaryColumnsOfTheEntity(): void + { + $registry = new Registry($this->dbal); + $entity = $this->entity($registry, 'compositeKey', CompositeKey::class); + $entity->getFields()->set('a', (new Field())->setType('integer')->setColumn('a')); + $entity->getFields()->set('b', (new Field())->setType('integer')->setColumn('b')); + + (new MergeIndexes(new AttributeReader()))->run($registry); + + Assert::same($entity->getPrimaryFields()->getNames(), ['a', 'b']); + Assert::same($registry->getTableSchema($entity)->getPrimaryKeys(), ['a', 'b']); + } + + /** + * @param class-string $class + */ + private function entity(Registry $registry, string $role, string $class): Entity + { + $entity = (new Entity())->setRole($role)->setClass($class); + $registry->register($entity); + $registry->linkTable($entity, null, $role); + + return $entity; + } +} diff --git a/tests/Fixtures/Fixtures30/ChildEntity.php b/tests/Fixtures/Fixtures30/ChildEntity.php new file mode 100644 index 00000000..60474fa2 --- /dev/null +++ b/tests/Fixtures/Fixtures30/ChildEntity.php @@ -0,0 +1,12 @@ + Date: Fri, 20 Nov 2026 21:06:16 +0400 Subject: [PATCH 5/5] ci: raise the mutation score gate The ignored mutants have no observable effect: a skipped relation property is always followed by more skipped ones, an explicit column name wins inside initField anyway, an empty column or index list is a no-op, the STI children map already holds class names, and an STI parent shares its table and primary key with the root. Assisted-By: Claude Opus 5.5 --- infection.json | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/infection.json b/infection.json index ccf5c2be..e10fef57 100644 --- a/infection.json +++ b/infection.json @@ -6,10 +6,36 @@ ] }, "timeout": 10, - "minMsi": 80, - "minCoveredMsi": 80, + "minMsi": 96, + "minCoveredMsi": 96, "testFramework": "testo", "tmpDir": "runtime", + "mutators": { + "@default": true, + "Coalesce": { + "ignore": ["Cycle\\Annotated\\Configurator::initColumns"] + }, + "Continue_": { + "ignore": ["Cycle\\Annotated\\Configurator::initRelations"] + }, + "FalseValue": { + "ignore": ["Cycle\\Annotated\\TableInheritance::getParentForForeignKey"] + }, + "ReturnRemoval": { + "ignore": [ + "Cycle\\Annotated\\MergeColumns::copy", + "Cycle\\Annotated\\MergeIndexes::render", + "Cycle\\Annotated\\TableInheritance::getParentForForeignKey" + ] + }, + "UnwrapArrayMap": { + "ignore": [ + "Cycle\\Annotated\\TableInheritance::run", + "Cycle\\Annotated\\TableInheritance::getTableName", + "Cycle\\Annotated\\TableInheritance::getDatabase" + ] + } + }, "logs": { "stryker": { "report": "4.x"