11
11
12
12
namespace CodeIgniter \Database \Live ;
13
13
14
- use CodeIgniter \Database \Forge ;
15
14
use CodeIgniter \Test \CIUnitTestCase ;
16
15
use CodeIgniter \Test \DatabaseTestTrait ;
17
16
use Config \Database ;
@@ -26,22 +25,22 @@ final class MetadataTest extends CIUnitTestCase
26
25
{
27
26
use DatabaseTestTrait;
28
27
29
- private ?Forge $ forge = null ;
30
- protected $ refresh = true ;
31
- protected $ seed = CITestSeeder::class;
32
-
33
28
/**
34
- * Array of expected tables.
29
+ * The seed file used for all tests within this test case.
30
+ *
31
+ * @var string
35
32
*/
36
- private array $ expectedTables ;
33
+ protected $ seed = CITestSeeder::class;
34
+
35
+ private array $ expectedTables = [];
37
36
38
37
protected function setUp (): void
39
38
{
40
39
parent ::setUp ();
41
40
42
- // Prepare the array of expected tables once
43
- $ prefix = $ this -> db -> getPrefix ();
44
- $ this -> expectedTables = [
41
+ $ prefix = $ this -> db -> getPrefix ();
42
+
43
+ $ tables = [
45
44
$ prefix . 'migrations ' ,
46
45
$ prefix . 'user ' ,
47
46
$ prefix . 'job ' ,
@@ -55,55 +54,90 @@ protected function setUp(): void
55
54
];
56
55
57
56
if (in_array ($ this ->db ->DBDriver , ['MySQLi ' , 'Postgre ' ], true )) {
58
- $ this -> expectedTables [] = $ prefix . 'ci_sessions ' ;
57
+ $ tables [] = $ prefix . 'ci_sessions ' ;
59
58
}
59
+
60
+ sort ($ tables );
61
+ $ this ->expectedTables = $ tables ;
60
62
}
61
63
62
- public function testListTables ()
64
+ private function createExtraneousTable (): void
63
65
{
64
- $ result = $ this ->db ->listTables (true );
66
+ $ oldPrefix = $ this ->db ->getPrefix ();
67
+ $ this ->db ->setPrefix ('tmp_ ' );
68
+
69
+ Database::forge ($ this ->DBGroup )
70
+ ->addField ([
71
+ 'name ' => ['type ' => 'varchar ' , 'constraint ' => 31 ],
72
+ 'created_at ' => ['type ' => 'datetime ' , 'null ' => true ],
73
+ ])
74
+ ->createTable ('widgets ' );
65
75
66
- $ this ->assertSame ( $ this -> expectedTables , array_values ( $ result ) );
76
+ $ this ->db -> setPrefix ( $ oldPrefix );
67
77
}
68
78
69
- public function testListTablesConstrainPrefix ()
79
+ private function dropExtraneousTable (): void
70
80
{
71
- $ result = $ this ->db ->listTables (true );
81
+ $ oldPrefix = $ this ->db ->getPrefix ();
82
+ $ this ->db ->setPrefix ('tmp_ ' );
83
+
84
+ Database::forge ($ this ->DBGroup )->dropTable ('widgets ' );
72
85
73
- $ this ->assertSame ( $ this -> expectedTables , array_values ( $ result ) );
86
+ $ this ->db -> setPrefix ( $ oldPrefix );
74
87
}
75
88
76
- public function testConstrainPrefixIgnoresOtherTables ()
89
+ public function testListTablesUnconstrainedByPrefixReturnsAllTables ()
77
90
{
78
- $ this ->forge = Database::forge ($ this ->DBGroup );
91
+ try {
92
+ $ this ->createExtraneousTable ();
79
93
80
- // Stash the prefix and change it
81
- $ DBPrefix = $ this ->db -> getPrefix ( );
82
- $ this ->db -> setPrefix ( ' tmp_ ' );
94
+ $ tables = $ this -> db -> listTables ();
95
+ $ this ->assertIsArray ( $ tables );
96
+ $ this ->assertNotSame ([], $ tables );
83
97
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 ' );
98
+ $ expectedTables = $ this ->expectedTables ;
99
+ $ expectedTables [] = 'tmp_widgets ' ;
97
100
98
- // Restore the prefix and get the tables with the original prefix
99
- $ this ->db ->setPrefix ($ DBPrefix );
100
- $ result = $ this ->db ->listTables (true );
101
+ sort ($ tables );
102
+ $ this ->assertSame ($ expectedTables , array_values ($ tables ));
103
+ } finally {
104
+ $ this ->dropExtraneousTable ();
105
+ }
106
+ }
101
107
102
- $ this ->assertSame ($ this ->expectedTables , array_values ($ result ));
108
+ public function testListTablesConstrainedByPrefixReturnsOnlyTablesWithMatchingPrefix ()
109
+ {
110
+ try {
111
+ $ this ->createExtraneousTable ();
103
112
104
- // Clean up temporary table
105
- $ this ->db ->setPrefix ('tmp_ ' );
106
- $ this ->forge ->dropTable ('widgets ' );
107
- $ this ->db ->setPrefix ($ DBPrefix );
113
+ $ tables = $ this ->db ->listTables (true );
114
+ $ this ->assertIsArray ($ tables );
115
+ $ this ->assertNotSame ([], $ tables );
116
+
117
+ sort ($ tables );
118
+ $ this ->assertSame ($ this ->expectedTables , array_values ($ tables ));
119
+ } finally {
120
+ $ this ->dropExtraneousTable ();
121
+ }
122
+ }
123
+
124
+ public function testListTablesConstrainedByExtraneousPrefixReturnsOnlyTheExtraneousTable ()
125
+ {
126
+ try {
127
+ $ this ->createExtraneousTable ();
128
+
129
+ $ oldPrefix = $ this ->db ->getPrefix ();
130
+ $ this ->db ->setPrefix ('tmp_ ' );
131
+
132
+ $ tables = $ this ->db ->listTables (true );
133
+ $ this ->assertIsArray ($ tables );
134
+ $ this ->assertNotSame ([], $ tables );
135
+
136
+ sort ($ tables );
137
+ $ this ->assertSame (['tmp_widgets ' ], array_values ($ tables ));
138
+ } finally {
139
+ $ this ->db ->setPrefix ($ oldPrefix );
140
+ $ this ->dropExtraneousTable ();
141
+ }
108
142
}
109
143
}
0 commit comments