Skip to content

Commit 8da78c1

Browse files
committed
Add test with inherited relation
1 parent eba2dba commit 8da78c1

File tree

6 files changed

+82
-4
lines changed

6 files changed

+82
-4
lines changed

src/Configurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function initFields(EntitySchema $entity, \ReflectionClass $class, string
120120
public function initRelations(EntitySchema $entity, \ReflectionClass $class): void
121121
{
122122
foreach ($class->getProperties() as $property) {
123-
// ignore properties declared by parent entties
123+
// ignore properties declared by parent entities
124124
// otherwise all the relation columns declared in parent would be duplicated across all child tables in JTI
125125
if ($this->findOwningEntity($class, $property->getDeclaringClass())->getName() !== $class->getName()) {
126126
continue;

tests/Annotated/Fixtures/Fixtures16/Executive.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cycle\Annotated\Annotation\Column;
88
use Cycle\Annotated\Annotation\Entity;
99
use Cycle\Annotated\Annotation\Inheritance\JoinedTable as InheritanceJoinedTable;
10+
use Cycle\Annotated\Annotation\Relation\HasOne;
1011

1112
/**
1213
* @Entity
@@ -21,4 +22,12 @@ class Executive extends ExecutiveProxy
2122
/** @Column(type="int") */
2223
#[Column(type: 'int')]
2324
public int $bonus;
25+
26+
/** @Column(type="int", nullable=true, typecast="int") */
27+
#[Column(type: 'int', nullable: true, typecast: 'int')]
28+
public ?int $added_tool_id;
29+
30+
/** @HasOne(target=Tool::class, innerKey="added_tool_id", outerKey="added_tool_id", nullable=true) */
31+
#[HasOne(target: Tool::class, innerKey: 'added_tool_id', outerKey: 'added_tool_id', nullable: true)]
32+
public Tool $addedTool;
2433
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Fixtures\Fixtures16;
6+
7+
use Cycle\Annotated\Annotation\Entity;
8+
use Cycle\Annotated\Annotation\Inheritance\JoinedTable as InheritanceJoinedTable;
9+
10+
/**
11+
* @Entity
12+
* @InheritanceJoinedTable(outerKey="foo_id")
13+
*/
14+
#[Entity]
15+
#[InheritanceJoinedTable(outerKey: 'foo_id')]
16+
class Executive2 extends Executive
17+
{
18+
}

tests/Annotated/Fixtures/Fixtures16/Person.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cycle\Annotated\Annotation\Column;
88
use Cycle\Annotated\Annotation\Entity;
99
use Cycle\Annotated\Annotation\Inheritance\DiscriminatorColumn;
10+
use Cycle\Annotated\Annotation\Relation\HasOne;
1011

1112
/**
1213
* @Entity
@@ -28,6 +29,14 @@ class Person
2829
#[Column(type: 'string')]
2930
public string $type;
3031

32+
/** @Column(type="int", nullable=true, typecast="int") */
33+
#[Column(type: 'int', nullable: true, typecast: 'int')]
34+
public ?int $tool_id;
35+
36+
/** @HasOne(target=Tool::class, innerKey="id", outerKey="tool_id", nullable=true) */
37+
#[HasOne(target: Tool::class, innerKey: 'id', outerKey: 'tool_id', nullable: true)]
38+
public Tool $tool;
39+
3140
public function getFooId(): int
3241
{
3342
return $this->foo_id;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Fixtures\Fixtures16;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Inheritance\DiscriminatorColumn;
10+
11+
/**
12+
* @Entity
13+
* @DiscriminatorColumn(name="type")
14+
*/
15+
#[Entity]
16+
#[DiscriminatorColumn(name: 'type')]
17+
class Tool
18+
{
19+
/** @Column(type="primary", name="id") */
20+
#[Column(type: 'primary', name: 'id')]
21+
public int $tool_id;
22+
}

tests/Annotated/Functional/Driver/Common/InheritanceTestCase.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Employee;
1717
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Executive;
1818
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Person;
19+
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Tool;
1920
use Cycle\ORM\SchemaInterface;
2021
use Cycle\Schema\Compiler;
2122
use Cycle\Schema\Generator\GenerateRelations;
@@ -69,6 +70,9 @@ public function testTableInheritance(ReaderInterface $reader): void
6970
// Ceo - Single table inheritance {value: ceo}
7071
// Beaver - Separate table
7172

73+
// Tool
74+
$this->assertArrayHasKey('tool', $schema);
75+
7276
// Person
7377
$this->assertCount(3, $schema['person'][SchemaInterface::CHILDREN]);
7478
$this->assertEquals([
@@ -86,57 +90,72 @@ public function testTableInheritance(ReaderInterface $reader): void
8690
// 'bonus' => 'bonus', // JTI
8791
'preferences' => 'preferences',
8892
'stocks' => 'stocks',
93+
'tool_id' => 'tool_id',
8994
// 'teethAmount' => 'teeth_amount', // Not child
9095
], $schema['person'][SchemaInterface::COLUMNS]);
9196
$this->assertEmpty($schema['person'][SchemaInterface::PARENT] ?? null);
9297
$this->assertEmpty($schema['person'][SchemaInterface::PARENT_KEY] ?? null);
9398
$this->assertSame('people', $schema['person'][SchemaInterface::TABLE]);
99+
$this->assertCount(1, $schema['person'][SchemaInterface::RELATIONS]);
94100

95101
// Employee
96102
$this->assertArrayHasKey('employee', $schema);
97103
$this->assertCount(1, $schema['employee']);
98104
$this->assertSame(Employee::class, $schema['employee'][SchemaInterface::ENTITY]);
99105
$this->assertNull($schema['employee'][SchemaInterface::TABLE] ?? null);
106+
$this->assertCount(0, $schema['employee'][SchemaInterface::RELATIONS] ?? []);
100107

101108
// Customer
102109
$this->assertArrayHasKey('customer', $schema);
103110
$this->assertCount(1, $schema['customer']);
104111
$this->assertSame(Customer::class, $schema['customer'][SchemaInterface::ENTITY]);
105112
$this->assertNull($schema['customer'][SchemaInterface::TABLE] ?? null);
113+
$this->assertCount(0, $schema['customer'][SchemaInterface::RELATIONS] ?? []);
106114

107115
// Executive
108116
$this->assertSame('employee', $schema['executive'][SchemaInterface::PARENT]);
109117
$this->assertSame('foo_id', $schema['executive'][SchemaInterface::PARENT_KEY]);
110118
$this->assertSame('executives', $schema['executive'][SchemaInterface::TABLE]);
111-
$this->assertSame(
119+
$this->assertEquals(
112120
[
113121
'bonus' => 'bonus',
114122
'proxyFieldWithAnnotation' => 'proxy',
115123
'foo_id' => 'id',
116124
'hidden' => 'hidden',
125+
'added_tool_id' => 'added_tool_id',
117126
],
118-
$schema['executive'][SchemaInterface::COLUMNS]
127+
$schema['executive'][SchemaInterface::COLUMNS],
119128
);
129+
$this->assertCount(1, $schema['executive'][SchemaInterface::RELATIONS]);
130+
131+
// Executive2
132+
$this->assertSame('executive', $schema['executive2'][SchemaInterface::PARENT]);
133+
$this->assertSame('foo_id', $schema['executive2'][SchemaInterface::PARENT_KEY]);
134+
$this->assertEquals(['foo_id' => 'id'], $schema['executive2'][SchemaInterface::COLUMNS]);
135+
$this->assertCount(0, $schema['executive2'][SchemaInterface::RELATIONS]);
120136

121137
// Ceo
122138
$this->assertArrayHasKey('ceo', $schema);
123139
$this->assertCount(1, $schema['ceo']);
124140
$this->assertSame(Ceo::class, $schema['ceo'][SchemaInterface::ENTITY]);
125141
$this->assertNull($schema['ceo'][SchemaInterface::TABLE] ?? null);
142+
$this->assertCount(0, $schema['ceo'][SchemaInterface::RELATIONS] ?? []);
126143

127144
// Beaver
128145
$this->assertEmpty($schema['beaver'][SchemaInterface::DISCRIMINATOR] ?? null);
129146
$this->assertEmpty($schema['beaver'][SchemaInterface::PARENT] ?? null);
130147
$this->assertEmpty($schema['beaver'][SchemaInterface::PARENT_KEY] ?? null);
131148
$this->assertEmpty($schema['beaver'][SchemaInterface::CHILDREN] ?? null);
132149
$this->assertSame('beavers', $schema['beaver'][SchemaInterface::TABLE]);
133-
$this->assertSame([
150+
$this->assertEquals([
134151
'teethAmount' => 'teeth_amount',
135152
'foo_id' => 'id',
136153
'name' => 'name',
137154
'type' => 'type',
138155
'hidden' => 'hidden',
156+
'tool_id' => 'tool_id',
139157
], $schema['beaver'][SchemaInterface::COLUMNS]);
158+
$this->assertCount(1, $schema['beaver'][SchemaInterface::RELATIONS] ?? []);
140159
}
141160

142161
public function testTableInheritanceWithIncorrectClassesOrder(): void
@@ -150,6 +169,7 @@ public function testTableInheritanceWithIncorrectClassesOrder(): void
150169
new \ReflectionClass(Employee::class),
151170
new \ReflectionClass(Executive::class),
152171
new \ReflectionClass(Person::class),
172+
new \ReflectionClass(Tool::class),
153173
]);
154174

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

0 commit comments

Comments
 (0)