diff --git a/.env.testing b/.env.testing new file mode 100644 index 0000000..44ba50f --- /dev/null +++ b/.env.testing @@ -0,0 +1,22 @@ +APP_ENV=testing +APP_DEBUG=true +APP_URL=http://localhost +APP_KEY=base64:Zm7kR6vDfsQQmCeWTR2Mk2WQGu6fGP1UHEcZVk/hIt0= + +# ── Hard rule — tests must never touch the shared SQLite file. ─────────── +# phpunit.xml also sets these via , but having them here means Laravel +# picks them up during the earliest bootstrap step (before any trait or +# setUp hook can run), closing the window that let migrate:fresh wipe the +# dev DB on 2026-10-21. If you change these values, read tests/TestCase.php's +# assertSafeTestDatabase() guard first. +DB_CONNECTION=sqlite +DB_DATABASE=:memory: + +BCRYPT_ROUNDS=4 +CACHE_DRIVER=array +MAIL_MAILER=array +QUEUE_CONNECTION=sync +SESSION_DRIVER=array + +TELESCOPE_ENABLED=false +PULSE_ENABLED=false diff --git a/tests/TestCase.php b/tests/TestCase.php index 2932d4a..dac81e1 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,4 +7,39 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; + + /** + * Belt-and-braces guard against a repeat of the 2026-10-21 incident, where + * a test with the RefreshDatabase trait ran migrate:fresh against the + * shared dev SQLite file and wiped every row. + * + * By the time setUp() runs we know the final DB config. If it points at + * anything other than an in-memory SQLite (or a file inside the test + * directory), we abort the whole run before any migration can execute. + */ + protected function setUp(): void + { + parent::setUp(); + $this->assertSafeTestDatabase(); + } + + private function assertSafeTestDatabase(): void + { + $driver = config('database.default'); + $database = config("database.connections.{$driver}.database"); + + $allowed = $database === ':memory:' + || $database === null + || str_contains((string) $database, '/testing') + || str_contains((string) $database, '/tmp/') + || str_ends_with((string) $database, '.testing.sqlite'); + + if (! $allowed) { + // Fatal, not an assertion failure — a wrong DB target has to stop + // execution before Laravel can run even one query against it. + fwrite(STDERR, "\n\n*** ABORTING TESTS: unsafe DB target '{$database}' on connection '{$driver}'. ***\n"); + fwrite(STDERR, "Set DB_CONNECTION=sqlite and DB_DATABASE=:memory: in .env.testing (or in your test's setUp before parent::setUp).\n\n"); + exit(2); + } + } }