CaiWuXiTong/tests/Feature/Business/CourierStockQuoteBatchTest.php

228 lines
13 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use App\Models\Business\CourierQuoteChange;
use App\Models\Business\CourierStockQuoteEntry;
use App\Support\CourierStockQuoteBatchService;
use App\Support\QuoteManagementSnapshot;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Modules\EcommerceFinance\Repositories\CourierInformationMirrorRepository;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Tests\Feature\FeatureTestCase;
class CourierStockQuoteBatchTest extends FeatureTestCase
{
private array $temporaryFiles = [];
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
$this->mirror([
['code' => 'EXP-001', 'name' => '同名网点'], ['code' => 'EXP-002', 'name' => '同名网点'],
['code' => 'EXP-003', 'name' => '第三网点'],
]);
app()->instance(QuoteManagementSnapshot::class, new QuoteManagementSnapshot('/nonexistent-batch-quotes.json'));
}
protected function tearDown(): void
{
foreach ($this->temporaryFiles as $path) {
if (is_file($path)) {
unlink($path);
}
}
parent::tearDown();
}
public function test_preview_keeps_workbook_without_creating_quotes_and_never_guesses_ambiguous_names(): void
{
$this->loginAs()->get(route('business.courier-stock-quotes.create'))->assertOk()
->assertSeeText('共用计费规则')->assertSeeText('只补录一家');
$batch = $this->preview(['EXP-001', '同名网点', '第三网点', '说明']);
$this->get(route('business.courier-stock-quotes.batch', $batch))->assertOk()
->assertSeeText('名称重复,请选编号')->assertSeeText('保存并提交审核');
$service = app(CourierStockQuoteBatchService::class);
$manifest = $service->load($batch, (int) $this->user->id);
$rows = $service->rows($manifest, app(\App\Support\CourierStockQuoteEntryService::class)->courierOptions());
$this->assertSame(['EXP-001', '', 'EXP-003', ''], array_column($rows, 'courier_expcode'));
$this->assertSame('=B2/2', $manifest['sheets'][0]['formulas'][1][2]);
Storage::disk('uploads')->assertExists($manifest['source_file']['path']);
$this->assertDatabaseCount('business_courier_stock_quote_entries', 0);
$this->assertDatabaseCount('business_courier_quote_changes', 0);
}
public function test_bulk_save_reuses_rule_groups_overrides_dates_and_preserves_separate_prices(): void
{
$batch = $this->preview(['EXP-001', 'EXP-002', '说明']);
$selection = $this->selection(['EXP-001', 'EXP-002', '']);
$selection['entries'][2]['selected'] = false;
$selection['rule_groups'][] = ['name' => '单独规则', 'rules' => '超长件按实际重量计费'];
$selection['entries'][1]['rule_group'] = 1;
$selection['entries'][1]['effective_date'] = '2025-11-01';
$url = route('business.courier-stock-quotes.batch-save', $batch);
$this->loginAs()->post($url, ['selection_json' => json_encode($selection)])->assertRedirect();
$entries = CourierStockQuoteEntry::with('change')->orderBy('id')->get();
$this->assertCount(2, $entries);
$this->assertSame('按原表重量段计费,面单另计', $entries[0]->change->quote_payload['billing_rules']);
$this->assertSame('超长件按实际重量计费', $entries[1]->change->quote_payload['billing_rules']);
$this->assertSame('2025-11-01', $entries[1]->change->effective_date->toDateString());
$this->assertEquals(5, $entries[0]->sourceQuote()['values'][1][1]);
$this->assertEquals(6, $entries[1]->sourceQuote()['values'][1][1]);
$this->assertSame($entries[0]->files[0]['path'], $entries[1]->files[0]['path']);
$this->assertSame(CourierQuoteChange::STATUS_DRAFT, $entries[0]->change->status);
$this->assertDatabaseCount('business_courier_quote_versions', 0);
$this->get(route('business.courier-stock-quotes.batch', $batch))->assertOk()->assertSeeText('已录入');
// 同一预览重发只返回原结果,不生成重复记录。
$this->post($url, ['selection_json' => json_encode($selection)])->assertRedirect();
$this->assertDatabaseCount('business_courier_stock_quote_entries', 2);
}
public function test_bulk_submit_requires_confirmation_and_does_not_approve_or_fake_signatures(): void
{
$batch = $this->preview(['EXP-001', 'EXP-002']);
$selection = [...$this->selection(['EXP-001', 'EXP-002']), 'submit_for_review' => true, 'confirmed' => false];
$url = route('business.courier-stock-quotes.batch-save', $batch);
$this->withExceptionHandling()->loginAs()->post($url, ['selection_json' => json_encode($selection)])->assertSessionHasErrors('confirmed');
$this->assertDatabaseCount('business_courier_stock_quote_entries', 0);
$selection['confirmed'] = true;
$this->post($url, ['selection_json' => json_encode($selection)])->assertSessionHasNoErrors()->assertRedirect();
$this->assertSame([CourierQuoteChange::STATUS_PENDING_REVIEW], CourierQuoteChange::pluck('status')->unique()->all());
$this->assertDatabaseCount('business_courier_quote_versions', 0);
$this->assertDatabaseCount('business_courier_quote_signatures', 0);
}
public function test_row_errors_are_atomic_keep_preview_and_do_not_override_existing_quotes(): void
{
$batch = $this->preview(['EXP-001', 'EXP-002']);
$selection = $this->selection(['EXP-001', 'EXP-001']);
$this->withExceptionHandling()->loginAs()->from(route('business.courier-stock-quotes.batch', $batch))
->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($selection)])
->assertSessionHasErrors('entries.1')->assertSessionHasInput('selection_json');
$this->assertDatabaseCount('business_courier_stock_quote_entries', 0);
$selection['entries'][1]['courier_expcode'] = 'EXP-002';
$this->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($selection)])->assertSessionHasNoErrors();
$another = $this->preview(['EXP-001']);
$this->post(route('business.courier-stock-quotes.batch-save', $another), ['selection_json' => json_encode($this->selection(['EXP-001']))])
->assertSessionHasErrors('entries.0');
$this->assertDatabaseCount('business_courier_stock_quote_entries', 2);
$this->get(route('business.courier-stock-quotes.batch', $another))->assertOk()->assertSeeText('已录入,默认跳过');
}
public function test_real_date_and_known_courier_are_required_but_notes_can_be_skipped(): void
{
$batch = $this->preview(['EXP-001', '说明']);
$selection = $this->selection(['EXP-001', 'UNKNOWN']);
$selection['effective_date'] = '';
$this->withExceptionHandling()->loginAs()->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($selection)])
->assertSessionHasErrors(['entries.0', 'entries.1']);
$this->assertDatabaseCount('business_courier_stock_quote_entries', 0);
$selection['entries'][0]['effective_date'] = '2024-06-01';
$selection['entries'][1]['selected'] = false;
$this->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($selection)])->assertSessionHasNoErrors();
$this->assertDatabaseCount('business_courier_stock_quote_entries', 1);
}
public function test_preview_is_scoped_to_company_and_uploader_and_rejects_fake_sheet_indices(): void
{
$batch = $this->preview(['EXP-001']);
$manifest = app(CourierStockQuoteBatchService::class)->load($batch, (int) $this->user->id);
$manifestPath = dirname($manifest['source_file']['path']) . '/manifest.json';
$selection = $this->selection(['EXP-001']);
$selection['entries'] = [50 => $selection['entries'][0]];
$this->withExceptionHandling()->loginAs()->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($selection)])
->assertSessionHasErrors('entries.50');
$manifest['user_id'] = (int) $this->user->id + 1;
Storage::disk('uploads')->put($manifestPath, json_encode($manifest));
$this->get(route('business.courier-stock-quotes.batch', $batch), ['X-Requested-With' => 'XMLHttpRequest'])->assertNotFound();
$manifest['user_id'] = (int) $this->user->id;
$manifest['company_id'] = (int) company_id() + 1;
Storage::disk('uploads')->put($manifestPath, json_encode($manifest));
$this->get(route('business.courier-stock-quotes.batch', $batch), ['X-Requested-With' => 'XMLHttpRequest'])->assertNotFound();
}
public function test_one_hundred_twenty_sheets_preview_and_save_as_one_batch(): void
{
$couriers = $names = [];
for ($index = 1; $index <= 120; $index++) {
$names[] = $code = sprintf('BULK-%03d', $index);
$couriers[] = ['code' => $code, 'name' => '批量网点' . $index];
}
$this->mirror($couriers);
$batch = $this->preview($names);
$this->get(route('business.courier-stock-quotes.batch', $batch))->assertOk()->assertSeeText('120');
$this->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($this->selection($names))])->assertRedirect();
$this->assertDatabaseCount('business_courier_stock_quote_entries', 120);
$this->assertDatabaseCount('business_courier_quote_changes', 120);
$this->assertCount(1, CourierStockQuoteEntry::all()->pluck('files')->map(fn ($files) => $files[0]['path'])->unique());
}
public function test_empty_sheet_can_be_skipped_and_failed_result_storage_rolls_back_the_entire_batch(): void
{
$batch = $this->preview(['EXP-001', '空表']);
$this->get(route('business.courier-stock-quotes.batch', $batch))->assertOk()->assertSeeText('空工作表,请取消选择');
$selection = $this->selection(['EXP-001', 'EXP-002']);
$this->withExceptionHandling()->loginAs()->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($selection)])
->assertSessionHasErrors('entries.1');
$selection['entries'][1]['selected'] = false;
$disk = Storage::disk('uploads');
$failingDisk = \Mockery::mock($disk);
$failingDisk->shouldReceive('put')->once()->andReturn(false);
Storage::shouldReceive('disk')->with('uploads')->andReturn($failingDisk);
$this->post(route('business.courier-stock-quotes.batch-save', $batch), ['selection_json' => json_encode($selection)])
->assertSessionHasErrors('source_quote');
$this->assertDatabaseCount('business_courier_stock_quote_entries', 0);
$this->assertDatabaseCount('business_courier_quote_changes', 0);
}
private function preview(array $names): string
{
$response = $this->loginAs()->post(route('business.courier-stock-quotes.batch-preview'), [
'source_quote' => $this->workbook($names), 'quote_type' => '标准快递报价',
'effective_date' => '2025-12-01', 'billing_rules' => '按原表重量段计费,面单另计',
])->assertRedirect();
return basename($response->headers->get('Location'));
}
private function selection(array $codes): array
{
return ['quote_type' => '标准快递报价', 'effective_date' => '2025-12-01', 'summary' => '整本报价历史补录',
'rule_groups' => [['name' => '共用规则', 'rules' => '按原表重量段计费,面单另计']],
'submit_for_review' => false, 'confirmed' => false,
'entries' => array_map(fn ($code) => ['selected' => true, 'courier_expcode' => $code, 'rule_group' => 0, 'effective_date' => ''], $codes),
];
}
private function workbook(array $names): UploadedFile
{
$book = new Spreadsheet();
foreach ($names as $index => $name) {
$sheet = ($index === 0 ? $book->getActiveSheet() : $book->createSheet())->setTitle($name);
if ($name === '空表') {
continue;
}
$sheet->fromArray([['省份', '首重', '续重'], ['江苏省', 5 + $index, null]]);
$sheet->setCellValue('C2', '=B2/2');
}
$path = tempnam(sys_get_temp_dir(), 'batch-quote-');
$this->temporaryFiles[] = $path;
IOFactory::createWriter($book, 'Xlsx')->save($path);
$book->disconnectWorksheets();
return new UploadedFile($path, '整本存量报价.xlsx', null, null, true);
}
private function mirror(array $couriers): void
{
$mirror = \Mockery::mock(CourierInformationMirrorRepository::class);
$mirror->shouldReceive('overviewSource')->andReturn(['express' => array_map(fn ($courier) => (object) ['expcode' => $courier['code'], 'expname' => $courier['name']], $couriers)]);
app()->instance(CourierInformationMirrorRepository::class, $mirror);
}
}