123 lines
4.0 KiB
PHP
123 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\Role;
|
|
use App\Models\User;
|
|
use Database\Seeders\DatabaseSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use Tests\TestCase;
|
|
|
|
class DatabaseSeederTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/** @return array<string, array{Role}> */
|
|
public static function roles(): array
|
|
{
|
|
return array_combine(Role::values(), array_map(fn (Role $role): array => [$role], Role::cases()));
|
|
}
|
|
|
|
private function seedDemo(): void
|
|
{
|
|
$this->app->make(DatabaseSeeder::class)->run();
|
|
}
|
|
|
|
public function test_it_creates_exactly_the_five_demo_accounts_with_their_profiles(): void
|
|
{
|
|
$this->seedDemo();
|
|
|
|
$this->assertSame(5, User::count());
|
|
$this->assertEqualsCanonicalizing(Role::values(), User::pluck('username')->all());
|
|
|
|
foreach (Role::cases() as $role) {
|
|
$this->assertSame(1, $role->profileModel()::count(), "profil {$role->value}");
|
|
}
|
|
}
|
|
|
|
#[DataProvider('roles')]
|
|
public function test_each_demo_account_has_the_documented_shape(Role $role): void
|
|
{
|
|
$this->seedDemo();
|
|
|
|
$user = User::where('username', $role->value)->firstOrFail();
|
|
|
|
$this->assertSame($role, $user->role);
|
|
$this->assertSame("{$role->value}@siakad-untan.test", $user->email);
|
|
$this->assertTrue($user->is_active);
|
|
$this->assertNotNull($user->email_verified_at);
|
|
$this->assertTrue(Hash::check('password', $user->password));
|
|
$this->assertNotSame('password', $user->getRawOriginal('password'));
|
|
|
|
$this->assertInstanceOf($role->profileModel(), $user->profile);
|
|
$this->assertNotEmpty($user->profile->nomor_induk);
|
|
$this->assertNotSame('', trim($user->name));
|
|
}
|
|
|
|
public function test_demo_identifiers_and_names(): void
|
|
{
|
|
$this->seedDemo();
|
|
|
|
$this->assertSame('D1041211001', User::where('username', 'mahasiswa')->first()->profile->nim);
|
|
$this->assertSame(2021, (int) User::where('username', 'mahasiswa')->first()->profile->angkatan);
|
|
$this->assertSame('0012058701', User::where('username', 'dosen')->first()->profile->nidn);
|
|
$this->assertStringContainsString('Dr.', User::where('username', 'dosen')->first()->name);
|
|
}
|
|
|
|
public function test_it_is_idempotent(): void
|
|
{
|
|
$this->seedDemo();
|
|
$ids = User::orderBy('id')->pluck('id', 'username')->all();
|
|
|
|
$this->seedDemo();
|
|
$this->seedDemo();
|
|
|
|
$this->assertSame(5, User::count());
|
|
$this->assertSame($ids, User::orderBy('id')->pluck('id', 'username')->all());
|
|
|
|
foreach (Role::cases() as $role) {
|
|
$this->assertSame(1, $role->profileModel()::count(), "profil {$role->value} tidak boleh ganda");
|
|
}
|
|
}
|
|
|
|
public function test_rerun_restores_a_changed_demo_account_without_duplicates(): void
|
|
{
|
|
$this->seedDemo();
|
|
User::where('username', 'admin')->update(['is_active' => false, 'name' => 'Diubah']);
|
|
|
|
$this->seedDemo();
|
|
|
|
$admin = User::where('username', 'admin')->firstOrFail();
|
|
$this->assertTrue($admin->is_active);
|
|
$this->assertNotSame('Diubah', $admin->name);
|
|
$this->assertSame(5, User::count());
|
|
}
|
|
|
|
public function test_demo_accounts_can_log_in_through_the_form(): void
|
|
{
|
|
$this->seedDemo();
|
|
|
|
foreach (Role::cases() as $role) {
|
|
$this->post(route('login.store'), ['username' => $role->value, 'password' => 'password'])
|
|
->assertRedirect(route($role->dashboardRoute()));
|
|
|
|
$this->post(route('logout'));
|
|
}
|
|
}
|
|
|
|
public function test_it_is_skipped_in_production(): void
|
|
{
|
|
$this->app->detectEnvironment(fn (): string => 'production');
|
|
$this->assertTrue(app()->isProduction());
|
|
|
|
$this->seedDemo();
|
|
|
|
$this->assertSame(0, User::count());
|
|
foreach (Role::cases() as $role) {
|
|
$this->assertSame(0, $role->profileModel()::count());
|
|
}
|
|
}
|
|
}
|