mirror of
https://github.com/linuxserver/Heimdall.git
synced 2026-08-07 07:16:13 +00:00
2555ab1b3c
The :memory: overrides in phpunit.xml had been commented out since 2024, so RefreshDatabase ran migrate:fresh against the real .env database and wiped it on every local test run. Enable the overrides, let the special :memory: identifier bypass database_path() resolution and the boot-time touch(), and add a TestCase guard that aborts the suite unless it is pointed at in-memory sqlite. ItemExportTest only ever passed by reading the populated dev database; seed the root dashboard item it depends on so it passes on a fresh DB.
48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
|
|
|
abstract class TestCase extends BaseTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->guardAgainstRealDatabase();
|
|
}
|
|
|
|
/**
|
|
* Refuse to run the test suite against anything other than an
|
|
* in-memory SQLite database.
|
|
*
|
|
* The suite uses RefreshDatabase (migrate:fresh), which would wipe
|
|
* whatever database it is pointed at. The only supported test
|
|
* configuration for this repo is sqlite + ':memory:'. Any other
|
|
* connection (a real sqlite file, mysql, pgsql, ...) is rejected so
|
|
* we can never destroy real data.
|
|
*/
|
|
private function guardAgainstRealDatabase(): void
|
|
{
|
|
$default = config('database.default');
|
|
$driver = config("database.connections.{$default}.driver");
|
|
$database = config("database.connections.{$default}.database");
|
|
|
|
if ($driver === 'sqlite' && $database === ':memory:') {
|
|
return;
|
|
}
|
|
|
|
throw new \RuntimeException(sprintf(
|
|
'Refusing to run tests: the default database connection (%s) is '
|
|
. 'driver "%s" pointing at "%s". Tests only run against an '
|
|
. 'in-memory SQLite database (driver "sqlite", database ":memory:") '
|
|
. 'to avoid wiping real data via RefreshDatabase. Check phpunit.xml '
|
|
. 'DB_CONNECTION/DB_DATABASE overrides.',
|
|
$default,
|
|
$driver,
|
|
is_scalar($database) ? (string) $database : gettype($database)
|
|
));
|
|
}
|
|
}
|