@@ -26,22 +26,22 @@ final class MetadataTest extends CIUnitTestCase
26
26
{
27
27
use DatabaseTestTrait;
28
28
29
- private ?Forge $ forge = null ;
30
- protected $ refresh = true ;
31
- protected $ seed = CITestSeeder::class;
32
-
33
29
/**
34
- * Array of expected tables.
30
+ * The seed file used for all tests within this test case.
31
+ *
32
+ * @var string
35
33
*/
36
- private array $ expectedTables ;
34
+ protected $ seed = CITestSeeder::class;
35
+
36
+ private array $ expectedTables = [];
37
37
38
38
protected function setUp (): void
39
39
{
40
40
parent ::setUp ();
41
41
42
- // Prepare the array of expected tables once
43
- $ prefix = $ this -> db -> getPrefix ();
44
- $ this -> expectedTables = [
42
+ $ prefix = $ this -> db -> getPrefix ();
43
+
44
+ $ tables = [
45
45
$ prefix . 'migrations ' ,
46
46
$ prefix . 'user ' ,
47
47
$ prefix . 'job ' ,
@@ -55,55 +55,90 @@ protected function setUp(): void
55
55
];
56
56
57
57
if (in_array ($ this ->db ->DBDriver , ['MySQLi ' , 'Postgre ' ], true )) {
58
- $ this -> expectedTables [] = $ prefix . 'ci_sessions ' ;
58
+ $ tables [] = $ prefix . 'ci_sessions ' ;
59
59
}
60
+
61
+ sort ($ tables );
62
+ $ this ->expectedTables = $ tables ;
60
63
}
61
64
62
- public function testListTables ()
65
+ private function createExtraneousTable (): void
63
66
{
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 ' );
65
76
66
- $ this ->assertSame ( $ this -> expectedTables , array_values ( $ result ) );
77
+ $ this ->db -> setPrefix ( $ oldPrefix );
67
78
}
68
79
69
- public function testListTablesConstrainPrefix ()
80
+ private function dropExtraneousTable (): void
70
81
{
71
- $ result = $ this ->db ->listTables (true );
82
+ $ oldPrefix = $ this ->db ->getPrefix ();
83
+ $ this ->db ->setPrefix ('tmp_ ' );
84
+
85
+ Database::forge ($ this ->DBGroup )->dropTable ('widgets ' );
72
86
73
- $ this ->assertSame ( $ this -> expectedTables , array_values ( $ result ) );
87
+ $ this ->db -> setPrefix ( $ oldPrefix );
74
88
}
75
89
76
- public function testConstrainPrefixIgnoresOtherTables ()
90
+ public function testListTablesUnconstrainedByPrefixReturnsAllTables ()
77
91
{
78
- $ this ->forge = Database::forge ($ this ->DBGroup );
92
+ try {
93
+ $ this ->createExtraneousTable ();
79
94
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 );
83
98
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 ' ;
97
101
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
+ }
101
108
102
- $ this ->assertSame ($ this ->expectedTables , array_values ($ result ));
109
+ public function testListTablesConstrainedByPrefixReturnsOnlyTablesWithMatchingPrefix ()
110
+ {
111
+ try {
112
+ $ this ->createExtraneousTable ();
103
113
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
+ }
108
143
}
109
144
}
0 commit comments