76 lines
3.3 KiB
PHP
76 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Console;
|
||
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||
use Tests\TestCase;
|
||
|
||
class ImportKukaErpRecipientsTest extends TestCase
|
||
{
|
||
public function test_command_imports_workbook_rows_and_replaces_previous_pool(): void
|
||
{
|
||
config([
|
||
'database.connections.business_mirror' => [
|
||
'driver' => 'sqlite',
|
||
'database' => ':memory:',
|
||
'prefix' => '',
|
||
],
|
||
'upstream_mirrors.connection' => 'business_mirror',
|
||
]);
|
||
DB::purge('business_mirror');
|
||
Schema::connection('business_mirror')->create('kuka_posting_erp_recipient_rows', function (Blueprint $table): void {
|
||
$table->bigIncrements('id');
|
||
$table->unsignedBigInteger('company_id')->default(1);
|
||
$table->unsignedInteger('sort_order');
|
||
$table->string('receiver_name');
|
||
$table->string('receiver_mobile');
|
||
$table->string('receiver_state');
|
||
$table->string('receiver_city');
|
||
$table->string('receiver_district');
|
||
$table->string('receiver_address');
|
||
$table->string('buyer_nick');
|
||
$table->unsignedInteger('source_row_number')->nullable();
|
||
$table->char('row_hash', 64);
|
||
$table->timestamp('imported_at');
|
||
$table->timestamps();
|
||
});
|
||
|
||
$spreadsheet = new Spreadsheet();
|
||
$sheet = $spreadsheet->getActiveSheet();
|
||
$sheet->setTitle('Sheet1');
|
||
$sheet->fromArray([
|
||
['收货人(必填)', '收货人手机(必填)', '收货人的所在省份(必填)', '收货人的所在城市(必填)', '收货人的所在区(县)(必填)', '收货地址(必填)', '买家昵称(必填)'],
|
||
['clientName', 'clientMobile', 'clientProvince', 'clientCity', 'clientArea', 'clientAddress', 'buyerNick'],
|
||
['测试甲', '13800000001', '江苏省', '常州市', '武进区', '测试路1号', '昵称甲'],
|
||
['测试乙', '13800000002', '江苏省', '常州市', '新北区', '测试路2号', '昵称乙'],
|
||
]);
|
||
$path = tempnam(sys_get_temp_dir(), 'kuka-recipients-');
|
||
(new Xlsx($spreadsheet))->save($path);
|
||
|
||
try {
|
||
$this->artisan('kuka-posting:import-erp-recipients', ['file' => $path])
|
||
->expectsOutput('已导入 ERP 收货人池:2 条。')
|
||
->assertSuccessful();
|
||
$this->artisan('kuka-posting:import-erp-recipients', ['file' => $path])
|
||
->assertSuccessful();
|
||
|
||
$rows = DB::connection('business_mirror')
|
||
->table('kuka_posting_erp_recipient_rows')
|
||
->orderBy('sort_order')
|
||
->get();
|
||
$this->assertCount(2, $rows);
|
||
$this->assertSame('测试甲', $rows[0]->receiver_name);
|
||
$this->assertSame('13800000001', $rows[0]->receiver_mobile);
|
||
$this->assertSame(3, (int) $rows[0]->source_row_number);
|
||
$this->assertSame('测试乙', $rows[1]->receiver_name);
|
||
$this->assertSame('昵称乙', $rows[1]->buyer_nick);
|
||
} finally {
|
||
@unlink($path);
|
||
}
|
||
}
|
||
}
|