koel/tests/Traits/CreatesApplication.php

45 lines
1.2 KiB
PHP
Raw Normal View History

2017-02-14 14:53:02 +08:00
<?php
2020-06-13 14:19:24 +02:00
namespace Tests\Traits;
2017-02-14 14:53:02 +08:00
2019-07-22 11:54:39 +02:00
use App\Console\Kernel;
2017-06-10 14:25:30 +01:00
use App\Models\User;
2018-08-31 20:47:15 +07:00
use Illuminate\Contracts\Console\Kernel as Artisan;
2018-08-23 08:58:17 +02:00
use Illuminate\Foundation\Application;
2022-12-04 14:56:25 +01:00
use Illuminate\Support\Facades\DB;
2017-02-14 14:53:02 +08:00
trait CreatesApplication
{
2021-06-05 12:47:56 +02:00
protected string $mediaPath = __DIR__ . '/../songs';
private Kernel $artisan;
protected string $baseUrl = 'http://localhost';
2022-12-04 14:56:25 +01:00
public static bool $migrated = false;
2017-02-14 14:53:02 +08:00
2020-12-22 21:11:22 +01:00
public function createApplication(): Application
2017-02-14 14:53:02 +08:00
{
2022-07-07 12:45:57 +02:00
$this->mediaPath = realpath($this->mediaPath);
/** @var Application $app */
2020-12-22 21:11:22 +01:00
$app = require __DIR__ . '/../../bootstrap/app.php';
2017-02-14 14:53:02 +08:00
/** @var Kernel $artisan */
$artisan = $app->make(Artisan::class);
$this->artisan = $artisan;
2018-08-31 20:47:15 +07:00
$this->artisan->bootstrap();
2017-02-14 14:53:02 +08:00
2022-12-04 14:56:25 +01:00
// 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');
2017-06-10 14:25:30 +01:00
2022-12-04 14:56:25 +01:00
if (!User::query()->count()) {
$this->artisan->call('db:seed');
}
2017-06-10 14:25:30 +01:00
2022-12-04 14:56:25 +01:00
CreatesApplication::$migrated = true;
2017-06-10 14:25:30 +01:00
}
2022-12-04 14:56:25 +01:00
return $app;
2017-06-10 14:25:30 +01:00
}
2017-02-14 14:53:02 +08:00
}