85 lines
3.5 KiB
PHP
85 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Member;
|
|
use App\Models\Prize;
|
|
use App\Services\MemberImportService;
|
|
use App\Services\PrizeImportService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class ImportTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_member_import_handles_duplicates_and_missing_name(): void
|
|
{
|
|
$rows = [
|
|
['no_pekerja' => 'P001', 'no_kp' => '900101-01-1111', 'nama' => 'Ali'],
|
|
['no_pekerja' => 'P001', 'no_kp' => '900101-01-2222', 'nama' => 'Ali Duplicate Pekerja'], // dup dalam fail
|
|
['no_pekerja' => 'P002', 'no_kp' => '', 'nama' => ''], // gagal: nama kosong
|
|
['no_pekerja' => 'P003', 'no_kp' => '900101-01-3333', 'nama' => 'Siti'],
|
|
];
|
|
|
|
$log = (new MemberImportService())->import($rows, 'test.csv');
|
|
|
|
$this->assertEquals(2, $log->success_count);
|
|
$this->assertEquals(1, $log->duplicate_count);
|
|
$this->assertEquals(1, $log->failed_count);
|
|
$this->assertEquals(2, Member::count());
|
|
}
|
|
|
|
public function test_member_import_default_status_aktif(): void
|
|
{
|
|
(new MemberImportService())->import([['nama' => 'Tiada Status']], 'test.csv');
|
|
$this->assertTrue(Member::first()->status_aktif);
|
|
}
|
|
|
|
public function test_prize_import_expands_quantity(): void
|
|
{
|
|
$rows = [
|
|
['nama_hadiah' => 'Hamper', 'kuantiti' => '5', 'susunan_cabutan' => '3'],
|
|
['nama_hadiah' => 'Motosikal', 'kuantiti' => '1', 'susunan_cabutan' => '1'],
|
|
];
|
|
|
|
$log = (new PrizeImportService())->import($rows, 'hadiah.csv');
|
|
|
|
$this->assertEquals(6, $log->success_count);
|
|
$this->assertEquals(6, Prize::count());
|
|
$this->assertEquals(5, Prize::where('nama_hadiah', 'like', 'Hamper%')->count());
|
|
$this->assertDatabaseHas('prizes', ['nama_hadiah' => 'Hamper #5', 'draw_order' => 3]);
|
|
$this->assertDatabaseHas('prizes', ['nama_hadiah' => 'Motosikal', 'draw_order' => 1]);
|
|
}
|
|
|
|
public function test_prize_import_overwrites_same_susunan_cabutan(): void
|
|
{
|
|
$service = new PrizeImportService();
|
|
$service->import([['nama_hadiah' => 'Hadiah Lama', 'kuantiti' => '2', 'susunan_cabutan' => '1']], 'lama.csv');
|
|
|
|
$this->assertEquals(2, Prize::where('draw_order', 1)->count());
|
|
|
|
$log = $service->import([['nama_hadiah' => 'Hadiah Baru', 'kuantiti' => '1', 'susunan_cabutan' => '1']], 'baru.csv');
|
|
|
|
$this->assertEquals(1, $log->success_count);
|
|
$this->assertEquals(2, $log->duplicate_count);
|
|
$this->assertEquals(1, Prize::where('draw_order', 1)->count());
|
|
$this->assertDatabaseHas('prizes', ['nama_hadiah' => 'Hadiah Baru', 'draw_order' => 1]);
|
|
$this->assertDatabaseMissing('prizes', ['nama_hadiah' => 'Hadiah Lama']);
|
|
}
|
|
|
|
public function test_prize_import_does_not_overwrite_confirmed_winner(): void
|
|
{
|
|
$service = new PrizeImportService();
|
|
$service->import([['nama_hadiah' => 'Hadiah Disahkan', 'kuantiti' => '1', 'susunan_cabutan' => '1']], 'lama.csv');
|
|
Prize::where('draw_order', 1)->update(['status' => Prize::STATUS_DISAHKAN]);
|
|
|
|
$log = $service->import([['nama_hadiah' => 'Cuba Ganti', 'kuantiti' => '1', 'susunan_cabutan' => '1']], 'baru.csv');
|
|
|
|
$this->assertEquals(0, $log->success_count);
|
|
$this->assertEquals(1, $log->failed_count);
|
|
$this->assertDatabaseHas('prizes', ['nama_hadiah' => 'Hadiah Disahkan']);
|
|
$this->assertDatabaseMissing('prizes', ['nama_hadiah' => 'Cuba Ganti']);
|
|
}
|
|
}
|