From c3f1c320a4e58593e6c341b17bc928219735f701 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Thu, 27 Aug 2026 15:21:16 -0300 Subject: [PATCH] test: isolate PHPUnit database --- phpunit.xml | 1 + tests/TestCase.php | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 642274a..a3b9b10 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,6 +19,7 @@ + diff --git a/tests/TestCase.php b/tests/TestCase.php index fe1ffc2..9b31cc8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,25 @@ namespace Tests; +use Illuminate\Foundation\Application; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; +use RuntimeException; abstract class TestCase extends BaseTestCase { - // + public function createApplication(): Application + { + $app = parent::createApplication(); + $connection = (string) $app['config']->get('database.default'); + $database = (string) $app['config']->get("database.connections.{$connection}.database"); + $usesInMemorySqlite = $connection === 'sqlite' && $database === ':memory:'; + + if (! $usesInMemorySqlite && ! str_ends_with(strtolower($database), '_test')) { + throw new RuntimeException( + "Unsafe test database [{$database}]. Tests may only use an in-memory SQLite database or a database ending in _test.", + ); + } + + return $app; + } }