77 lines
3.5 KiB
PHP
77 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use Illuminate\Contracts\Filesystem\Factory;
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
use Modules\EcommerceFinance\Services\PlantingReimbursementCorrections;
|
|
use PHPUnit\Framework\TestCase;
|
|
use RuntimeException;
|
|
|
|
class PlantingReimbursementCorrectionsTest extends TestCase
|
|
{
|
|
private function service(array $documents): PlantingReimbursementCorrections
|
|
{
|
|
$disk = $this->createMock(Filesystem::class);
|
|
$disk->method('exists')->willReturnCallback(static fn ($path) => isset($documents[$path]));
|
|
$disk->method('get')->willReturnCallback(static fn ($path) => json_encode($documents[$path]));
|
|
$factory = $this->createMock(Factory::class);
|
|
$factory->method('disk')->with('local')->willReturn($disk);
|
|
|
|
return new PlantingReimbursementCorrections($factory);
|
|
}
|
|
|
|
private function document(array $patch = []): array
|
|
{
|
|
return ['rows' => [array_replace([
|
|
'work_order_no' => 'GD-TEST', 'online_order_no' => '1234567890123456789',
|
|
'task_payment' => '20.50', 'reimbursement_amount' => '28.50',
|
|
], $patch)]];
|
|
}
|
|
|
|
private function row(array $patch = []): array
|
|
{
|
|
return array_replace([
|
|
'work_order_no' => 'GD-TEST', 'online_order_no' => '1234567890123456789',
|
|
'task_payment' => '35.00', 'reimbursement_amount' => '43.00', 'actual_paid_amount' => '35.00',
|
|
'task_commission' => '8.00', 'seller_remark' => '备注',
|
|
], $patch);
|
|
}
|
|
|
|
public function test_corrects_only_the_matched_work_order_and_preserves_other_fields_after_resync(): void
|
|
{
|
|
$service = $this->service([PlantingReimbursementCorrections::path(1, '2026-11') => $this->document()]);
|
|
foreach (['备注', '同步后的备注'] as $remark) {
|
|
$rows = [$this->row(['seller_remark' => $remark]), $this->row(['work_order_no' => 'GD-OTHER']), $this->row(['online_order_no' => '1234567890123456790'])];
|
|
$result = $service->apply($rows, 1, '2026-11');
|
|
$this->assertSame(array_replace($rows[0], ['task_payment' => '20.50', 'reimbursement_amount' => '28.50']), $result[0]);
|
|
$this->assertSame($rows[1], $result[1]);
|
|
$this->assertSame($rows[2], $result[2]);
|
|
$this->assertSame($rows[0]['task_payment'], '35.00');
|
|
}
|
|
}
|
|
|
|
public function test_corrections_are_isolated_by_company_and_month(): void
|
|
{
|
|
$service = $this->service([PlantingReimbursementCorrections::path(1, '2026-11') => $this->document()]);
|
|
$rows = [$this->row()];
|
|
$this->assertSame($rows, $service->apply($rows, 2, '2026-11'));
|
|
$this->assertSame($rows, $service->apply($rows, 1, '2026-10'));
|
|
}
|
|
|
|
public function test_zero_amount_is_retained(): void
|
|
{
|
|
$service = $this->service([PlantingReimbursementCorrections::path(1, '2026-11') => $this->document(['task_payment' => '0.00', 'reimbursement_amount' => '8.00'])]);
|
|
$result = $service->apply([$this->row()], 1, '2026-11');
|
|
$this->assertSame('0.00', $result[0]['task_payment']);
|
|
$this->assertSame('8.00', $result[0]['reimbursement_amount']);
|
|
}
|
|
|
|
public function test_invalid_amount_does_not_silently_show_the_uncorrected_amount(): void
|
|
{
|
|
$service = $this->service([PlantingReimbursementCorrections::path(1, '2026-11') => $this->document(['reimbursement_amount' => null])]);
|
|
$this->expectException(RuntimeException::class);
|
|
$service->apply([$this->row()], 1, '2026-11');
|
|
}
|
|
}
|