Skip to content

Commit 2c72730

Browse files
committed
Refactor MetadataTest to improve the testing
1 parent 23aa26f commit 2c72730

File tree

1 file changed

+77
-42
lines changed

1 file changed

+77
-42
lines changed

tests/system/Database/Live/MetadataTest.php

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ final class MetadataTest extends CIUnitTestCase
2626
{
2727
use DatabaseTestTrait;
2828

29-
private ?Forge $forge = null;
30-
protected $refresh = true;
31-
protected $seed = CITestSeeder::class;
32-
3329
/**
34-
* Array of expected tables.
30+
* The seed file used for all tests within this test case.
31+
*
32+
* @var string
3533
*/
36-
private array $expectedTables;
34+
protected $seed = CITestSeeder::class;
35+
36+
private array $expectedTables = [];
3737

3838
protected function setUp(): void
3939
{
4040
parent::setUp();
4141

42-
// Prepare the array of expected tables once
43-
$prefix = $this->db->getPrefix();
44-
$this->expectedTables = [
42+
$prefix = $this->db->getPrefix();
43+
44+
$tables = [
4545
$prefix . 'migrations',
4646
$prefix . 'user',
4747
$prefix . 'job',
@@ -55,55 +55,90 @@ protected function setUp(): void
5555
];
5656

5757
if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre'], true)) {
58-
$this->expectedTables[] = $prefix . 'ci_sessions';
58+
$tables[] = $prefix . 'ci_sessions';
5959
}
60+
61+
sort($tables);
62+
$this->expectedTables = $tables;
6063
}
6164

62-
public function testListTables()
65+
private function createExtraneousTable(): void
6366
{
64-
$result = $this->db->listTables(true);
67+
$oldPrefix = $this->db->getPrefix();
68+
$this->db->setPrefix('tmp_');
69+
70+
Database::forge($this->DBGroup)
71+
->addField([
72+
'name' => ['type' => 'varchar', 'constraint' => 31],
73+
'created_at' => ['type' => 'datetime', 'null' => true],
74+
])
75+
->createTable('widgets');
6576

66-
$this->assertSame($this->expectedTables, array_values($result));
77+
$this->db->setPrefix($oldPrefix);
6778
}
6879

69-
public function testListTablesConstrainPrefix()
80+
private function dropExtraneousTable(): void
7081
{
71-
$result = $this->db->listTables(true);
82+
$oldPrefix = $this->db->getPrefix();
83+
$this->db->setPrefix('tmp_');
84+
85+
Database::forge($this->DBGroup)->dropTable('widgets');
7286

73-
$this->assertSame($this->expectedTables, array_values($result));
87+
$this->db->setPrefix($oldPrefix);
7488
}
7589

76-
public function testConstrainPrefixIgnoresOtherTables()
90+
public function testListTablesUnconstrainedByPrefixReturnsAllTables()
7791
{
78-
$this->forge = Database::forge($this->DBGroup);
92+
try {
93+
$this->createExtraneousTable();
7994

80-
// Stash the prefix and change it
81-
$DBPrefix = $this->db->getPrefix();
82-
$this->db->setPrefix('tmp_');
95+
$tables = $this->db->listTables();
96+
$this->assertIsArray($tables);
97+
$this->assertNotSame([], $tables);
8398

84-
// Create a table with the new prefix
85-
$fields = [
86-
'name' => [
87-
'type' => 'varchar',
88-
'constraint' => 31,
89-
],
90-
'created_at' => [
91-
'type' => 'datetime',
92-
'null' => true,
93-
],
94-
];
95-
$this->forge->addField($fields);
96-
$this->forge->createTable('widgets');
99+
$expectedTables = $this->expectedTables;
100+
$expectedTables[] = 'tmp_widgets';
97101

98-
// Restore the prefix and get the tables with the original prefix
99-
$this->db->setPrefix($DBPrefix);
100-
$result = $this->db->listTables(true);
102+
sort($tables);
103+
$this->assertSame($expectedTables, array_values($tables));
104+
} finally {
105+
$this->dropExtraneousTable();
106+
}
107+
}
101108

102-
$this->assertSame($this->expectedTables, array_values($result));
109+
public function testListTablesConstrainedByPrefixReturnsOnlyTablesWithMatchingPrefix()
110+
{
111+
try {
112+
$this->createExtraneousTable();
103113

104-
// Clean up temporary table
105-
$this->db->setPrefix('tmp_');
106-
$this->forge->dropTable('widgets');
107-
$this->db->setPrefix($DBPrefix);
114+
$tables = $this->db->listTables(true);
115+
$this->assertIsArray($tables);
116+
$this->assertNotSame([], $tables);
117+
118+
sort($tables);
119+
$this->assertSame($this->expectedTables, array_values($tables));
120+
} finally {
121+
$this->dropExtraneousTable();
122+
}
123+
}
124+
125+
public function testListTablesConstrainedByExtraneousPrefixReturnsOnlyTheExtraneousTable()
126+
{
127+
try {
128+
$this->createExtraneousTable();
129+
130+
$oldPrefix = $this->db->getPrefix();
131+
$this->db->setPrefix('tmp_');
132+
133+
$tables = $this->db->listTables(true);
134+
$this->assertIsArray($tables);
135+
$this->assertNotSame([], $tables);
136+
137+
sort($tables);
138+
$this->assertSame(['tmp_widgets'], array_values($tables));
139+
} finally {
140+
$this->db->setPrefix($oldPrefix);
141+
$this->dropExtraneousTable();
142+
}
108143
}
109144
}

0 commit comments

Comments
 (0)