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
42 changes: 42 additions & 0 deletions tests/Fixtures/Locator/FailingClassReader.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 class.
*/
final class FailingClassReader extends Reader
{
public function __construct(
private readonly \Throwable $error,
) {}

public function getClassMetadata(\ReflectionClass $class, ?string $name = null): iterable
{
throw $this->error;
}

public function getFunctionMetadata(\ReflectionFunctionAbstract $function, ?string $name = null): iterable
{
return [];
}

public function getPropertyMetadata(\ReflectionProperty $property, ?string $name = null): iterable
{
return [];
}

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/Unit/Attribute/AnnotationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Cycle\Annotated\Tests\Unit\Attribute;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Embeddable;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\GeneratedValue;
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\Table\PrimaryKey;
use Cycle\ORM\Relation;
Expand Down Expand Up @@ -76,4 +79,19 @@ public function primaryKeyIsAUniqueIndex(): void
Assert::true($key->isUnique());
Assert::same($key->getColumns(), ['id', 'type']);
}

public function embeddableKeepsItsColumns(): void
{
$columns = [new Column('string', property: 'street')];

$embeddable = new Embeddable(columns: $columns);

Assert::same($embeddable->getColumns(), $columns);
}

public function relationLoadsLazilyByDefault(): void
{
Assert::same((new HasOne(target: 'user'))->getLoad(), 'lazy');
Assert::same((new HasOne(target: 'user', load: 'eager'))->getLoad(), 'eager');
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Locator/TokenizerEmbeddingLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
namespace Cycle\Annotated\Tests\Unit\Locator;

use Cycle\Annotated\Annotation\Embeddable;
use Cycle\Annotated\Exception\AnnotationException;
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\Annotated\Tests\Fixtures\Locator\FailingClassReader;
use Cycle\Testing\ORM\Schema\ClassList;
use Testo\Assert;
use Testo\Data\DataProvider;
use Testo\Expect;
use Testo\Test;

#[Test]
Expand Down Expand Up @@ -61,4 +64,17 @@ public function testGetEmbeddings(array $expected, array $classes): void

Assert::equals($locator->getEmbeddings(), $expected);
}

public function wrapsAnExceptionOfTheReader(): never
{
$previous = new \RuntimeException('broken', 42);
$locator = new TokenizerEmbeddingLocator(new ClassList(Address::class), new FailingClassReader($previous));

Expect::exception(AnnotationException::class)
->withMessage('broken')
->withCode(42)
->withPrevious($previous);

$locator->getEmbeddings();
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Locator/TokenizerEntityLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
namespace Cycle\Annotated\Tests\Unit\Locator;

use Cycle\Annotated\Annotation\Entity as Attribute;
use Cycle\Annotated\Exception\AnnotationException;
use Cycle\Annotated\Locator\Entity;
use Cycle\Annotated\Locator\TokenizerEntityLocator;
use Cycle\Annotated\Tests\Fixtures\Fixtures1\AnotherClass;
use Cycle\Annotated\Tests\Fixtures\Fixtures1\Tag;
use Cycle\Annotated\Tests\Fixtures\Fixtures1\Typecast\Typecaster;
use Cycle\Annotated\Tests\Fixtures\Fixtures1\Typecast\UuidTypecaster;
use Cycle\Annotated\Tests\Fixtures\Fixtures1\WithTable;
use Cycle\Annotated\Tests\Fixtures\Locator\FailingClassReader;
use Cycle\Testing\ORM\Schema\ClassList;
use Testo\Assert;
use Testo\Data\DataProvider;
use Testo\Expect;
use Testo\Test;

#[Test]
Expand Down Expand Up @@ -51,4 +54,17 @@ public function testGetEntities(array $expected, array $classes): void

Assert::equals($locator->getEntities(), $expected);
}

public function wrapsAnExceptionOfTheReader(): never
{
$previous = new \RuntimeException('broken', 42);
$locator = new TokenizerEntityLocator(new ClassList(Tag::class), new FailingClassReader($previous));

Expect::exception(AnnotationException::class)
->withMessage('broken')
->withCode(42)
->withPrevious($previous);

$locator->getEntities();
}
}
Loading