@@ -36,25 +36,64 @@ final class ForgeTest extends CIUnitTestCase
3636 protected $ seed = CITestSeeder::class;
3737 private Forge $ forge ;
3838
39+ private function dropAllMockTables (): void
40+ {
41+ $ tablesToDrop = [
42+ 'forge_test_invoices ' ,
43+ 'forge_test_inv ' ,
44+ 'forge_test_users ' ,
45+ 'actions ' ,
46+ 'forge_test_table ' ,
47+ 'test_exists ' ,
48+ 'forge_test_attributes ' ,
49+ 'forge_array_constraint ' ,
50+ 'forge_nullable_table ' ,
51+ 'forge_test_1 ' ,
52+ 'forge_test_two ' ,
53+ 'forge_test_three ' ,
54+ 'forge_test_four ' ,
55+ 'forge_test_modify ' ,
56+ 'droptest ' ,
57+ 'key_test_users ' ,
58+ 'test_stores ' ,
59+ 'user2 ' ,
60+ 'forge_test_table_dummy ' ,
61+ ];
62+
63+ foreach ($ tablesToDrop as $ table ) {
64+ $ this ->forge ->dropTable ($ table , true );
65+ }
66+ }
67+
3968 protected function setUp (): void
4069 {
4170 $ this ->forge = Database::forge ($ this ->DBGroup );
4271
43- // when running locally if one of these tables isn't dropped it may cause error
44- $ this ->forge ->dropTable ('forge_test_invoices ' , true );
45- $ this ->forge ->dropTable ('forge_test_inv ' , true );
46- $ this ->forge ->dropTable ('forge_test_users ' , true );
47- $ this ->forge ->dropTable ('actions ' , true );
72+ $ this ->dropAllMockTables ();
73+
74+ db_connect ($ this ->DBGroup )->resetDataCache ();
4875
4976 parent ::setUp ();
5077 }
5178
79+ protected function tearDown (): void
80+ {
81+ parent ::tearDown ();
82+ $ this ->dropAllMockTables ();
83+ }
84+
5285 public function testCreateDatabase (): void
5386 {
5487 if ($ this ->db ->DBDriver === 'OCI8 ' ) {
5588 $ this ->markTestSkipped ('OCI8 does not support create database. ' );
5689 }
5790
91+ try {
92+ $ this ->forge ->dropDatabase ('test_forge_database ' );
93+ } catch (DatabaseException ) {
94+ // Ignore if doesn't exist
95+ }
96+
5897 $ databaseCreated = $ this ->forge ->createDatabase ('test_forge_database ' );
5998
6099 $ this ->assertTrue ($ databaseCreated );
@@ -68,14 +107,20 @@ public function testCreateDatabaseWithDots(): void
68107
69108 $ dbName = 'test_com.sitedb.web ' ;
70109
110+ try {
111+ $ this ->forge ->dropDatabase ($ dbName );
112+ } catch (DatabaseException ) {
113+ // Ignore if doesn't exist
114+ }
115+
71116 $ databaseCreated = $ this ->forge ->createDatabase ($ dbName );
72117
73118 $ this ->assertTrue ($ databaseCreated );
74119
75120 // Checks if tableExists() works.
76121 $ config = config (Database::class)->{$ this ->DBGroup };
77122 $ config ['database ' ] = $ dbName ;
78- $ db = db_connect ($ config );
123+ $ db = db_connect ($ config, false );
79124 $ result = $ db ->tableExists ('not_exist ' );
80125
81126 $ this ->assertFalse ($ result );
@@ -151,6 +196,12 @@ public function testDropDatabase(): void
151196 $ this ->markTestSkipped ('SQLite3 requires file path to drop database ' );
152197 }
153198
199+ try {
200+ $ this ->forge ->createDatabase ('test_forge_database ' );
201+ } catch (DatabaseException ) {
202+ // Ignore if exists
203+ }
204+
154205 $ databaseDropped = $ this ->forge ->dropDatabase ('test_forge_database ' );
155206
156207 $ this ->assertTrue ($ databaseDropped );
0 commit comments