CaiWuXiTong/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationRepositoryTest.php

90 lines
5.0 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository as Repository;
use Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository;
use Tests\TestCase;
class SupplierBookkeepingValuationRepositoryTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['jushuitan.connection' => 'business_mirror', 'upstream_mirrors.connection' => 'business_mirror']);
// The production dataset key contains dots; configure the full dataset map directly.
config(['upstream_mirrors.datasets' => [ProductCatalogMirrorRepository::DATASET_KEY => [
'source_key' => 'operations_replica', 'schema' => 'rpt', 'object' => 'jst_sku',
'target' => 'fixture_valuation_skus', 'keys' => ['autoid'],
]]]);
$schema = Schema::connection('business_mirror');
if (!$schema->hasTable('jst_oms_shipping')) {
$schema->create('jst_oms_shipping', function (Blueprint $table) {
$table->id();
$table->integer('source_id');
foreach (['express_no', 'trade_no', 'barcode', 'qty', 'freight', 'ship_time'] as $column) $table->string($column)->nullable();
$table->index('express_no');
$table->index('trade_no');
});
}
$schema->create('fixture_valuation_skus', function (Blueprint $table) {
$table->integer('autoid');
foreach (['_mirror_key', 'sku_id', 'sku_type', 'cost_price'] as $column) $table->string($column)->nullable();
});
if (!$schema->hasTable('business_mirror_table_states')) {
$schema->create('business_mirror_table_states', function (Blueprint $table) {
$table->string('dataset_key')->primary();
$table->dateTime('initial_completed_at')->nullable();
});
}
DB::connection('business_mirror')->table('business_mirror_table_states')->updateOrInsert(
['dataset_key' => ProductCatalogMirrorRepository::DATASET_KEY], ['initial_completed_at' => '2026-10-01 00:00:00']);
}
public function test_queries_exact_references_and_six_month_dates_without_borrowing_same_order_packages(): void
{
$db = DB::connection('business_mirror');
foreach ([['EXP-1', '2026-05-01'], ['EXP-1', '2026-04-30'], ['EXP-1', '2026-11-01'], ['EXP-2', '2026-10-20']] as $i => [$tracking, $date]) {
$db->table('jst_oms_shipping')->insert(['source_id' => $i+1, 'express_no' => $tracking, 'trade_no' => 'ORDER-1',
'barcode' => 'SKU-A', 'qty' => 1, 'freight' => '8.50', 'ship_time' => $date . ' 00:00:00']);
}
$repo = app(Repository::class);
$rows = $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => 'EXP-1', 'online_order_no' => 'ORDER-1']]);
$this->assertCount(1, $rows);
$this->assertSame('EXP-1', $rows[0]['express_no']);
$all = $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => '', 'online_order_no' => 'ORDER-1']]);
$this->assertCount(2, $all);
$this->assertSame([], $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => '', 'online_order_no' => '']]));
}
public function test_uses_latest_normal_product_cost_and_decodes_sku_plus_without_treating_missing_as_zero(): void
{
DB::connection('business_mirror')->table('fixture_valuation_skus')->insert([
['autoid' => 1, '_mirror_key' => '1', 'sku_id' => 'SKU-A', 'sku_type' => 'normal', 'cost_price' => '10.00'],
['autoid' => 2, '_mirror_key' => '2', 'sku_id' => 'SKU-A', 'sku_type' => 'normal', 'cost_price' => '20.00'],
['autoid' => 3, '_mirror_key' => '3', 'sku_id' => 'SKU-A', 'sku_type' => 'combination', 'cost_price' => '90.00'],
['autoid' => 4, '_mirror_key' => '4', 'sku_id' => 'SKU_X002B_B', 'sku_type' => 'normal', 'cost_price' => '15.00'],
]);
$costs = app(Repository::class)->costsFor(['SKU-A', 'SKU+B', 'MISSING']);
$this->assertSame('20.00', $costs['SKU-A']);
$this->assertSame('15.00', $costs['SKU+B']);
$this->assertArrayNotHasKey('MISSING', $costs);
}
public function test_latest_cost_wins_when_encoded_sku_variant_falls_in_another_query_batch(): void
{
DB::connection('business_mirror')->table('fixture_valuation_skus')->insert([
['autoid' => 1, '_mirror_key' => '1', 'sku_id' => 'SKU+B', 'sku_type' => 'normal', 'cost_price' => '10.00'],
['autoid' => 2, '_mirror_key' => '2', 'sku_id' => 'SKU_X002B_B', 'sku_type' => 'normal', 'cost_price' => '25.00'],
]);
// 74 pairs + one uppercase-only numeric SKU place SKU+B at the 150-value boundary.
$skus = array_map(static fn($i) => 'ITEM-'.$i, range(1, 74));
$skus[] = '123';
$skus[] = 'SKU+B';
$this->assertSame('25.00', app(Repository::class)->costsFor($skus)['SKU+B']);
}
}