| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace Webkul\BagistoApi\Tests\Unit\Migration;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- use Longyi\Core\Models\ProductOption;
- use Longyi\Core\Models\ProductOptionValue;
- use Longyi\Core\Models\ProductVariant;
- use Webkul\BagistoApi\Tests\BagistoApiTestCase;
- use Webkul\Product\Models\Product;
- class RepairMigratedVariantSkusCommandTest extends BagistoApiTestCase
- {
- public function setUp(): void
- {
- parent::setUp();
- if (! Schema::hasColumn('products', 'migrated_from_asteria_id')
- || ! Schema::hasTable('product_variants')
- || ! Schema::hasTable('product_variant_option_values')
- ) {
- $this->markTestSkipped('Flexible variant tables are missing.');
- }
- }
- public function test_it_rewrites_sort_order_skus_from_option_value_labels(): void
- {
- [$product, $variant] = $this->seedVariant('REPAIR-WIG-001', 'REPAIR-WIG-001-1-1', 991900001);
- $variantId = (int) $variant->id;
- $this->artisan('products:repair-variant-skus', [
- '--sku' => 'REPAIR-WIG-001',
- ])->assertSuccessful();
- $fresh = ProductVariant::query()->find($variantId);
- $this->assertNotNull($fresh);
- $this->assertSame($variantId, (int) $fresh->id);
- $this->assertSame('REPAIR-WIG-001-12IN-1B', $fresh->sku);
- $this->assertSame('REPAIR-WIG-001', $product->fresh()->sku);
- }
- public function test_dry_run_does_not_write_skus(): void
- {
- [, $variant] = $this->seedVariant('REPAIR-WIG-002', 'REPAIR-WIG-002-1-1', 991900002);
- $this->artisan('products:repair-variant-skus', [
- '--sku' => 'REPAIR-WIG-002',
- '--dry-run' => true,
- ])->assertSuccessful();
- $this->assertSame('REPAIR-WIG-002-1-1', $variant->fresh()->sku);
- }
- public function test_it_leaves_products_that_were_not_migrated_from_asteria(): void
- {
- [, $variant] = $this->seedVariant('REPAIR-WIG-003', 'REPAIR-WIG-003-1-1', null);
- $this->artisan('products:repair-variant-skus', [
- '--sku' => 'REPAIR-WIG-003',
- ])->assertSuccessful();
- $this->assertSame('REPAIR-WIG-003-1-1', $variant->fresh()->sku);
- }
- public function test_it_rewrites_order_invoice_and_shipment_skus(): void
- {
- [$product, $variant] = $this->seedVariant('REPAIR-WIG-004', 'REPAIR-WIG-004-1-1', 991900004);
- $newSku = 'REPAIR-WIG-004-12IN-1B';
- $now = now();
- $orderId = DB::table('orders')->insertGetId([
- 'increment_id' => 'REPAIR-ORD-004',
- 'status' => 'processing',
- 'is_guest' => 1,
- 'created_at' => $now,
- 'updated_at' => $now,
- ]);
- $nativeId = DB::table('order_items')->insertGetId([
- 'order_id' => $orderId,
- 'product_id' => $product->id,
- 'sku' => 'REPAIR-WIG-004-1-1',
- 'name' => 'Wig',
- 'qty_ordered' => 1,
- 'additional' => json_encode([
- 'variant_id' => $variant->id,
- 'variant_sku' => 'REPAIR-WIG-004-1-1',
- 'attributes' => [
- 'hair_length' => [
- 'attribute_name' => 'Hair Length',
- 'option_label' => 'Hair Length',
- 'value_label' => '12"',
- ],
- 'hair_color' => [
- 'attribute_name' => 'Hair Color',
- 'option_label' => 'Hair Color',
- 'value_label' => 'Natural Black',
- ],
- ],
- ], JSON_UNESCAPED_UNICODE),
- 'created_at' => $now,
- 'updated_at' => $now,
- ]);
- $migratedId = DB::table('order_items')->insertGetId([
- 'order_id' => $orderId,
- 'product_id' => $product->id,
- 'sku' => 'REPAIR-WIG-004',
- 'name' => 'Wig',
- 'qty_ordered' => 1,
- 'additional' => json_encode([
- 'attributes' => [
- ['attribute_name' => 'Hair Length', 'option_label' => '12"', 'attribute_type' => 'drop_down'],
- ['attribute_name' => 'Free Gift', 'option_label' => 'Yes', 'attribute_type' => 'drop_down'],
- ['attribute_name' => 'Hair Color', 'option_label' => 'Natural Black', 'attribute_type' => 'drop_down'],
- ],
- ], JSON_UNESCAPED_UNICODE),
- 'created_at' => $now,
- 'updated_at' => $now,
- ]);
- $invoiceId = DB::table('invoice_items')->insertGetId([
- 'order_item_id' => $nativeId,
- 'sku' => 'REPAIR-WIG-004-1-1',
- 'name' => 'Wig',
- 'qty' => 1,
- 'created_at' => $now,
- 'updated_at' => $now,
- ]);
- $shipmentId = DB::table('shipment_items')->insertGetId([
- 'shipment_id' => 1,
- 'order_item_id' => $migratedId,
- 'sku' => 'REPAIR-WIG-004',
- 'name' => 'Wig',
- 'qty' => 1,
- 'created_at' => $now,
- 'updated_at' => $now,
- ]);
- $this->artisan('products:repair-variant-skus', [
- '--sku' => 'REPAIR-WIG-004',
- '--dry-run' => true,
- ])->assertSuccessful();
- $this->assertSame('REPAIR-WIG-004-1-1', DB::table('order_items')->where('id', $nativeId)->value('sku'));
- $this->artisan('products:repair-variant-skus', [
- '--sku' => 'REPAIR-WIG-004',
- ])->assertSuccessful();
- $this->assertSame($newSku, $variant->fresh()->sku);
- $this->assertSame($newSku, DB::table('order_items')->where('id', $nativeId)->value('sku'));
- $this->assertSame($newSku, DB::table('order_items')->where('id', $migratedId)->value('sku'));
- $this->assertSame($newSku, DB::table('invoice_items')->where('id', $invoiceId)->value('sku'));
- $this->assertSame($newSku, DB::table('shipment_items')->where('id', $shipmentId)->value('sku'));
- $nativeAdditional = json_decode((string) DB::table('order_items')->where('id', $nativeId)->value('additional'), true);
- $this->assertSame($newSku, $nativeAdditional['variant_sku']);
- $migratedAdditional = json_decode((string) DB::table('order_items')->where('id', $migratedId)->value('additional'), true);
- $this->assertArrayNotHasKey('variant_sku', $migratedAdditional);
- }
- /**
- * @return array{0: Product, 1: ProductVariant}
- */
- private function seedVariant(string $parentSku, string $variantSku, ?int $asteriaId): array
- {
- $suffix = str_replace('-', '_', strtolower($parentSku));
- $product = new Product;
- $product->sku = $parentSku;
- $product->type = 'flexible_variant';
- $product->migrated_from_asteria_id = $asteriaId;
- $product->save();
- $length = ProductOption::query()->create([
- 'label' => 'Hair Length',
- 'type' => 'select',
- 'code' => $suffix.'_length',
- ]);
- $color = ProductOption::query()->create([
- 'label' => 'Hair Color',
- 'type' => 'select',
- 'code' => $suffix.'_color',
- ]);
- $twelve = ProductOptionValue::query()->create([
- 'product_option_id' => $length->id,
- 'label' => '12"',
- 'code' => '12IN',
- 'position' => 1,
- ]);
- $black = ProductOptionValue::query()->create([
- 'product_option_id' => $color->id,
- 'label' => 'Natural Black',
- 'code' => '1B',
- 'position' => 1,
- ]);
- $now = now();
- DB::table('product_product_options')->insert([
- [
- 'product_id' => $product->id,
- 'product_option_id' => $length->id,
- 'position' => 0,
- 'is_required' => 1,
- 'created_at' => $now,
- 'updated_at' => $now,
- ],
- [
- 'product_id' => $product->id,
- 'product_option_id' => $color->id,
- 'position' => 1,
- 'is_required' => 1,
- 'created_at' => $now,
- 'updated_at' => $now,
- ],
- ]);
- $variant = ProductVariant::query()->create([
- 'product_id' => $product->id,
- 'sku' => $variantSku,
- 'price' => 99,
- 'quantity' => 1,
- 'status' => 1,
- 'sort_order' => 0,
- ]);
- DB::table('product_variant_option_values')->insert([
- [
- 'product_variant_id' => $variant->id,
- 'product_option_value_id' => $twelve->id,
- 'created_at' => $now,
- 'updated_at' => $now,
- ],
- [
- 'product_variant_id' => $variant->id,
- 'product_option_value_id' => $black->id,
- 'created_at' => $now,
- 'updated_at' => $now,
- ],
- ]);
- return [$product, $variant];
- }
- }
|