70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\PayrollEntry;
|
|
use App\Models\PayrollRun;
|
|
use App\Models\Payslip;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class PayslipGenerationRelationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_payroll_entry_defines_payslip_relationship(): void
|
|
{
|
|
$company = User::factory()->create(['type' => 'company']);
|
|
$employee = User::factory()->create(['type' => 'employee', 'created_by' => $company->id]);
|
|
|
|
$payrollRun = PayrollRun::create([
|
|
'title' => 'Test Payroll Run',
|
|
'payroll_frequency' => 'monthly',
|
|
'pay_period_start' => '2026-09-01',
|
|
'pay_period_end' => '2026-09-15',
|
|
'pay_date' => '2026-09-15',
|
|
'status' => 'processing',
|
|
'created_by' => $company->id,
|
|
]);
|
|
|
|
$payrollEntry = PayrollEntry::create([
|
|
'payroll_run_id' => $payrollRun->id,
|
|
'employee_id' => $employee->id,
|
|
'basic_salary' => 15000,
|
|
'net_pay' => 15000,
|
|
'gross_pay' => 15000,
|
|
'created_by' => $company->id,
|
|
]);
|
|
|
|
// Verify relationship method exists and query works
|
|
$pending = PayrollEntry::where('payroll_run_id', $payrollRun->id)
|
|
->whereDoesntHave('payslip')
|
|
->exists();
|
|
|
|
$this->assertTrue($pending);
|
|
|
|
// Create payslip for entry
|
|
$payslip = Payslip::create([
|
|
'payroll_entry_id' => $payrollEntry->id,
|
|
'employee_id' => $employee->id,
|
|
'payslip_number' => 'PS-TEST-001',
|
|
'pay_period_start' => '2026-09-01',
|
|
'pay_period_end' => '2026-09-15',
|
|
'pay_date' => '2026-09-15',
|
|
'status' => 'generated',
|
|
'created_by' => $company->id,
|
|
]);
|
|
|
|
$this->assertInstanceOf(Payslip::class, $payrollEntry->fresh()->payslip);
|
|
$this->assertEquals($payslip->id, $payrollEntry->fresh()->payslip->id);
|
|
|
|
// Now whereDoesntHave should return false
|
|
$pendingAfter = PayrollEntry::where('payroll_run_id', $payrollRun->id)
|
|
->whereDoesntHave('payslip')
|
|
->exists();
|
|
|
|
$this->assertFalse($pendingAfter);
|
|
}
|
|
}
|