Skip to content

Commit 6d18f2d

Browse files
committed
Add test for other prefixes
1 parent aabaf2f commit 6d18f2d

1 file changed

Lines changed: 52 additions & 19 deletions

File tree

tests/system/Database/Live/MetadataTest.php

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,75 @@ class MetadataTest extends CIDatabaseTestCase
1111

1212
protected $seed = 'Tests\Support\Database\Seeds\CITestSeeder';
1313

14-
public function testCanListTables()
14+
/**
15+
* Array of expected tables.
16+
*
17+
* @var array
18+
*/
19+
protected $expectedTables;
20+
21+
protected function setUp()
1522
{
16-
$prefix = $this->db->getPRefix();
17-
$expected = [
23+
parent::setUp();
24+
25+
// Prepare the array of expected tables once
26+
$prefix = $this->db->getPrefix();
27+
$this->expectedTables = [
1828
$prefix . 'migrations',
1929
$prefix . 'user',
2030
$prefix . 'job',
2131
$prefix . 'misc',
2232
$prefix . 'empty',
2333
$prefix . 'secondary'
2434
];
25-
35+
}
36+
37+
public function testListTables()
38+
{
2639
$result = $this->db->listTables();
27-
28-
$this->assertEquals($expected, $result);
40+
41+
$this->assertEquals($this->expectedTables, array_values($result));
2942
}
3043

3144
//--------------------------------------------------------------------
3245

33-
public function testCanListTablesConstrainPrefix()
46+
public function testListTablesConstrainPrefix()
3447
{
35-
$prefix = $this->db->getPRefix();
36-
$expected = [
37-
$prefix . 'migrations',
38-
$prefix . 'user',
39-
$prefix . 'job',
40-
$prefix . 'misc',
41-
$prefix . 'empty',
42-
$prefix . 'secondary'
48+
$result = $this->db->listTables(true);
49+
50+
$this->assertEquals($this->expectedTables, array_values($result));
51+
}
52+
53+
//--------------------------------------------------------------------
54+
55+
public function testConstrainPrefixIgnoresOtherTables()
56+
{
57+
$this->forge = \Config\Database::forge($this->DBGroup);
58+
59+
// Stash the prefix and change it
60+
$DBPrefix = $this->db->getPrefix();
61+
$this->db->setPrefix('tmp_');
62+
63+
// Create a table with the new prefix
64+
$fields = [
65+
'name' => ['type' => 'varchar', 'constraint' => 31],
66+
'created_at' => ['type' => 'datetime', 'null' => true],
4367
];
44-
68+
$this->forge->addField($fields);
69+
$this->forge->createTable('widgets');
70+
71+
// Restore the prefix and get the tables with the original prefix
72+
$this->db->setPrefix($DBPrefix);
4573
$result = $this->db->listTables(true);
46-
47-
$this->assertEquals($expected, $result);
74+
75+
$this->assertEquals($expected, array_values($result));
76+
77+
// Clean up temporary table
78+
$this->db->setPrefix('tmp_');
79+
$forge->dropTable('widgets');
80+
$this->db->setPrefix($DBPrefix);
4881
}
49-
82+
5083
//--------------------------------------------------------------------
5184

5285
}

0 commit comments

Comments
 (0)