33namespace Tests ;
44
55use ArrayAccess ;
6+ use Database \Seeders \PermissionSeeder ;
67use Illuminate \Foundation \Testing \RefreshDatabase ;
78use Illuminate \Foundation \Testing \RefreshDatabaseState ;
89use Illuminate \Foundation \Testing \TestCase as BaseTestCase ;
1920use ProcessMaker \Jobs \RefreshArtisanCaches ;
2021use ProcessMaker \Models \Process ;
2122use ProcessMaker \Models \User ;
23+ use ProcessMaker \Providers \AuthServiceProvider ;
2224use Tests \TestSeeder ;
2325
2426abstract class TestCase extends BaseTestCase
@@ -54,7 +56,7 @@ protected function setUp(): void
5456
5557 if (self ::$ currentNonTransactionalTest && self ::$ currentNonTransactionalTest !== $ class ) {
5658 // The last test to run was non-transactional, so we need to refresh the database
57- $ this ->restoreDatabaseFromSnapshot ( );
59+ $ this ->restoreDatabaseSnapshot ( ' non-transactional-test ' );
5860 self ::$ currentNonTransactionalTest = null ;
5961 }
6062
@@ -89,8 +91,26 @@ protected function setUp(): void
8991 }
9092
9193 if (!self ::$ databaseSnapshotFile ) {
92- self ::$ databaseSnapshotFile = $ this ->takeDatabaseSnapshot ();
94+ self ::$ databaseSnapshotFile = $ this ->takeDatabaseSnapshot (' non-transactional-test ' );
9395 }
96+
97+ if ($ this ->withPermissions === true ) {
98+ $ this ->initializePermissions ();
99+ }
100+ }
101+
102+ public function initializePermissions ($ runSeeder = true )
103+ {
104+ if ($ runSeeder ) {
105+ //Run the permission seeder
106+ (new PermissionSeeder )->run ();
107+ }
108+
109+ // Reboot our AuthServiceProvider. This is necessary so that it can
110+ // pick up the new permissions and setup gates for each of them.
111+ $ asp = new AuthServiceProvider (app ());
112+ $ asp ->boot ();
113+ $ asp ->defineGates ();
94114 }
95115
96116 /**
@@ -217,12 +237,14 @@ private function populateDatabase() : bool
217237 return (bool ) env ('POPULATE_DATABASE ' , true );
218238 }
219239
220- private function takeDatabaseSnapshot ($ filename = ' test-db-snapshot.db ' )
240+ public function takeDatabaseSnapshot ($ id = null )
221241 {
222242 if (!$ this ->populateDatabase ()) {
223243 return ;
224244 }
225245
246+ $ filename = 'snapshot_ ' . $ id . '.db ' ;
247+
226248 $ snapshotFile = base_path ($ filename );
227249 $ command = 'mysqldump ' . $ this ->mysqlConnectionString ();
228250 $ command .= ' ' . env ('DB_DATABASE ' ) . ' > ' . $ snapshotFile ;
@@ -231,11 +253,17 @@ private function takeDatabaseSnapshot($filename = 'test-db-snapshot.db')
231253 dd ("Failed to take database snapshot: $ command " );
232254 }
233255
234- return $ snapshotFile ;
256+ return $ id ;
235257 }
236258
237- public function restoreDatabaseFromSnapshot ( $ filename = ' test-db-snapshot.db ' )
259+ public function restoreDatabaseSnapshot ( $ id )
238260 {
261+ $ filename = 'snapshot_ ' . $ id . '.db ' ;
262+
263+ if (!file_exists (base_path ($ filename ))) {
264+ throw new \Exception ("Database snapshot not found: $ filename " );
265+ }
266+
239267 if (!$ this ->populateDatabase ()) {
240268 return ;
241269 }
0 commit comments