-
-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathLoadedSubscriber.php
58 lines (50 loc) · 1.64 KB
/
LoadedSubscriber.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/
namespace Tests;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Event\TestSuite\Loaded;
use PHPUnit\Event\TestSuite\LoadedSubscriber as LoadedSubscriberInterface;
final class LoadedSubscriber implements LoadedSubscriberInterface
{
use CreatesApplication;
use MigrateApplication;
public function notify(Loaded $event): void
{
$this->createApplication();
$this->migrateApplication();
if (config('features.vuejs') === true) {
return;
}
/** @var User|null $admin */
$admin = User::find(1);
if ($admin === null) {
$admin = new User();
$admin->incrementing = false;
$admin->id = 1;
$admin->may_upload = true;
$admin->may_edit_own_settings = true;
$admin->may_administrate = true;
$admin->username = 'admin';
$admin->password = Hash::make('password');
$admin->save();
if (Schema::connection(null)->getConnection()->getDriverName() === 'pgsql' && DB::table('users')->count() > 0) {
// when using PostgreSQL, the next ID value is kept when inserting without incrementing
// which results in errors because trying to insert a user with ID = 1.
// Thus, we need to reset the index to the greatest ID + 1
/** @var User $lastUser */
$lastUser = User::query()->orderByDesc('id')->first();
DB::statement('ALTER SEQUENCE users_id_seq1 RESTART WITH ' . strval($lastUser->id + 1));
}
} elseif (!$admin->may_administrate) {
$admin->may_administrate = true;
$admin->save();
}
}
}