]> BookStack Code Mirror - bookstack/blobdiff - tests/TestCase.php
Added initial support for parallel testing
[bookstack] / tests / TestCase.php
index 92ae33a4ebeeef1729ea4934cb8d5e9882788aeb..0926b0dcc171d9367ead165995ace45fa779bf31 100644 (file)
@@ -26,6 +26,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Support\Env;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Testing\Assert as PHPUnit;
 use Monolog\Handler\TestHandler;
@@ -89,6 +90,14 @@ abstract class TestCase extends BaseTestCase
         return $this->editor;
     }
 
+    /**
+     * Set the current user context to be a viewer.
+     */
+    public function asViewer()
+    {
+        return $this->actingAs($this->getViewer());
+    }
+
     /**
      * Get an instance of a user with 'viewer' permissions.
      */
@@ -291,6 +300,8 @@ abstract class TestCase extends BaseTestCase
     /**
      * Run a set test with the given env variable.
      * Remembers the original and resets the value after test.
+     * Database config is juggled so the value can be restored when
+     * parallel testing are used, where multiple databases exist.
      */
     protected function runWithEnv(string $name, $value, callable $callback)
     {
@@ -303,7 +314,12 @@ abstract class TestCase extends BaseTestCase
             $_SERVER[$name] = $value;
         }
 
+        $database = config('database.connections.mysql_testing.database');
         $this->refreshApplication();
+
+        DB::purge();
+        config()->set('database.connections.mysql_testing.database', $database);
+
         $callback();
 
         if (is_null($originalVal)) {
@@ -430,7 +446,7 @@ abstract class TestCase extends BaseTestCase
     }
 
     /**
-     * @return Entity[]
+     * @return array{page: Page, chapter: Chapter, book: Book, bookshelf: Bookshelf}
      */
     protected function getEachEntityType(): array
     {