142 lines
5.4 KiB
PHP
142 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Enums\Role;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\UniqueConstraintViolationException;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use Tests\TestCase;
|
|
|
|
class ProfileModelTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/** @return array<string, array{Role, string, string}> role, tabel, kolom nomor induk */
|
|
public static function profiles(): array
|
|
{
|
|
return [
|
|
'superuser' => [Role::Superuser, 'profil_superuser', 'nip'],
|
|
'admin' => [Role::Admin, 'profil_admin', 'nip'],
|
|
'operator' => [Role::Operator, 'profil_operator', 'nip'],
|
|
'dosen' => [Role::Dosen, 'profil_dosen', 'nidn'],
|
|
'mahasiswa' => [Role::Mahasiswa, 'profil_mahasiswa', 'nim'],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('profiles')]
|
|
public function test_profile_factory_creates_a_valid_row_linked_to_a_user_of_the_same_role(Role $role, string $table, string $identifier): void
|
|
{
|
|
$profile = $role->profileModel()::factory()->create();
|
|
|
|
$this->assertDatabaseHas($table, ['id' => $profile->id, 'user_id' => $profile->user_id]);
|
|
$this->assertSame($table, $profile->getTable());
|
|
$this->assertSame($role, $profile->user->role);
|
|
$this->assertNotNull($profile->{$identifier});
|
|
$this->assertSame($profile->{$identifier}, $profile->nomor_induk);
|
|
}
|
|
|
|
#[DataProvider('profiles')]
|
|
public function test_profile_belongs_to_user_and_user_exposes_it(Role $role, string $table, string $identifier): void
|
|
{
|
|
$profile = $role->profileModel()::factory()->create();
|
|
|
|
$this->assertInstanceOf(BelongsTo::class, $profile->user());
|
|
$this->assertTrue($profile->user->is(User::findOrFail($profile->user_id)));
|
|
$this->assertTrue($profile->user->{$role->profileRelation()}->is($profile));
|
|
}
|
|
|
|
#[DataProvider('profiles')]
|
|
public function test_profile_factory_can_be_attached_to_an_existing_user(Role $role, string $table, string $identifier): void
|
|
{
|
|
$user = User::factory()->state(['role' => $role])->create();
|
|
|
|
$profile = $role->profileModel()::factory()->create(['user_id' => $user->id]);
|
|
|
|
$this->assertSame(1, User::count());
|
|
$this->assertSame($user->id, $profile->user_id);
|
|
}
|
|
|
|
#[DataProvider('profiles')]
|
|
public function test_a_user_can_have_only_one_profile_row_per_role(Role $role, string $table, string $identifier): void
|
|
{
|
|
$profile = $role->profileModel()::factory()->create();
|
|
|
|
$this->expectException(UniqueConstraintViolationException::class);
|
|
|
|
$role->profileModel()::factory()->create(['user_id' => $profile->user_id]);
|
|
}
|
|
|
|
#[DataProvider('profiles')]
|
|
public function test_identifier_is_unique(Role $role, string $table, string $identifier): void
|
|
{
|
|
$role->profileModel()::factory()->create([$identifier => '123456']);
|
|
|
|
$this->expectException(UniqueConstraintViolationException::class);
|
|
|
|
$role->profileModel()::factory()->create([$identifier => '123456']);
|
|
}
|
|
|
|
#[DataProvider('profiles')]
|
|
public function test_deleting_the_user_deletes_the_profile(Role $role, string $table, string $identifier): void
|
|
{
|
|
$profile = $role->profileModel()::factory()->create();
|
|
|
|
$profile->user->delete();
|
|
|
|
$this->assertDatabaseMissing($table, ['id' => $profile->id]);
|
|
}
|
|
|
|
#[DataProvider('profiles')]
|
|
public function test_profile_table_has_the_documented_columns(Role $role, string $table, string $identifier): void
|
|
{
|
|
$expected = match ($role) {
|
|
Role::Dosen => ['nidn', 'nip', 'jabatan_fungsional'],
|
|
Role::Mahasiswa => ['nim', 'angkatan', 'status'],
|
|
default => ['nip', 'jabatan', 'unit_kerja'],
|
|
};
|
|
|
|
foreach (['id', 'user_id', 'created_at', 'updated_at', ...$expected] as $column) {
|
|
$this->assertTrue(Schema::hasColumn($table, $column), "{$table}.{$column} tidak ada.");
|
|
}
|
|
}
|
|
|
|
public function test_staff_profiles_allow_missing_optional_columns(): void
|
|
{
|
|
foreach ([Role::Superuser, Role::Admin, Role::Operator] as $role) {
|
|
$profile = $role->profileModel()::factory()->create(['nip' => null, 'jabatan' => null, 'unit_kerja' => null]);
|
|
|
|
$this->assertNull($profile->nomor_induk);
|
|
}
|
|
}
|
|
|
|
public function test_dosen_profile_allows_missing_optional_columns(): void
|
|
{
|
|
$dosen = Role::Dosen->profileModel()::factory()->create(['nidn' => null, 'nip' => null, 'jabatan_fungsional' => null]);
|
|
|
|
$this->assertNull($dosen->nomor_induk);
|
|
}
|
|
|
|
public function test_mahasiswa_defaults_and_casts(): void
|
|
{
|
|
$user = User::factory()->mahasiswa()->create();
|
|
$mahasiswa = Role::Mahasiswa->profileModel()::create(['user_id' => $user->id, 'nim' => 'D1041250001', 'angkatan' => 2025]);
|
|
|
|
$this->assertSame('aktif', $mahasiswa->fresh()->status);
|
|
$this->assertSame('D1041250001', $mahasiswa->fresh()->nomor_induk);
|
|
$this->assertEquals(2025, $mahasiswa->fresh()->angkatan);
|
|
}
|
|
|
|
public function test_every_user_factory_default_creates_no_profile(): void
|
|
{
|
|
User::factory()->count(3)->create();
|
|
|
|
foreach (Role::cases() as $role) {
|
|
$this->assertSame(0, $role->profileModel()::count());
|
|
}
|
|
}
|
|
}
|