test: isolate PHPUnit database

This commit is contained in:
2026-08-27 15:21:16 -03:00
parent bbe3cf82f5
commit c3f1c320a4
2 changed files with 18 additions and 1 deletions

View File

@@ -19,6 +19,7 @@
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_DATABASE" value="shopit_test" force="true"/>
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="APP_CONFIG_CACHE" value="bootstrap/cache/phpunit-config.php"/>
<env name="APP_EVENTS_CACHE" value="bootstrap/cache/phpunit-events.php"/>

View File

@@ -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;
}
}