first
This commit is contained in:
54
tests/Feature/ImportTest.php
Normal file
54
tests/Feature/ImportTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user