forked from doctrine/dbal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaManagerTest.php
More file actions
123 lines (103 loc) · 4.37 KB
/
SchemaManagerTest.php
File metadata and controls
123 lines (103 loc) · 4.37 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
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional\Schema;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;
use function array_merge;
final class SchemaManagerTest extends FunctionalTestCase
{
private AbstractSchemaManager $schemaManager;
/** @throws Exception */
protected function setUp(): void
{
$this->schemaManager = $this->connection->createSchemaManager();
}
/**
* @param callable(AbstractSchemaManager):Comparator $comparatorFactory
*
* @dataProvider dataEmptyDiffRegardlessOfForeignTableQuotes
*/
public function testEmptyDiffRegardlessOfForeignTableQuotes(
callable $comparatorFactory,
string $foreignTableName
): void {
if (! $this->connection->getDatabasePlatform()->supportsSchemas()) {
self::markTestSkipped('Platform does not support schemas.');
}
$this->dropAndCreateSchema('other_schema');
$tableForeign = new Table($foreignTableName);
$tableForeign->addColumn('id', 'integer');
$tableForeign->setPrimaryKey(['id']);
$this->dropAndCreateTable($tableForeign);
$tableTo = new Table('other_schema.other_table');
$tableTo->addColumn('id', 'integer');
$tableTo->addColumn('user_id', 'integer');
$tableTo->setPrimaryKey(['id']);
$tableTo->addForeignKeyConstraint($tableForeign, ['user_id'], ['id']);
$this->dropAndCreateTable($tableTo);
$schemaFrom = $this->schemaManager->introspectSchema();
$tableFrom = $schemaFrom->getTable('other_schema.other_table');
$diff = $comparatorFactory($this->schemaManager)->compareTables($tableFrom, $tableTo);
self::assertTrue($diff->isEmpty());
}
/** @return iterable<mixed[]> */
public static function dataEmptyDiffRegardlessOfForeignTableQuotes(): iterable
{
foreach (ComparatorTestUtils::comparatorProvider() as $comparatorArguments) {
foreach (
[
'unquoted' => ['other_schema.user'],
'partially quoted' => ['other_schema."user"'],
'fully quoted' => ['"other_schema"."user"'],
] as $testArguments
) {
yield array_merge($comparatorArguments, $testArguments);
}
}
}
/**
* @param callable(AbstractSchemaManager):Comparator $comparatorFactory
*
* @dataProvider dataDropIndexInAnotherSchema
*/
public function testDropIndexInAnotherSchema(callable $comparatorFactory, string $tableName): void
{
if (! $this->connection->getDatabasePlatform()->supportsSchemas()) {
self::markTestSkipped('Platform does not support schemas.');
}
$this->dropAndCreateSchema('other_schema');
$this->dropAndCreateSchema('case');
$tableFrom = new Table($tableName);
$tableFrom->addColumn('id', Types::INTEGER);
$tableFrom->addColumn('name', Types::STRING);
$tableFrom->addUniqueIndex(['name'], 'some_table_name_unique_index');
$this->dropAndCreateTable($tableFrom);
$tableTo = clone $tableFrom;
$tableTo->dropIndex('some_table_name_unique_index');
$diff = $comparatorFactory($this->schemaManager)->compareTables($tableFrom, $tableTo);
self::assertNotFalse($diff);
$this->schemaManager->alterTable($diff);
$tableFinal = $this->schemaManager->introspectTable($tableName);
self::assertEmpty($tableFinal->getIndexes());
}
/** @return iterable<mixed[]> */
public static function dataDropIndexInAnotherSchema(): iterable
{
foreach (ComparatorTestUtils::comparatorProvider() as $comparatorScenario => $comparatorArguments) {
foreach (
[
'default schema' => ['some_table'],
'unquoted schema' => ['other_schema.some_table'],
'quoted schema' => ['"other_schema".some_table'],
'reserved schema' => ['case.some_table'],
] as $testScenario => $testArguments
) {
yield $comparatorScenario . ' - ' . $testScenario => array_merge($comparatorArguments, $testArguments);
}
}
}
}