Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
87 changes: 87 additions & 0 deletions tests/Feature/Common/EntitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Feature\Common;

use Cycle\Annotated\Entities;
use Cycle\Annotated\Exception\AnnotationException;
use Cycle\Annotated\Locator\TokenizerEntityLocator;
use Cycle\Annotated\Tests\Feature\BaseTestCase;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\Animal;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\Author;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\Book;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\Dog;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\DogOwner;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\Review;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\UnicornOwner;
use Cycle\Schema\Exception\RelationException;
use Cycle\Schema\Registry;
use Cycle\Testing\ORM\Schema\ClassList;
use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\ReaderInterface;
use Testo\Assert;
use Testo\Data\DataProvider;
use Testo\Expect;
use Testo\Test;

#[Test]
final class EntitiesTest extends BaseTestCase
{
#[DataProvider('allReadersProvider')]
public function wrapsThroughKeysIntoArrays(ReaderInterface $reader): void
{
$registry = new Registry($this->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;
}
}
55 changes: 55 additions & 0 deletions tests/Feature/Common/Inheritance/TableInheritanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Feature\Common\Inheritance;

use Cycle\Annotated\Entities;
use Cycle\Annotated\Locator\TokenizerEntityLocator;
use Cycle\Annotated\TableInheritance;
use Cycle\Annotated\Tests\Feature\BaseTestCase;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Employee;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Executive;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Person;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Tool;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\Car;
use Cycle\Annotated\Tests\Fixtures\Fixtures29\Vehicle;
use Cycle\Schema\Registry;
use Cycle\Testing\ORM\Schema\ClassList;
use Spiral\Attributes\AttributeReader;
use Testo\Assert;
use Testo\Test;

#[Test]
final class TableInheritanceTest extends BaseTestCase
{
public function turnsTheInheritedPrimaryKeyOfAJoinedTableIntoAPlainColumn(): void
{
$registry = $this->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;
}
}
98 changes: 98 additions & 0 deletions tests/Feature/Common/MergeColumnsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Feature\Common;

use Cycle\Annotated\MergeColumns;
use Cycle\Annotated\Tests\Feature\BaseTestCase;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\ChildEntity;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\Named;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\Subject;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectMapper;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectRepository;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectScope;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectSource;
use Cycle\Annotated\Utils\EntityUtils;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Registry;
use Spiral\Attributes\AnnotationReader;
use Spiral\Attributes\AttributeReader;
use Testo\Assert;
use Testo\Test;

#[Test]
final class MergeColumnsTest extends BaseTestCase
{
public function copiesColumnsDeclaredOnRelatedClasses(): void
{
$registry = new Registry($this->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;
}
}
83 changes: 83 additions & 0 deletions tests/Feature/Common/MergeIndexesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Feature\Common;

use Cycle\Annotated\MergeIndexes;
use Cycle\Annotated\Tests\Feature\BaseTestCase;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\ChildEntity;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\CompositeKey;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\Subject;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectMapper;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectRepository;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectScope;
use Cycle\Annotated\Tests\Fixtures\Fixtures30\SubjectSource;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Registry;
use Spiral\Attributes\AttributeReader;
use Testo\Assert;
use Testo\Test;

#[Test]
final class MergeIndexesTest extends BaseTestCase
{
public function rendersIndexesDeclaredOnRelatedClasses(): void
{
$registry = new Registry($this->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;
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/Configurator/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Configurator;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Embeddable;

#[Embeddable(role: 'postal', mapper: 'AddressMapper', typecast: ['Typecaster', 'Sub/Typecaster'])]
class Address
{
#[Column(type: 'string')]
public string $street;
}
7 changes: 7 additions & 0 deletions tests/Fixtures/Configurator/AddressMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Configurator;

class AddressMapper {}
7 changes: 7 additions & 0 deletions tests/Fixtures/Configurator/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Configurator;

class Author {}
Loading
Loading