/
CreatesApplication.php
44 lines (33 loc) 路 1.21 KB
/
CreatesApplication.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace Tests\Traits;
use App\Console\Kernel;
use App\Models\User;
use Illuminate\Contracts\Console\Kernel as Artisan;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\DB;
trait CreatesApplication
{
protected string $mediaPath = __DIR__ . '/../songs';
private Kernel $artisan;
protected string $baseUrl = 'http://localhost';
public static bool $migrated = false;
public function createApplication(): Application
{
$this->mediaPath = realpath($this->mediaPath);
/** @var Application $app */
$app = require __DIR__ . '/../../bootstrap/app.php';
$artisan = $app->make(Artisan::class);
assert($artisan instanceof Kernel);
$this->artisan = $artisan;
$this->artisan->bootstrap();
// Unless the DB is stored in memory, we need to migrate the DB only once for the whole test suite.
if (!CreatesApplication::$migrated || DB::connection()->getDatabaseName() === ':memory:') {
$this->artisan->call('migrate');
if (!User::query()->count()) {
$this->artisan->call('db:seed');
}
CreatesApplication::$migrated = true;
}
return $app;
}
}