generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DbSchemaManagerTest.php
155 lines (125 loc) · 4.81 KB
/
DbSchemaManagerTest.php
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
<?php
declare(strict_types=1);
namespace Yiisoft\Rbac\Cycle\Tests\Base;
use InvalidArgumentException;
use Yiisoft\Rbac\Cycle\DbSchemaManager;
abstract class DbSchemaManagerTest extends TestCase
{
use SchemaTrait;
protected function setUp(): void
{
// Skip
}
protected function tearDown(): void
{
if (str_starts_with($this->getName(), 'testInitWithEmptyTableNames')) {
return;
}
if ($this->getName() === 'testHasTableWithEmptyString' || $this->getName() === 'testDropTableWithEmptyString') {
return;
}
if (str_starts_with($this->getName(), 'testGet')) {
return;
}
parent::tearDown();
}
protected function populateDatabase(): void
{
// Skip
}
public function dataInitWithEmptyTableNames(): array
{
return [
[
['itemsTable' => null, 'itemsChildrenTable' => null, 'assignmentsTable' => null],
'At least items and items children table names or assignments table name must be set.',
],
[
['itemsTable' => 'items', 'itemsChildrenTable' => null, 'assignmentsTable' => null],
'Items and items children table names must be set together.',
],
[
['itemsTable' => null, 'itemsChildrenTable' => 'items_children', 'assignmentsTable' => null],
'Items and items children table names must be set together.',
],
[['itemsTable' => ''], 'Items table name can\'t be empty.'],
[['itemsChildrenTable' => ''], 'Items children table name can\'t be empty.'],
[['assignmentsTable' => ''], 'Assignments table name can\'t be empty.'],
];
}
/**
* @dataProvider dataInitWithEmptyTableNames
*/
public function testInitWithEmptyTableNames(array $tableNameArguments, string $expectedExceptionMessage): void
{
$arguments = ['database' => $this->getDatabase()];
$arguments = array_merge($tableNameArguments, $arguments);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
new DbSchemaManager(...$arguments);
}
public function testCreateTablesSeparately(): void
{
$schemaManager = $this->createSchemaManager();
$schemaManager->createItemsTable();
$schemaManager->createItemsChildrenTable();
$schemaManager->createAssignmentsTable();
$this->checkTables();
}
public function testEnsureTablesMultiple(): void
{
$schemaManager = $this->createSchemaManager();
$schemaManager->ensureTables();
$schemaManager->ensureTables();
$this->checkTables();
}
public function testCreateItemTables(): void
{
$schemaManager = $this->createSchemaManager(assignmentsTable: null);
$schemaManager->ensureTables();
$this->checkItemsTable();
$this->checkItemsChildrenTable();
$this->assertFalse($schemaManager->hasTable(DbSchemaManager::ASSIGNMENTS_TABLE));
}
public function testCreateAssignmentsTable(): void
{
$schemaManager = $this->createSchemaManager(itemsTable: null, itemsChildrenTable: null);
$schemaManager->ensureTables();
$this->checkAssignmentsTable();
$this->assertFalse($schemaManager->hasTable(DbSchemaManager::ITEMS_TABLE));
$this->assertFalse($schemaManager->hasTable(DbSchemaManager::ITEMS_CHILDREN_TABLE));
}
public function testHasTableWithEmptyString(): void
{
$schemaManager = $this->createSchemaManager();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Table name must be non-empty.');
$schemaManager->hasTable('');
}
public function testDropTableWithEmptyString(): void
{
$schemaManager = $this->createSchemaManager();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Table name must be non-empty.');
$schemaManager->dropTable('');
}
public function testGetItemsTable(): void
{
$this->assertSame('yii_rbac_item', $this->createSchemaManager()->getItemsTable());
}
public function testGetItemsChildrenTable(): void
{
$this->assertSame('yii_rbac_item_child', $this->createSchemaManager()->getItemsChildrenTable());
}
public function testGetAssignmentsTable(): void
{
$this->assertSame('yii_rbac_assignment', $this->createSchemaManager()->getAssignmentsTable());
}
public function testEnsureNoTables(): void
{
$schemaManager = $this->createSchemaManager();
$schemaManager->ensureTables();
$schemaManager->ensureNoTables();
$this->checkNoTables();
}
}