Skip to content

Commit

Permalink
Add test with inherited relation
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 2, 2024
1 parent eba2dba commit 8da78c1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function initFields(EntitySchema $entity, \ReflectionClass $class, string
public function initRelations(EntitySchema $entity, \ReflectionClass $class): void
{
foreach ($class->getProperties() as $property) {
// ignore properties declared by parent entties
// ignore properties declared by parent entities
// otherwise all the relation columns declared in parent would be duplicated across all child tables in JTI
if ($this->findOwningEntity($class, $property->getDeclaringClass())->getName() !== $class->getName()) {
continue;
Expand Down
9 changes: 9 additions & 0 deletions tests/Annotated/Fixtures/Fixtures16/Executive.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Inheritance\JoinedTable as InheritanceJoinedTable;
use Cycle\Annotated\Annotation\Relation\HasOne;

/**
* @Entity
Expand All @@ -21,4 +22,12 @@ class Executive extends ExecutiveProxy
/** @Column(type="int") */
#[Column(type: 'int')]
public int $bonus;

/** @Column(type="int", nullable=true, typecast="int") */
#[Column(type: 'int', nullable: true, typecast: 'int')]
public ?int $added_tool_id;

/** @HasOne(target=Tool::class, innerKey="added_tool_id", outerKey="added_tool_id", nullable=true) */
#[HasOne(target: Tool::class, innerKey: 'added_tool_id', outerKey: 'added_tool_id', nullable: true)]
public Tool $addedTool;
}
18 changes: 18 additions & 0 deletions tests/Annotated/Fixtures/Fixtures16/Executive2.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\Fixtures16;

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

/**
* @Entity
* @InheritanceJoinedTable(outerKey="foo_id")
*/
#[Entity]
#[InheritanceJoinedTable(outerKey: 'foo_id')]
class Executive2 extends Executive
{
}
9 changes: 9 additions & 0 deletions tests/Annotated/Fixtures/Fixtures16/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Inheritance\DiscriminatorColumn;
use Cycle\Annotated\Annotation\Relation\HasOne;

/**
* @Entity
Expand All @@ -28,6 +29,14 @@ class Person
#[Column(type: 'string')]
public string $type;

/** @Column(type="int", nullable=true, typecast="int") */
#[Column(type: 'int', nullable: true, typecast: 'int')]
public ?int $tool_id;

/** @HasOne(target=Tool::class, innerKey="id", outerKey="tool_id", nullable=true) */
#[HasOne(target: Tool::class, innerKey: 'id', outerKey: 'tool_id', nullable: true)]
public Tool $tool;

public function getFooId(): int
{
return $this->foo_id;
Expand Down
22 changes: 22 additions & 0 deletions tests/Annotated/Fixtures/Fixtures16/Tool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Fixtures16;

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

/**
* @Entity
* @DiscriminatorColumn(name="type")
*/
#[Entity]
#[DiscriminatorColumn(name: 'type')]
class Tool
{
/** @Column(type="primary", name="id") */
#[Column(type: 'primary', name: 'id')]
public int $tool_id;
}
26 changes: 23 additions & 3 deletions tests/Annotated/Functional/Driver/Common/InheritanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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\ORM\SchemaInterface;
use Cycle\Schema\Compiler;
use Cycle\Schema\Generator\GenerateRelations;
Expand Down Expand Up @@ -69,6 +70,9 @@ public function testTableInheritance(ReaderInterface $reader): void
// Ceo - Single table inheritance {value: ceo}
// Beaver - Separate table

// Tool
$this->assertArrayHasKey('tool', $schema);

// Person
$this->assertCount(3, $schema['person'][SchemaInterface::CHILDREN]);
$this->assertEquals([
Expand All @@ -86,57 +90,72 @@ public function testTableInheritance(ReaderInterface $reader): void
// 'bonus' => 'bonus', // JTI
'preferences' => 'preferences',
'stocks' => 'stocks',
'tool_id' => 'tool_id',
// 'teethAmount' => 'teeth_amount', // Not child
], $schema['person'][SchemaInterface::COLUMNS]);
$this->assertEmpty($schema['person'][SchemaInterface::PARENT] ?? null);
$this->assertEmpty($schema['person'][SchemaInterface::PARENT_KEY] ?? null);
$this->assertSame('people', $schema['person'][SchemaInterface::TABLE]);
$this->assertCount(1, $schema['person'][SchemaInterface::RELATIONS]);

// Employee
$this->assertArrayHasKey('employee', $schema);
$this->assertCount(1, $schema['employee']);
$this->assertSame(Employee::class, $schema['employee'][SchemaInterface::ENTITY]);
$this->assertNull($schema['employee'][SchemaInterface::TABLE] ?? null);
$this->assertCount(0, $schema['employee'][SchemaInterface::RELATIONS] ?? []);

// Customer
$this->assertArrayHasKey('customer', $schema);
$this->assertCount(1, $schema['customer']);
$this->assertSame(Customer::class, $schema['customer'][SchemaInterface::ENTITY]);
$this->assertNull($schema['customer'][SchemaInterface::TABLE] ?? null);
$this->assertCount(0, $schema['customer'][SchemaInterface::RELATIONS] ?? []);

// Executive
$this->assertSame('employee', $schema['executive'][SchemaInterface::PARENT]);
$this->assertSame('foo_id', $schema['executive'][SchemaInterface::PARENT_KEY]);
$this->assertSame('executives', $schema['executive'][SchemaInterface::TABLE]);
$this->assertSame(
$this->assertEquals(
[
'bonus' => 'bonus',
'proxyFieldWithAnnotation' => 'proxy',
'foo_id' => 'id',
'hidden' => 'hidden',
'added_tool_id' => 'added_tool_id',
],
$schema['executive'][SchemaInterface::COLUMNS]
$schema['executive'][SchemaInterface::COLUMNS],
);
$this->assertCount(1, $schema['executive'][SchemaInterface::RELATIONS]);

// Executive2
$this->assertSame('executive', $schema['executive2'][SchemaInterface::PARENT]);
$this->assertSame('foo_id', $schema['executive2'][SchemaInterface::PARENT_KEY]);
$this->assertEquals(['foo_id' => 'id'], $schema['executive2'][SchemaInterface::COLUMNS]);
$this->assertCount(0, $schema['executive2'][SchemaInterface::RELATIONS]);

// Ceo
$this->assertArrayHasKey('ceo', $schema);
$this->assertCount(1, $schema['ceo']);
$this->assertSame(Ceo::class, $schema['ceo'][SchemaInterface::ENTITY]);
$this->assertNull($schema['ceo'][SchemaInterface::TABLE] ?? null);
$this->assertCount(0, $schema['ceo'][SchemaInterface::RELATIONS] ?? []);

// Beaver
$this->assertEmpty($schema['beaver'][SchemaInterface::DISCRIMINATOR] ?? null);
$this->assertEmpty($schema['beaver'][SchemaInterface::PARENT] ?? null);
$this->assertEmpty($schema['beaver'][SchemaInterface::PARENT_KEY] ?? null);
$this->assertEmpty($schema['beaver'][SchemaInterface::CHILDREN] ?? null);
$this->assertSame('beavers', $schema['beaver'][SchemaInterface::TABLE]);
$this->assertSame([
$this->assertEquals([
'teethAmount' => 'teeth_amount',
'foo_id' => 'id',
'name' => 'name',
'type' => 'type',
'hidden' => 'hidden',
'tool_id' => 'tool_id',
], $schema['beaver'][SchemaInterface::COLUMNS]);
$this->assertCount(1, $schema['beaver'][SchemaInterface::RELATIONS] ?? []);
}

public function testTableInheritanceWithIncorrectClassesOrder(): void
Expand All @@ -150,6 +169,7 @@ public function testTableInheritanceWithIncorrectClassesOrder(): void
new \ReflectionClass(Employee::class),
new \ReflectionClass(Executive::class),
new \ReflectionClass(Person::class),
new \ReflectionClass(Tool::class),
]);

$schema = (new Compiler())->compile($r, [
Expand Down

0 comments on commit 8da78c1

Please sign in to comment.