From 677ed96dedf9821835c24143fcf555e6c10aade7 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sat, 21 Nov 2026 00:25:04 +0400 Subject: [PATCH 1/3] test(annotation): construct every relation attribute in the Unit suite The Unit coverage run never loaded these attribute classes, so Codecov kept counting their constructor lines from static analysis as missed. Assisted-By: Claude Opus 5.5 --- tests/Unit/Attribute/AnnotationsTest.php | 40 ++++++++++++++++++++++++ tests/Unit/Attribute/ColumnTest.php | 8 +++++ 2 files changed, 48 insertions(+) diff --git a/tests/Unit/Attribute/AnnotationsTest.php b/tests/Unit/Attribute/AnnotationsTest.php index 2671a840..170b5219 100644 --- a/tests/Unit/Attribute/AnnotationsTest.php +++ b/tests/Unit/Attribute/AnnotationsTest.php @@ -8,20 +8,39 @@ use Cycle\Annotated\Annotation\Embeddable; use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\GeneratedValue; +use Cycle\Annotated\Annotation\Inheritance\DiscriminatorColumn; use Cycle\Annotated\Annotation\Inheritance\JoinedTable; use Cycle\Annotated\Annotation\Inheritance\SingleTable; use Cycle\Annotated\Annotation\Relation\HasOne; use Cycle\Annotated\Annotation\Relation\Inverse; +use Cycle\Annotated\Annotation\Relation\ManyToMany; +use Cycle\Annotated\Annotation\Relation\Morphed\BelongsToMorphed; +use Cycle\Annotated\Annotation\Relation\Morphed\MorphedHasMany; +use Cycle\Annotated\Annotation\Relation\Morphed\MorphedHasOne; +use Cycle\Annotated\Annotation\Relation\Morphed\RefersToMorphed; +use Cycle\Annotated\Annotation\Relation\RefersTo; +use Cycle\Annotated\Annotation\Relation\Relation as RelationAnnotation; use Cycle\Annotated\Annotation\Table\PrimaryKey; use Cycle\ORM\Relation; use Cycle\ORM\Schema\GeneratedField; use Testo\Assert; +use Testo\Data\DataProvider; use Testo\Data\DataSet; use Testo\Test; #[Test] final class AnnotationsTest { + public static function relationsWithInverse(): iterable + { + yield 'many to many' => [ManyToMany::class, ['through' => 'postTag'], 'manyToMany']; + yield 'refers to' => [RefersTo::class, [], 'refersTo']; + yield 'morphed has one' => [MorphedHasOne::class, [], 'morphedHasOne']; + yield 'morphed has many' => [MorphedHasMany::class, [], 'morphedHasMany']; + yield 'belongs to morphed' => [BelongsToMorphed::class, [], 'belongsToMorphed']; + yield 'refers to morphed' => [RefersToMorphed::class, [], 'refersToMorphed']; + } + public function entityKeepsSeveralTypecastHandlers(): void { $entity = new Entity(typecast: ['Foo', 'Bar']); @@ -94,4 +113,25 @@ public function relationLoadsLazilyByDefault(): void Assert::same((new HasOne(target: 'user'))->getLoad(), 'lazy'); Assert::same((new HasOne(target: 'user', load: 'eager'))->getLoad(), 'eager'); } + + /** + * @param class-string $class + */ + #[DataProvider('relationsWithInverse')] + public function relationKeepsItsConstructorArguments(string $class, array $arguments, string $type): void + { + $inverse = new Inverse(as: 'owner', type: 'belongsTo'); + + $relation = new $class(...$arguments, target: 'target', load: 'eager', inverse: $inverse); + + Assert::same($relation->getType(), $type); + Assert::same($relation->getLoad(), 'eager'); + Assert::same($relation->getInverse(), $inverse); + Assert::array($relation->getOptions())->doesNotHaveKeys('target', 'inverse'); + } + + public function discriminatorColumnKeepsItsName(): void + { + Assert::same((new DiscriminatorColumn(name: 'kind'))->getName(), 'kind'); + } } diff --git a/tests/Unit/Attribute/ColumnTest.php b/tests/Unit/Attribute/ColumnTest.php index 8c43f88c..70150ebf 100644 --- a/tests/Unit/Attribute/ColumnTest.php +++ b/tests/Unit/Attribute/ColumnTest.php @@ -161,6 +161,14 @@ public function testValuesOfANonEnumColumnStayAnAttribute(): void Assert::same($column->getAttributes(), ['values' => ['a', 'b']]); } + public function testEnumValuesTakenFromTheCasesOfAnEnumInstance(): void + { + $column = new Column(type: 'enum', values: StringEnum::B); + + Assert::same($column->getType(), 'enum(a,b)'); + Assert::array($column->getAttributes())->doesNotHaveKeys('values'); + } + public function testEnumValuesThatAreNotScalarAreLeftEmpty(): void { $column = new Column(type: 'enum', values: ['a', new StringableObject('b'), 1]); From ff7a1b0b38c7bc6dff916ebbe73f938496350732 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sat, 21 Nov 2026 00:25:05 +0400 Subject: [PATCH 2/3] test(configurator): cover the readonly schema, foreign key and reader failure paths Assisted-By: Claude Opus 5.5 --- tests/Fixtures/Configurator/Draft.php | 23 +++++ .../Configurator/TargetlessRelation.php | 32 +++++++ tests/Unit/ConfiguratorTest.php | 86 +++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 tests/Fixtures/Configurator/Draft.php create mode 100644 tests/Fixtures/Configurator/TargetlessRelation.php diff --git a/tests/Fixtures/Configurator/Draft.php b/tests/Fixtures/Configurator/Draft.php new file mode 100644 index 00000000..eddf095d --- /dev/null +++ b/tests/Fixtures/Configurator/Draft.php @@ -0,0 +1,23 @@ +getInnerColumns(), ['author_ref']); } + public function marksTheSchemaOfAReadonlyEntity(): void + { + $entity = $this->configurator()->initEntity(new Entity(readonlySchema: true), new \ReflectionClass(Post::class)); + + Assert::true($entity->getOptions()->get(SyncTables::READONLY_SCHEMA)); + } + + public function requiresTheTargetOfARelation(): never + { + $entity = new EntitySchema(); + $entity->setClass(Draft::class); + + Expect::exception(AnnotationException::class) + ->withMessage(\sprintf('Relation target definition is required on `%s`.`reviewer`', Draft::class)); + + $this->configurator()->initRelations($entity, new \ReflectionClass(Draft::class)); + } + + public function wrapsAnExceptionOfTheReaderWhileReadingRelations(): never + { + $entity = new EntitySchema(); + $entity->setClass(Post::class); + + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(7); + + (new Configurator($this->throwingReader(new \RuntimeException('broken', 7)))) + ->initRelations($entity, new \ReflectionClass(Post::class)); + } + + public function takesTheInnerKeyOfAPropertyForeignKeyFromTheProperty(): void + { + $entity = new EntitySchema(); + $entity->setClass(Draft::class); + + $this->configurator()->initForeignKeys(new Entity(), $entity, new \ReflectionClass(Draft::class)); + + $keys = \iterator_to_array($entity->getForeignKeys(), false); + Assert::count($keys, 1); + Assert::same($keys[0]->getInnerColumns(), ['editorId']); + } + + public function requiresTheInnerKeyOfAnEntityForeignKey(): never + { + $entity = new EntitySchema(); + $entity->setClass(Post::class); + + Expect::exception(AnnotationException::class) + ->withMessage(\sprintf('Inner column definition for the foreign key is required on `%s`', Post::class)); + + $this->configurator()->initForeignKeys( + new Entity(foreignKeys: [new ForeignKey(target: Author::class)]), + $entity, + new \ReflectionClass(\stdClass::class), + ); + } + + public function wrapsAnExceptionOfTheReaderWhileReadingClassForeignKeys(): never + { + $entity = new EntitySchema(); + $entity->setClass(Post::class); + + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(5); + + (new Configurator(new FailingClassReader(new \RuntimeException('broken', 5)))) + ->initForeignKeys(new Entity(), $entity, new \ReflectionClass(Post::class)); + } + + public function wrapsAnExceptionOfTheReaderWhileReadingGeneratedFields(): never + { + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(3); + + (new Configurator($this->throwingReader(new \RuntimeException('broken', 3)))) + ->initGeneratedFields(new EntitySchema(), new \ReflectionClass(Post::class)); + } + + public function resolvesATypecastWrittenAsClassAndMethod(): void + { + $field = $this->field(new Column(type: 'string', typecast: 'Sub/Typecaster::cast')); + + Assert::same($field->getTypecast(), [SubTypecaster::class, 'cast']); + } + public function makesANullableColumnDefaultToNull(): void { $field = $this->field(new Column(type: 'string', nullable: true)); From 7a15568ff597d9345e46722615573e9c1983c406 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sat, 21 Nov 2026 00:25:06 +0400 Subject: [PATCH 3/3] test(generators): run the schema generators in the Unit suite TableInheritance, MergeColumns and MergeIndexes ran only in the SQLite suite, so the Unit coverage report counted their lines from static analysis, including assert() and multi-line arguments that pcov never reports, and Codecov kept them missed. Assisted-By: Claude Opus 5.5 --- tests/Fixtures/ForeignKey/Customer.php | 15 +++ tests/Fixtures/ForeignKey/Order.php | 20 ++++ .../Locator/FailingPropertyReader.php | 42 +++++++ tests/Fixtures/TableInheritance/Animal.php | 18 +++ tests/Fixtures/TableInheritance/Bird.php | 11 ++ tests/Fixtures/TableInheritance/Cat.php | 12 ++ tests/Fixtures/TableInheritance/Dog.php | 17 +++ tests/Fixtures/TableInheritance/Fish.php | 12 ++ tests/Fixtures/TableInheritance/Hybrid.php | 19 +++ tests/Fixtures/TableInheritance/Puppy.php | 12 ++ tests/Unit/EmbeddingsTest.php | 21 ++++ tests/Unit/EntitiesTest.php | 34 ++++++ tests/Unit/MergeColumnsTest.php | 54 +++++++++ tests/Unit/MergeIndexesTest.php | 57 +++++++++ tests/Unit/TableInheritanceTest.php | 110 ++++++++++++++++++ 15 files changed, 454 insertions(+) create mode 100644 tests/Fixtures/ForeignKey/Customer.php create mode 100644 tests/Fixtures/ForeignKey/Order.php create mode 100644 tests/Fixtures/Locator/FailingPropertyReader.php create mode 100644 tests/Fixtures/TableInheritance/Animal.php create mode 100644 tests/Fixtures/TableInheritance/Bird.php create mode 100644 tests/Fixtures/TableInheritance/Cat.php create mode 100644 tests/Fixtures/TableInheritance/Dog.php create mode 100644 tests/Fixtures/TableInheritance/Fish.php create mode 100644 tests/Fixtures/TableInheritance/Hybrid.php create mode 100644 tests/Fixtures/TableInheritance/Puppy.php create mode 100644 tests/Unit/EntitiesTest.php create mode 100644 tests/Unit/MergeColumnsTest.php create mode 100644 tests/Unit/MergeIndexesTest.php create mode 100644 tests/Unit/TableInheritanceTest.php diff --git a/tests/Fixtures/ForeignKey/Customer.php b/tests/Fixtures/ForeignKey/Customer.php new file mode 100644 index 00000000..e16ea4fc --- /dev/null +++ b/tests/Fixtures/ForeignKey/Customer.php @@ -0,0 +1,15 @@ +error; + } + + public function getConstantMetadata(\ReflectionClassConstant $constant, ?string $name = null): iterable + { + return []; + } + + public function getParameterMetadata(\ReflectionParameter $parameter, ?string $name = null): iterable + { + return []; + } +} diff --git a/tests/Fixtures/TableInheritance/Animal.php b/tests/Fixtures/TableInheritance/Animal.php new file mode 100644 index 00000000..28034378 --- /dev/null +++ b/tests/Fixtures/TableInheritance/Animal.php @@ -0,0 +1,18 @@ +run(new Registry(new DatabaseManager(new DatabaseConfig()))); } + + public function wrapsAnExceptionOfTheReader(): never + { + $previous = new \RuntimeException('broken', 42); + $locator = new class implements EmbeddingLocatorInterface { + public function getEmbeddings(): array + { + return [new Embedding(new Embeddable(), new \ReflectionClass(PostalAddress::class))]; + } + }; + $embeddings = new Embeddings($locator, new FailingPropertyReader($previous)); + + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(42)->withPrevious($previous); + + $embeddings->run(new Registry(new DatabaseManager(new DatabaseConfig()))); + } } diff --git a/tests/Unit/EntitiesTest.php b/tests/Unit/EntitiesTest.php new file mode 100644 index 00000000..68e47248 --- /dev/null +++ b/tests/Unit/EntitiesTest.php @@ -0,0 +1,34 @@ +run(new Registry(Memory::manager())); + + $keys = \iterator_to_array($registry->getEntity('order')->getForeignKeys(), false); + Assert::count($keys, 1); + Assert::same($keys[0]->getTarget(), 'customer'); + Assert::same($keys[0]->getInnerColumns(), ['customer_ref']); + Assert::same($keys[0]->getOuterColumns(), ['customer_id']); + } +} diff --git a/tests/Unit/MergeColumnsTest.php b/tests/Unit/MergeColumnsTest.php new file mode 100644 index 00000000..dc7f0e56 --- /dev/null +++ b/tests/Unit/MergeColumnsTest.php @@ -0,0 +1,54 @@ +subject($registry); + $entity->setMapper('Cycle\Annotated\Tests\Fixtures\Fixtures30\MissingMapper'); + $entity->setRepository(SubjectRepository::class); + + (new MergeColumns(new AttributeReader()))->run($registry); + + Assert::same($entity->getFields()->getNames(), ['fromRepository']); + } + + public function wrapsAnExceptionOfTheReader(): never + { + $registry = new Registry(Memory::manager()); + $this->subject($registry); + $previous = new \RuntimeException('broken', 42); + + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(42)->withPrevious($previous); + + (new MergeColumns(new FailingClassReader($previous)))->run($registry); + } + + private function subject(Registry $registry): Entity + { + $entity = (new Entity())->setRole('subject')->setClass(Subject::class); + $registry->register($entity); + $registry->linkTable($entity, null, 'subjects'); + + return $entity; + } +} diff --git a/tests/Unit/MergeIndexesTest.php b/tests/Unit/MergeIndexesTest.php new file mode 100644 index 00000000..404487d1 --- /dev/null +++ b/tests/Unit/MergeIndexesTest.php @@ -0,0 +1,57 @@ +subject($registry); + $entity->setMapper('Cycle\Annotated\Tests\Fixtures\Fixtures30\MissingMapper'); + $entity->setRepository(SubjectRepository::class); + $registry->getTableSchema($entity)->string('repository_column'); + + (new MergeIndexes(new AttributeReader()))->run($registry); + + $indexes = $registry->getTableSchema($entity)->getIndexes(); + Assert::count($indexes, 1); + Assert::same(\reset($indexes)->getColumns(), ['repository_column']); + } + + public function wrapsAnExceptionOfTheReader(): never + { + $registry = new Registry(Memory::manager()); + $this->subject($registry); + $previous = new \RuntimeException('broken', 42); + + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(42)->withPrevious($previous); + + (new MergeIndexes(new FailingClassReader($previous)))->run($registry); + } + + private function subject(Registry $registry): Entity + { + $entity = (new Entity())->setRole('subject')->setClass(Subject::class); + $registry->register($entity); + $registry->linkTable($entity, null, 'subjects'); + + return $entity; + } +} diff --git a/tests/Unit/TableInheritanceTest.php b/tests/Unit/TableInheritanceTest.php new file mode 100644 index 00000000..227707fd --- /dev/null +++ b/tests/Unit/TableInheritanceTest.php @@ -0,0 +1,110 @@ +entities(Animal::class, Dog::class); + + (new TableInheritance(new AttributeReader()))->run($registry); + + $id = $registry->getEntity('dog')->getFields()->get('id'); + Assert::same($id->getType(), 'bigInteger'); + Assert::true($id->isPrimary()); + Assert::false($registry->getEntity('dog')->getFields()->has('name')); + } + + public function rejectsAnOuterKeyTheParentDoesNotHave(): never + { + $registry = $this->entities(Animal::class, Cat::class); + + Expect::exception(WrongParentKeyColumnException::class); + + (new TableInheritance(new AttributeReader()))->run($registry); + } + + public function leavesAnUnknownInheritanceTypeToOtherGenerators(): void + { + $registry = $this->entities(Animal::class, Bird::class); + + (new TableInheritance(new AttributeReader()))->run($registry); + + $bird = $registry->getEntity('bird'); + Assert::null($bird->getInheritance()); + Assert::null($registry->getEntity('animal')->getInheritance()); + Assert::same($registry->getTable($bird), 'birds'); + } + + public function dropsFieldsOfASingleTableThatNoEntityOfTheHierarchyOwns(): void + { + $registry = $this->entities(Animal::class, Fish::class); + $animal = $registry->getEntity('animal'); + $animal->getFields()->set('foreign', (new Field())->setType('string')->setColumn('foreign')); + + (new TableInheritance(new AttributeReader()))->run($registry); + + Assert::false($animal->getFields()->has('foreign')); + Assert::true($animal->getFields()->has('name')); + } + + public function failsWhenTheParentEntityIsNotRegistered(): never + { + $registry = new Registry(Memory::manager()); + $animal = (new Entity())->setRole('animal')->setClass(Animal::class); + $registry->register($animal); + $registry->registerChildWithoutMerge($animal, (new Entity())->setRole('puppy')->setClass(Puppy::class)); + + Expect::exception(AnnotationException::class) + ->withMessage(\sprintf('The entity could not be found for the role `%s`.', Dog::class)); + + (new TableInheritance(new AttributeReader()))->run($registry); + } + + public function wrapsAnExceptionOfTheReader(): never + { + $registry = $this->entities(Animal::class, Dog::class); + $previous = new \RuntimeException('broken', 42); + + Expect::exception(AnnotationException::class)->withMessage('broken')->withCode(42)->withPrevious($previous); + + (new TableInheritance(new FailingClassReader($previous)))->run($registry); + } + + /** + * @param class-string ...$classes + */ + private function entities(string ...$classes): Registry + { + $reader = new AttributeReader(); + $registry = new Registry(Memory::manager()); + + return (new Entities(new TokenizerEntityLocator(new ClassList(...$classes), $reader), $reader))->run($registry); + } +}