-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathJoinedTableTestCase.php
More file actions
233 lines (196 loc) · 8 KB
/
JoinedTableTestCase.php
File metadata and controls
233 lines (196 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
declare(strict_types=1);
namespace Cycle\Annotated\Tests\Functional\Driver\Common\Inheritance;
use Cycle\Annotated\Embeddings;
use Cycle\Annotated\Entities;
use Cycle\Annotated\Locator\TokenizerEmbeddingLocator;
use Cycle\Annotated\Locator\TokenizerEntityLocator;
use Cycle\Annotated\MergeColumns;
use Cycle\Annotated\MergeIndexes;
use Cycle\Annotated\TableInheritance;
use Cycle\Annotated\Tests\Fixtures\Fixtures16\Executive;
use Cycle\Annotated\Tests\Functional\Driver\Common\BaseTestCase;
use Cycle\Annotated\Tests\Traits\TableTrait;
use Cycle\Database\Schema\AbstractForeignKey;
use Cycle\Database\Schema\AbstractIndex;
use Cycle\ORM\EntityManager;
use Cycle\ORM\Schema;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Compiler;
use Cycle\Schema\Generator\GenerateRelations;
use Cycle\Schema\Generator\GenerateTypecast;
use Cycle\Schema\Generator\RenderRelations;
use Cycle\Schema\Generator\RenderTables;
use Cycle\Schema\Generator\ResetTables;
use Cycle\Schema\Generator\SyncTables;
use Cycle\Schema\Registry;
use PHPUnit\Framework\Attributes\DataProvider;
use Spiral\Attributes\ReaderInterface;
use Spiral\Tokenizer\Config\TokenizerConfig;
use Spiral\Tokenizer\Tokenizer;
abstract class JoinedTableTestCase extends BaseTestCase
{
use TableTrait;
public function setUp(): void
{
parent::setUp();
$this->makeTable(
table: 'people',
columns: [
'id' => 'primary',
'type' => 'string',
'name' => 'string',
'salary' => 'float',
'preferences' => 'string',
'bar' => 'string',
'stocks' => 'int',
]
);
$this->makeTable(
table: 'executives',
columns: [
'id' => 'int',
'bonus' => 'float',
'proxy' => 'string',
'hidden' => 'string',
],
pk: ['id']
);
$this->makeTable(
table: 'suppliers',
columns: [
'id' => 'int',
'custom_id' => 'int',
],
pk: ['id']
);
$this->makeTable(
table: 'external_suppliers',
columns: [
'id' => 'int',
],
pk: ['id']
);
$this->makeTable(
table: 'local_suppliers',
columns: [
'id' => 'int',
],
pk: ['id']
);
$this->makeTable(
table: 'buyers',
columns: [
'id' => 'int',
],
pk: ['id']
);
}
#[DataProvider('allReadersProvider')]
public function testTableInheritance(ReaderInterface $reader): void
{
$this->orm = $this->orm->with(new Schema($this->compile($reader)));
$em = new EntityManager($this->orm);
$executive = new Executive();
$executive->bonus = 15000;
$executive->name = 'executive';
$executive->salary = 50000;
$executive->hidden = 'secret';
$executive->type = 'executive';
$executive->proxyFieldWithAnnotation = 'value';
$em->persist($executive);
$em->run();
$this->orm->getHeap()->clean();
$loadedExecutive = $this->orm->getRepository(Executive::class)->findByPK($executive->getFooId());
$this->assertInstanceOf(Executive::class, $loadedExecutive);
$this->assertSame('executive', $loadedExecutive->getName());
$this->assertSame(50000, $loadedExecutive->getSalary());
$this->assertSame('secret', $loadedExecutive->hidden);
$this->assertSame(15000, $loadedExecutive->bonus);
$this->assertSame('executive', $loadedExecutive->getType());
$this->assertSame('value', $loadedExecutive->proxyFieldWithAnnotation);
}
#[DataProvider('allReadersProvider')]
public function testAddForeignKeys(ReaderInterface $reader): void
{
$this->orm = $this->orm->with(new Schema($this->compile($reader)));
$db = $this->dbal->database();
$suppliersFk = $db->table('suppliers')->getForeignKeys();
$executivesFk = $db->table('executives')->getForeignKeys();
$externalSuppliersFk = $db->table('external_suppliers')->getForeignKeys();
// simple case. Supplier -> Person
$this->assertCount(1, $suppliersFk);
$this->assertInstanceOf(AbstractForeignKey::class, reset($suppliersFk));
$this->assertSame('people', reset($suppliersFk)->getForeignTable());
$this->assertSame(['id'], reset($suppliersFk)->getForeignKeys());
$this->assertSame(['id'], reset($suppliersFk)->getColumns());
// Executive -> Employee (STI) -> Person
$this->assertCount(1, $executivesFk);
$this->assertInstanceOf(AbstractForeignKey::class, reset($executivesFk));
$this->assertSame('people', reset($executivesFk)->getForeignTable());
$this->assertSame(['id'], reset($executivesFk)->getForeignKeys());
$this->assertSame(['id'], reset($executivesFk)->getColumns());
// ExternalSupplier -> Supplier (JTI) -> Person. With outerKey
$this->assertCount(1, $externalSuppliersFk);
$this->assertInstanceOf(AbstractForeignKey::class, reset($externalSuppliersFk));
$this->assertSame('suppliers', reset($externalSuppliersFk)->getForeignTable());
$this->assertSame(['custom_id'], reset($externalSuppliersFk)->getForeignKeys());
$this->assertSame(['id'], reset($externalSuppliersFk)->getColumns());
}
#[DataProvider('allReadersProvider')]
public function testNotAddForeignKey(ReaderInterface $reader): void
{
$this->orm = $this->orm->with(new Schema($this->compile($reader)));
$this->assertCount(0, $this->dbal->database()->table('buyers')->getForeignKeys());
}
#[DataProvider('allReadersProvider')]
public function testAddIndex(ReaderInterface $reader): void
{
$this->orm = $this->orm->with(new Schema($this->compile($reader)));
$indexes = $this->dbal->database()->table('suppliers')->getIndexes();
// remove pk index
$indexes = array_filter($indexes, fn (AbstractIndex $index) => $index->getColumns() !== ['id']);
// one index added automatically, one added manual
$this->assertCount(2, $indexes);
$this->assertTrue($this->dbal->database()->table('suppliers')->hasIndex(['index_id']));
$this->assertTrue($this->dbal->database()->table('suppliers')->hasIndex(['custom_id']));
$this->assertTrue(reset($indexes)->isUnique());
$this->assertTrue(end($indexes)->isUnique());
}
#[DataProvider('allReadersProvider')]
public function testJtiParentColumns(ReaderInterface $reader): void
{
$schema = $this->compile($reader, 'Fixtures21');
$this->assertNotEmpty($schema);
$this->assertArrayHasKey(SchemaInterface::COLUMNS, $schema['person']);
// assert that parent doesn't have jti columns
$this->assertSame([
'id' => 'id',
'name' => 'name',
'type' => 'type',
], $schema['person'][SchemaInterface::COLUMNS]);
}
private function compile(ReaderInterface $reader, string $fixtures = 'Fixtures16'): array
{
$tokenizer = new Tokenizer(
new TokenizerConfig([
'directories' => [sprintf(__DIR__ . '/../../../../Fixtures/%s', $fixtures)],
'exclude' => [],
])
);
$locator = $tokenizer->classLocator();
return (new Compiler())->compile(new Registry($this->dbal), [
new ResetTables(),
new Embeddings(new TokenizerEmbeddingLocator($locator, $reader), $reader),
new Entities(new TokenizerEntityLocator($locator, $reader), $reader),
new TableInheritance($reader),
new MergeColumns($reader),
new GenerateRelations(),
new RenderTables(),
new RenderRelations(),
new MergeIndexes($reader),
new SyncTables(),
new GenerateTypecast(),
]);
}
}