Skip to content

Commit 1575f99

Browse files
committed
Migrate some tests to table editor API
1 parent a9f633e commit 1575f99

3 files changed

Lines changed: 215 additions & 102 deletions

File tree

tests/Tests/ORM/Functional/DatabaseDriverTest.php

Lines changed: 156 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
99
use Doctrine\DBAL\Platforms\SQLServerPlatform;
1010
use Doctrine\DBAL\Schema\AbstractSchemaManager;
11+
use Doctrine\DBAL\Schema\Column;
12+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1113
use Doctrine\DBAL\Schema\Name\Identifier;
1214
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
1315
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1416
use Doctrine\DBAL\Schema\Table;
17+
use Doctrine\DBAL\Schema\TableEditor;
18+
use Doctrine\DBAL\Types\Type;
1519
use PHPUnit\Framework\Attributes\Group;
1620

1721
use function array_change_key_case;
@@ -23,8 +27,7 @@
2327

2428
class DatabaseDriverTest extends DatabaseDriverTestCase
2529
{
26-
/** @var AbstractSchemaManager */
27-
protected $schemaManager = null;
30+
protected AbstractSchemaManager|null $schemaManager = null;
2831

2932
protected function setUp(): void
3033
{
@@ -38,28 +41,47 @@ protected function setUp(): void
3841
#[Group('DDC-2059')]
3942
public function testIssue2059(): void
4043
{
41-
$user = new Table('ddc2059_user');
42-
$user->addColumn('id', 'integer');
44+
$user = new Table(
45+
'ddc2059_user',
46+
[new Column('id', Type::getType('integer'))],
47+
);
4348

44-
if (class_exists(PrimaryKeyConstraint::class)) {
45-
$user->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
46-
} else {
47-
$user->setPrimaryKey(['id']);
48-
}
49+
$project = new Table(
50+
'ddc2059_project',
51+
[
52+
new Column('id', Type::getType('integer')),
53+
new Column('user_id', Type::getType('integer')),
54+
new Column('user', Type::getType('string')),
4955

50-
$project = new Table('ddc2059_project');
51-
$project->addColumn('id', 'integer');
52-
$project->addColumn('user_id', 'integer');
53-
$project->addColumn('user', 'string');
56+
],
57+
);
5458

55-
if (class_exists(PrimaryKeyConstraint::class)) {
56-
$project->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
59+
if (class_exists(TableEditor::class)) {
60+
$user = $user->edit()
61+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
62+
null,
63+
[new UnqualifiedName(Identifier::unquoted('id'))],
64+
true,
65+
))
66+
->create();
67+
$project = $project->edit()
68+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
69+
null,
70+
[new UnqualifiedName(Identifier::unquoted('id'))],
71+
true,
72+
))
73+
->addForeignKeyConstraint(new ForeignKeyConstraint(
74+
['user_id'],
75+
'ddc2059_user',
76+
['id'],
77+
))
78+
->create();
5779
} else {
80+
$user->setPrimaryKey(['id']);
5881
$project->setPrimaryKey(['id']);
82+
$project->addForeignKeyConstraint('ddc2059_user', ['user_id'], ['id']);
5983
}
6084

61-
$project->addForeignKeyConstraint('ddc2059_user', ['user_id'], ['id']);
62-
6385
$metadata = $this->convertToClassMetadata([$project, $user], []);
6486

6587
self::assertTrue(isset($metadata['Ddc2059Project']->fieldMappings['user']));
@@ -68,17 +90,26 @@ public function testIssue2059(): void
6890

6991
public function testLoadMetadataFromDatabase(): void
7092
{
71-
$table = new Table('dbdriver_foo');
72-
$table->addColumn('id', 'integer');
93+
$table = new Table(
94+
'dbdriver_foo',
95+
[
96+
new Column('id', Type::getType('integer')),
97+
new Column('bar', Type::getType('string'), ['notnull' => false, 'length' => 200]),
98+
],
99+
);
73100

74-
if (class_exists(PrimaryKeyConstraint::class)) {
75-
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
101+
if (class_exists(TableEditor::class)) {
102+
$table = $table->edit()
103+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
104+
null,
105+
[new UnqualifiedName(Identifier::unquoted('id'))],
106+
true,
107+
))
108+
->create();
76109
} else {
77110
$table->setPrimaryKey(['id']);
78111
}
79112

80-
$table->addColumn('bar', 'string', ['notnull' => false, 'length' => 200]);
81-
82113
$this->dropAndCreateTable($table);
83114

84115
$metadatas = $this->extractClassMetadata(['DbdriverFoo']);
@@ -101,29 +132,48 @@ public function testLoadMetadataFromDatabase(): void
101132

102133
public function testLoadMetadataWithForeignKeyFromDatabase(): void
103134
{
104-
$tableB = new Table('dbdriver_bar');
105-
$tableB->addColumn('id', 'integer');
106-
107-
if (class_exists(PrimaryKeyConstraint::class)) {
108-
$tableB->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
109-
} else {
110-
$tableB->setPrimaryKey(['id']);
111-
}
112-
113-
$this->dropAndCreateTable($tableB);
135+
$tableB = new Table(
136+
'dbdriver_bar',
137+
[
138+
new Column('id', Type::getType('integer')),
139+
],
140+
);
114141

115-
$tableA = new Table('dbdriver_baz');
116-
$tableA->addColumn('id', 'integer');
142+
$tableA = new Table(
143+
'dbdriver_baz',
144+
[
145+
new Column('id', Type::getType('integer')),
146+
new Column('bar_id', Type::getType('integer')),
147+
],
148+
);
117149

118-
if (class_exists(PrimaryKeyConstraint::class)) {
119-
$tableA->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
150+
if (class_exists(TableEditor::class)) {
151+
$tableB = $tableB->edit()
152+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
153+
null,
154+
[new UnqualifiedName(Identifier::unquoted('id'))],
155+
true,
156+
))
157+
->create();
158+
$tableA = $tableA->edit()
159+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
160+
null,
161+
[new UnqualifiedName(Identifier::unquoted('id'))],
162+
true,
163+
))
164+
->addForeignKeyConstraint(new ForeignKeyConstraint(
165+
['bar_id'],
166+
'dbdriver_bar',
167+
['id'],
168+
))
169+
->create();
120170
} else {
171+
$tableB->setPrimaryKey(['id']);
121172
$tableA->setPrimaryKey(['id']);
173+
$tableA->addForeignKeyConstraint('dbdriver_bar', ['bar_id'], ['id']);
122174
}
123175

124-
$tableA->addColumn('bar_id', 'integer');
125-
$tableA->addForeignKeyConstraint('dbdriver_bar', ['bar_id'], ['id']);
126-
176+
$this->dropAndCreateTable($tableB);
127177
$this->dropAndCreateTable($tableA);
128178

129179
$metadatas = $this->extractClassMetadata(['DbdriverBar', 'DbdriverBaz']);
@@ -158,59 +208,94 @@ public function testDetectManyToManyTables(): void
158208

159209
public function testIgnoreManyToManyTableWithoutFurtherForeignKeyDetails(): void
160210
{
161-
$tableB = new Table('dbdriver_bar');
162-
$tableB->addColumn('id', 'integer');
211+
$tableB = new Table(
212+
'dbdriver_bar',
213+
[new Column('id', Type::getType('integer'))],
214+
);
215+
216+
$tableA = new Table(
217+
'dbdriver_baz',
218+
[
219+
new Column('id', Type::getType('integer')),
220+
],
221+
);
163222

164223
if (class_exists(PrimaryKeyConstraint::class)) {
165-
$tableB->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
224+
$tableB = $tableB->edit()
225+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
226+
null,
227+
[new UnqualifiedName(Identifier::unquoted('id'))],
228+
true,
229+
))
230+
->create();
231+
$tableA = $tableA->edit()
232+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
233+
null,
234+
[new UnqualifiedName(Identifier::unquoted('id'))],
235+
true,
236+
))
237+
->create();
166238
} else {
167239
$tableB->setPrimaryKey(['id']);
240+
$tableA->setPrimaryKey(['id']);
168241
}
169242

170-
$tableA = new Table('dbdriver_baz');
171-
$tableA->addColumn('id', 'integer');
172-
173-
if (class_exists(PrimaryKeyConstraint::class)) {
174-
$tableA->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
243+
$tableMany = new Table(
244+
'dbdriver_bar_baz',
245+
[
246+
new Column('bar_id', Type::getType('integer')),
247+
new Column('baz_id', Type::getType('integer')),
248+
],
249+
);
250+
if (class_exists(TableEditor::class)) {
251+
$tableMany = $tableMany->edit()
252+
->addForeignKeyConstraint(new ForeignKeyConstraint(
253+
['bar_id'],
254+
'dbdriver_bar',
255+
['id'],
256+
))
257+
->create();
175258
} else {
176-
$tableA->setPrimaryKey(['id']);
259+
$tableMany->addForeignKeyConstraint('dbdriver_bar', ['bar_id'], ['id']);
177260
}
178261

179-
$tableMany = new Table('dbdriver_bar_baz');
180-
$tableMany->addColumn('bar_id', 'integer');
181-
$tableMany->addColumn('baz_id', 'integer');
182-
$tableMany->addForeignKeyConstraint('dbdriver_bar', ['bar_id'], ['id']);
183-
184262
$metadatas = $this->convertToClassMetadata([$tableA, $tableB], [$tableMany]);
185263

186264
self::assertEquals(0, count($metadatas['DbdriverBaz']->associationMappings), 'no association mappings should be detected.');
187265
}
188266

189267
public function testLoadMetadataFromDatabaseDetail(): void
190268
{
191-
$table = new Table('dbdriver_foo');
269+
$table = new Table(
270+
'dbdriver_foo',
271+
[
272+
new Column('id', Type::getType('integer'), ['unsigned' => true]),
273+
new Column('column_unsigned', Type::getType('integer'), ['unsigned' => true]),
274+
new Column('column_comment', Type::getType('string'), ['length' => 16, 'comment' => 'test_comment']),
275+
new Column('column_default', Type::getType('string'), ['length' => 16, 'default' => 'test_default']),
276+
new Column('column_decimal', Type::getType('decimal'), ['precision' => 4, 'scale' => 3]),
277+
new Column('column_index1', Type::getType('string'), ['length' => 16]),
278+
new Column('column_index2', Type::getType('string'), ['length' => 16]),
279+
new Column('column_unique_index1', Type::getType('string'), ['length' => 16]),
280+
new Column('column_unique_index2', Type::getType('string'), ['length' => 16]),
281+
],
282+
);
192283

193-
$table->addColumn('id', 'integer', ['unsigned' => true]);
284+
$table->addIndex(['column_index1', 'column_index2'], 'index1');
285+
$table->addUniqueIndex(['column_unique_index1', 'column_unique_index2'], 'unique_index1');
194286

195-
if (class_exists(PrimaryKeyConstraint::class)) {
196-
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
287+
if (class_exists(TableEditor::class)) {
288+
$table = $table->edit()
289+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(
290+
null,
291+
[new UnqualifiedName(Identifier::unquoted('id'))],
292+
true,
293+
))
294+
->create();
197295
} else {
198296
$table->setPrimaryKey(['id']);
199297
}
200298

201-
$table->addColumn('column_unsigned', 'integer', ['unsigned' => true]);
202-
$table->addColumn('column_comment', 'string', ['length' => 16, 'comment' => 'test_comment']);
203-
$table->addColumn('column_default', 'string', ['length' => 16, 'default' => 'test_default']);
204-
$table->addColumn('column_decimal', 'decimal', ['precision' => 4, 'scale' => 3]);
205-
206-
$table->addColumn('column_index1', 'string', ['length' => 16]);
207-
$table->addColumn('column_index2', 'string', ['length' => 16]);
208-
$table->addIndex(['column_index1', 'column_index2'], 'index1');
209-
210-
$table->addColumn('column_unique_index1', 'string', ['length' => 16]);
211-
$table->addColumn('column_unique_index2', 'string', ['length' => 16]);
212-
$table->addUniqueIndex(['column_unique_index1', 'column_unique_index2'], 'unique_index1');
213-
214299
$this->dropAndCreateTable($table);
215300

216301
$metadatas = $this->extractClassMetadata(['DbdriverFoo']);

tests/Tests/ORM/Functional/Ticket/DDC2387Test.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
namespace Doctrine\Tests\ORM\Functional\Ticket;
66

7+
use Doctrine\DBAL\Schema\Column;
8+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
79
use Doctrine\DBAL\Schema\Name\Identifier;
810
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
911
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1012
use Doctrine\DBAL\Schema\Table;
13+
use Doctrine\DBAL\Schema\TableEditor;
14+
use Doctrine\DBAL\Types\Type;
1115
use Doctrine\ORM\Mapping\ClassMetadata;
1216
use Doctrine\Tests\ORM\Functional\DatabaseDriverTestCase;
1317
use PHPUnit\Framework\Attributes\Group;
@@ -19,27 +23,37 @@ class DDC2387Test extends DatabaseDriverTestCase
1923
#[Group('DDC-2387')]
2024
public function testCompositeAssociationKeyDetection(): void
2125
{
22-
$product = new Table('ddc2387_product');
23-
$product->addColumn('id', 'integer');
24-
25-
if (class_exists(PrimaryKeyConstraint::class)) {
26-
$product->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
26+
$product = new Table(
27+
'ddc2387_product',
28+
[new Column('id', Type::getType('integer'))],
29+
);
30+
31+
$attributes = new Table(
32+
'ddc2387_attributes',
33+
[
34+
new Column('product_id', Type::getType('integer')),
35+
new Column('attribute_name', Type::getType('string')),
36+
],
37+
);
38+
39+
if (class_exists(TableEditor::class)) {
40+
$product = $product->edit()
41+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true))
42+
->create();
43+
$attributes = $attributes->edit()
44+
->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('product_id')), new UnqualifiedName(Identifier::unquoted('attribute_name'))], true))
45+
->addForeignKeyConstraint(new ForeignKeyConstraint(
46+
['product_id'],
47+
'ddc2387_product',
48+
['product_id'],
49+
))
50+
->create();
2751
} else {
2852
$product->setPrimaryKey(['id']);
29-
}
30-
31-
$attributes = new Table('ddc2387_attributes');
32-
$attributes->addColumn('product_id', 'integer');
33-
$attributes->addColumn('attribute_name', 'string');
34-
35-
if (class_exists(PrimaryKeyConstraint::class)) {
36-
$attributes->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('product_id')), new UnqualifiedName(Identifier::unquoted('attribute_name'))], true));
37-
} else {
3853
$attributes->setPrimaryKey(['product_id', 'attribute_name']);
54+
$attributes->addForeignKeyConstraint('ddc2387_product', ['product_id'], ['product_id']);
3955
}
4056

41-
$attributes->addForeignKeyConstraint('ddc2387_product', ['product_id'], ['product_id']);
42-
4357
$metadata = $this->convertToClassMetadata([$product, $attributes], []);
4458

4559
self::assertEquals(ClassMetadata::GENERATOR_TYPE_NONE, $metadata['Ddc2387Attributes']->generatorType);

0 commit comments

Comments
 (0)