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
23 changes: 23 additions & 0 deletions tests/Fixtures/Configurator/Draft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Configurator;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\ForeignKey;

#[Entity]
class Draft
{
#[Column(type: 'primary')]
public int $id;

#[Column(type: 'integer')]
#[ForeignKey(target: Author::class)]
public int $editorId;

#[TargetlessRelation]
public ?Author $reviewer = null;
}
32 changes: 32 additions & 0 deletions tests/Fixtures/Configurator/TargetlessRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Configurator;

use Cycle\Annotated\Annotation\Relation\Inverse;
use Cycle\Annotated\Annotation\Relation\RelationInterface;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
final class TargetlessRelation implements RelationInterface
{
public function getType(): string
{
return 'hasOne';
}

public function getTarget(): ?string
{
return null;
}

public function getOptions(): array
{
return [];
}

public function getInverse(): ?Inverse
{
return null;
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/ForeignKey/Customer.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\ForeignKey;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;

#[Entity(role: 'customer')]
class Customer
{
#[Column(type: 'primary', name: 'customer_id')]
public int $id;
}
20 changes: 20 additions & 0 deletions tests/Fixtures/ForeignKey/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\ForeignKey;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\ForeignKey;

#[Entity(role: 'order')]
#[ForeignKey(target: Customer::class, innerKey: 'customerId')]
class Order
{
#[Column(type: 'primary')]
public int $id;

#[Column(type: 'integer', name: 'customer_ref')]
public int $customerId;
}
42 changes: 42 additions & 0 deletions tests/Fixtures/Locator/FailingPropertyReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Locator;

use Spiral\Attributes\Reader;

/**
* Fails to read the metadata of any property.
*/
final class FailingPropertyReader 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 [];
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/TableInheritance/Animal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\TableInheritance;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;

#[Entity(role: 'animal', table: 'animals')]
class Animal
{
#[Column(type: 'bigPrimary')]
public int $id;

#[Column(type: 'string')]
public string $name;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/TableInheritance/Bird.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\TableInheritance;

use Cycle\Annotated\Annotation\Entity;

#[Entity(role: 'bird', table: 'birds')]
#[Hybrid]
class Bird extends Animal {}
12 changes: 12 additions & 0 deletions tests/Fixtures/TableInheritance/Cat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\TableInheritance;

use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Inheritance\JoinedTable;

#[Entity(role: 'cat', table: 'cats')]
#[JoinedTable(outerKey: 'missing')]
class Cat extends Animal {}
17 changes: 17 additions & 0 deletions tests/Fixtures/TableInheritance/Dog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\TableInheritance;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Inheritance\JoinedTable;

#[Entity(role: 'dog', table: 'dogs')]
#[JoinedTable]
class Dog extends Animal
{
#[Column(type: 'string')]
public string $breed;
}
12 changes: 12 additions & 0 deletions tests/Fixtures/TableInheritance/Fish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\TableInheritance;

use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Inheritance\SingleTable;

#[Entity(role: 'fish')]
#[SingleTable]
class Fish extends Animal {}
19 changes: 19 additions & 0 deletions tests/Fixtures/TableInheritance/Hybrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\TableInheritance;

use Cycle\Annotated\Annotation\Inheritance;

/**
* An inheritance type that only a custom generator knows how to handle.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final class Hybrid extends Inheritance
{
public function __construct()
{
parent::__construct('hybrid');
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/TableInheritance/Puppy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\TableInheritance;

use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Inheritance\SingleTable;

#[Entity(role: 'puppy')]
#[SingleTable]
class Puppy extends Dog {}
40 changes: 40 additions & 0 deletions tests/Unit/Attribute/AnnotationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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<RelationAnnotation> $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');
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Attribute/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Loading
Loading