CaiWuXiTong/tests/Unit/EcommerceFinance/SupplierBookkeepingValuationRepositoryTest.php

100 lines
6.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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('supplier_bookkeeping_shipping_months')) {
(require base_path('database/migrations/2026_11_07_100000_create_supplier_bookkeeping_shipping_mirror.php'))->up();
}
$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');
$db->table('supplier_bookkeeping_shipping_months')->insert(['source_month' => '2026-10-01', 'active_generation' => 'active']);
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('supplier_bookkeeping_shipping_rows')->insert(['generation' => 'active', 'source_row' => $i+2,
'express_no' => $tracking, 'old_express_no' => 'OLD-'.$tracking, 'trade_no' => 'ORDER-1', 'package_no' => '', 'unshipped' => '',
'barcode' => 'SKU-A', 'qty' => 1, 'freight' => '8.50', 'ship_time' => $date . ' 00:00:00']);
foreach (['tracking' => [$tracking, 'OLD-'.$tracking], 'order' => ['ORDER-1']] as $kind => $refs) foreach ($refs as $ref) {
$db->table('supplier_bookkeeping_shipping_refs')->insert(['generation' => 'active', 'source_row' => $i+2, 'kind' => $kind, 'reference' => $ref]);
}
}
$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']);
$this->assertCount(1, $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => 'OLD-EXP-1']]));
$this->assertCount(1, $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => " '@EXP-1 ", 'online_order_no' => 'ORDER-1']]));
$this->assertSame(['2026-10'], $repo->readyMonths());
$all = $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => '', 'online_order_no' => 'ORDER-1']]);
$this->assertCount(2, $all);
$this->assertCount(2, $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => " '@ ", 'online_order_no' => '@ORDER-1’']]));
$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_cleans_only_outer_copy_symbols_and_preserves_identifier_content(): void
{
$this->assertSame(['JDVB001', '0001234567890123456789', 'SKU-A+B', 'A@B'], Repository::numbers("\u{FEFF}'@JDVB001’ , '@0001234567890123456789";\u{200B}@SKU-A+B\u{00A0}\nA@B"));
$this->assertSame('SKU-A+B', Repository::sku(" ’@sku-a_x002b_b' "));
$this->assertSame([], Repository::numbers(" '@,@’ "));
}
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']);
}
}