]> BookStack Code Mirror - bookstack/blobdiff - tests/TestCase.php
Cleaned testing service provider usage
[bookstack] / tests / TestCase.php
index 95e58b267b812b6ecb8e06494316c371fe81d47d..5941941681d75bca4a9fdfdc2fe5c5d84de4f795 100644 (file)
@@ -22,10 +22,12 @@ use GuzzleHttp\Client;
 use GuzzleHttp\Handler\MockHandler;
 use GuzzleHttp\HandlerStack;
 use GuzzleHttp\Middleware;
+use Illuminate\Contracts\Console\Kernel;
 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;
@@ -47,6 +49,21 @@ abstract class TestCase extends BaseTestCase
      */
     protected string $baseUrl = 'http://localhost';
 
+    /**
+     * Creates the application.
+     *
+     * @return \Illuminate\Foundation\Application
+     */
+    public function createApplication()
+    {
+        /** @var \Illuminate\Foundation\Application  $app */
+        $app = require __DIR__ . '/../bootstrap/app.php';
+        $app->register(TestServiceProvider::class);
+        $app->make(Kernel::class)->bootstrap();
+
+        return $app;
+    }
+
     /**
      * Set the current user context to be an admin.
      */
@@ -89,6 +106,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 +316,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 +330,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)) {
@@ -428,4 +460,17 @@ abstract class TestCase extends BaseTestCase
 
         $this->assertDatabaseHas('activities', $detailsToCheck);
     }
+
+    /**
+     * @return array{page: Page, chapter: Chapter, book: Book, bookshelf: Bookshelf}
+     */
+    protected function getEachEntityType(): array
+    {
+        return [
+            'page'      => Page::query()->first(),
+            'chapter'   => Chapter::query()->first(),
+            'book'      => Book::query()->first(),
+            'bookshelf' => Bookshelf::query()->first(),
+        ];
+    }
 }