CaiWuXiTong/tests/Unit/Business/PlantingOrderLabelLookupTest.php

74 lines
3.2 KiB
PHP

<?php
namespace Tests\Unit\Business;
use App\Business\JstOrderLabelPlantingRules;
use App\Business\PlantingOrderLabelLookup;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class PlantingOrderLabelLookupTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
DB::connection()->getPdo()->sqliteCreateFunction('FIND_IN_SET', static function ($value, $list): int {
$index = array_search($value, explode(',', (string) $list), true);
return $index === false ? 0 : $index + 1;
});
}
public function test_reuses_the_exact_company_label_or_item_rule_without_scanning_again(): void
{
$this->rows([
[1, '000123', 'positive', null], [1, '000123', 'positive', null],
[1, '260901-ABC', 'other', 'A,X00006'], [1, 'NO', 'other', 'X000060'],
[2, 'OTHER-COMPANY', 'positive', null],
]);
$lookup = app(PlantingOrderLabelLookup::class);
$result = $lookup->orderNumbers(1, ['positive']);
sort($result);
$this->assertSame(['000123', '260901-ABC'], $result);
DB::connection()->enableQueryLog();
$this->assertSame($result, tap($lookup->orderNumbers(1, ['positive']), static function (&$rows): void { sort($rows); }));
$this->assertCount(0, array_filter(DB::connection()->getQueryLog(), static fn ($q) => str_contains($q['query'], 'from "ak_jst_order_labels"')));
}
public function test_shared_revision_invalidates_another_instances_local_cache(): void
{
$this->rows([[1, 'A', 'positive', null], [1, 'B', 'other', null]]);
$lookup = app(PlantingOrderLabelLookup::class);
$this->assertSame(['A'], $lookup->orderNumbers(1, ['positive']));
DB::table('jst_order_labels')->where('order_no', 'B')->update(['item_code' => 'X00006']);
// A different workstation changes the shared revision; local file cache remains.
Cache::store('upstream_shared')->forever('planting-order-labels:v1:1:revision', 'other-instance');
$result = $lookup->orderNumbers(1, ['positive']);
sort($result);
$this->assertSame(['A', 'B'], $result);
}
public function test_rule_changes_and_item_only_classification_do_not_reuse_old_results(): void
{
$this->rows([[1, 'A', 'positive', null], [1, 'B', 'other', 'X00006']]);
$lookup = app(PlantingOrderLabelLookup::class);
$this->assertCount(2, $lookup->orderNumbers(1, ['positive']));
$this->assertSame(['B'], $lookup->orderNumbers(1, []));
DB::table('jst_order_labels')->where('order_no', 'B')->update(['item_code' => null]);
$lookup->forget(1);
$this->assertSame([], $lookup->orderNumbers(1, []));
}
private function rows(array $rows): void
{
foreach ($rows as [$company, $number, $label, $code]) {
DB::table('jst_order_labels')->insert([
'company_id' => $company, 'order_no' => $number, 'label' => $label,
'item_code' => $code, 'shop_name' => 'test', 'ordered_at' => '2026-09-01 00:00:00',
'created_at' => now(), 'updated_at' => now(),
]);
}
}
}