44 lines
1.5 KiB
PHP
44 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
|
use RuntimeException;
|
|
use Tests\Support\InMemoryConnectionFactory;
|
|
|
|
abstract class TestCase extends BaseTestCase
|
|
{
|
|
/**
|
|
* Boot the application only when the test database is explicitly isolated.
|
|
*/
|
|
public function createApplication(): Application
|
|
{
|
|
$app = require dirname(__DIR__).'/bootstrap/app.php';
|
|
$this->traitsUsedByTest = class_uses_recursive(static::class);
|
|
|
|
if ($app->configurationIsCached()) {
|
|
throw new RuntimeException('Tests refuse cached configuration. Remove the test config cache before retrying.');
|
|
}
|
|
|
|
// Validate before providers boot or RefreshDatabase can run migrations.
|
|
$app->afterBootstrapping(LoadConfiguration::class, function (Application $app): void {
|
|
if (! $app->environment('testing')) {
|
|
throw new RuntimeException('Tests require APP_ENV=testing.');
|
|
}
|
|
|
|
InMemoryConnectionFactory::assertSafe((array) $app['config']->get(
|
|
'database.connections.'.$app['config']->get('database.default')
|
|
));
|
|
});
|
|
|
|
// Also guard named/dynamic connections and changes made by individual tests.
|
|
$app->extend('db.factory', fn () => new InMemoryConnectionFactory($app));
|
|
$app->make(Kernel::class)->bootstrap();
|
|
|
|
return $app;
|
|
}
|
|
}
|