Skip to content

Commit 1d1d527

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

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
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") */
27+
#[Column(type: 'int')]
28+
public int $added_tool_id;
29+
30+
/** @HasOne(target=Tool::class, innerKey="added_tool_id", outerKey="added_tool_id") */
31+
#[HasOne(target: Tool::class, innerKey: 'added_tool_id', outerKey: 'added_tool_id')]
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: 5 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,10 @@ class Person
2829
#[Column(type: 'string')]
2930
public string $type;
3031

32+
/** @HasOne(target=Tool::class, innerKey="id", outerKey="my_tool_id") */
33+
#[HasOne(target: Tool::class, innerKey: 'id', outerKey: 'my_tool_id')]
34+
public Tool $tool;
35+
3136
public function getFooId(): int
3237
{
3338
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: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function testTableInheritance(ReaderInterface $reader): void
6969
// Ceo - Single table inheritance {value: ceo}
7070
// Beaver - Separate table
7171

72+
// Tool
73+
$this->assertArrayHasKey('tool', $schema);
74+
7275
// Person
7376
$this->assertCount(3, $schema['person'][SchemaInterface::CHILDREN]);
7477
$this->assertEquals([
@@ -91,38 +94,62 @@ public function testTableInheritance(ReaderInterface $reader): void
9194
$this->assertEmpty($schema['person'][SchemaInterface::PARENT] ?? null);
9295
$this->assertEmpty($schema['person'][SchemaInterface::PARENT_KEY] ?? null);
9396
$this->assertSame('people', $schema['person'][SchemaInterface::TABLE]);
97+
$this->assertSame(
98+
[
99+
'foo_id' => 'id',
100+
'name' => 'name',
101+
'type' => 'type',
102+
'stocks' => 'stocks',
103+
'salary' => 'salary',
104+
'bar' => 'bar',
105+
'preferences' => 'preferences',
106+
],
107+
$schema['person'][SchemaInterface::COLUMNS],
108+
);
109+
$this->assertCount(1, $schema['person'][SchemaInterface::RELATIONS]);
94110

95111
// Employee
96112
$this->assertArrayHasKey('employee', $schema);
97113
$this->assertCount(1, $schema['employee']);
98114
$this->assertSame(Employee::class, $schema['employee'][SchemaInterface::ENTITY]);
99115
$this->assertNull($schema['employee'][SchemaInterface::TABLE] ?? null);
116+
$this->assertCount(0, $schema['employee'][SchemaInterface::RELATIONS] ?? []);
100117

101118
// Customer
102119
$this->assertArrayHasKey('customer', $schema);
103120
$this->assertCount(1, $schema['customer']);
104121
$this->assertSame(Customer::class, $schema['customer'][SchemaInterface::ENTITY]);
105122
$this->assertNull($schema['customer'][SchemaInterface::TABLE] ?? null);
123+
$this->assertCount(0, $schema['customer'][SchemaInterface::RELATIONS] ?? []);
106124

107125
// Executive
108126
$this->assertSame('employee', $schema['executive'][SchemaInterface::PARENT]);
109127
$this->assertSame('foo_id', $schema['executive'][SchemaInterface::PARENT_KEY]);
110128
$this->assertSame('executives', $schema['executive'][SchemaInterface::TABLE]);
111-
$this->assertSame(
129+
$this->assertEquals(
112130
[
113131
'bonus' => 'bonus',
114132
'proxyFieldWithAnnotation' => 'proxy',
115133
'foo_id' => 'id',
116134
'hidden' => 'hidden',
135+
'added_tool_id' => 'added_tool_id',
117136
],
118-
$schema['executive'][SchemaInterface::COLUMNS]
137+
$schema['executive'][SchemaInterface::COLUMNS],
119138
);
139+
$this->assertCount(1, $schema['executive'][SchemaInterface::RELATIONS]);
140+
141+
// Executive2
142+
$this->assertSame('executive', $schema['executive2'][SchemaInterface::PARENT]);
143+
$this->assertSame('foo_id', $schema['executive2'][SchemaInterface::PARENT_KEY]);
144+
$this->assertEquals(['foo_id' => 'id'], $schema['executive2'][SchemaInterface::COLUMNS]);
145+
$this->assertCount(0, $schema['executive2'][SchemaInterface::RELATIONS]);
120146

121147
// Ceo
122148
$this->assertArrayHasKey('ceo', $schema);
123149
$this->assertCount(1, $schema['ceo']);
124150
$this->assertSame(Ceo::class, $schema['ceo'][SchemaInterface::ENTITY]);
125151
$this->assertNull($schema['ceo'][SchemaInterface::TABLE] ?? null);
152+
$this->assertCount(0, $schema['ceo'][SchemaInterface::RELATIONS] ?? []);
126153

127154
// Beaver
128155
$this->assertEmpty($schema['beaver'][SchemaInterface::DISCRIMINATOR] ?? null);
@@ -137,6 +164,7 @@ public function testTableInheritance(ReaderInterface $reader): void
137164
'type' => 'type',
138165
'hidden' => 'hidden',
139166
], $schema['beaver'][SchemaInterface::COLUMNS]);
167+
$this->assertCount(1, $schema['beaver'][SchemaInterface::RELATIONS] ?? []);
140168
}
141169

142170
public function testTableInheritanceWithIncorrectClassesOrder(): void

0 commit comments

Comments
 (0)