58 lines
2.0 KiB
PHP
58 lines
2.0 KiB
PHP
<?php
|
||
|
||
namespace Tests;
|
||
|
||
use Illuminate\Contracts\Console\Kernel;
|
||
use Illuminate\Database\DatabaseManager;
|
||
use RuntimeException;
|
||
|
||
trait CreatesApplication
|
||
{
|
||
/**
|
||
* Creates the application.
|
||
*
|
||
* @return \Illuminate\Foundation\Application
|
||
*/
|
||
public function createApplication()
|
||
{
|
||
$testingConfigCache = __DIR__ . '/../bootstrap/cache/config-testing.php';
|
||
|
||
putenv('APP_CONFIG_CACHE=' . $testingConfigCache);
|
||
$_ENV['APP_CONFIG_CACHE'] = $testingConfigCache;
|
||
$_SERVER['APP_CONFIG_CACHE'] = $testingConfigCache;
|
||
|
||
$app = require __DIR__ . '/../bootstrap/app.php';
|
||
|
||
$app->make(Kernel::class)->bootstrap();
|
||
|
||
if (
|
||
! $app->environment('testing')
|
||
|| config('database.default') !== 'sqlite'
|
||
|| config('database.connections.sqlite.database') !== ':memory:'
|
||
) {
|
||
throw new RuntimeException('测试环境未使用内存 SQLite,已拒绝执行以保护共享数据库。');
|
||
}
|
||
|
||
// Several migrations deliberately target the business_mirror connection.
|
||
// Force that connection onto its own in-memory SQLite database before
|
||
// RefreshDatabase runs, even when the developer's local .env points at
|
||
// the shared production mirror.
|
||
config([
|
||
'database.connections.business_mirror' => config('database.connections.sqlite'),
|
||
'database.supplier_payable_maintenance_connection' => 'sqlite',
|
||
'jushuitan.connection' => 'business_mirror',
|
||
'upstream_mirrors.connection' => 'business_mirror',
|
||
]);
|
||
$app->make(DatabaseManager::class)->purge('business_mirror');
|
||
|
||
if (
|
||
config('database.connections.business_mirror.driver') !== 'sqlite'
|
||
|| config('database.connections.business_mirror.database') !== ':memory:'
|
||
) {
|
||
throw new RuntimeException('测试业务镜像连接未使用内存 SQLite,已拒绝执行以保护共享数据库。');
|
||
}
|
||
|
||
return $app;
|
||
}
|
||
}
|