RepairMigratedVariantSkusCommandTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Schema;
  5. use Longyi\Core\Models\ProductOption;
  6. use Longyi\Core\Models\ProductOptionValue;
  7. use Longyi\Core\Models\ProductVariant;
  8. use Webkul\BagistoApi\Tests\BagistoApiTestCase;
  9. use Webkul\Product\Models\Product;
  10. class RepairMigratedVariantSkusCommandTest extends BagistoApiTestCase
  11. {
  12. public function setUp(): void
  13. {
  14. parent::setUp();
  15. if (! Schema::hasColumn('products', 'migrated_from_asteria_id')
  16. || ! Schema::hasTable('product_variants')
  17. || ! Schema::hasTable('product_variant_option_values')
  18. ) {
  19. $this->markTestSkipped('Flexible variant tables are missing.');
  20. }
  21. }
  22. public function test_it_rewrites_sort_order_skus_from_option_value_labels(): void
  23. {
  24. [$product, $variant] = $this->seedVariant('REPAIR-WIG-001', 'REPAIR-WIG-001-1-1', 991900001);
  25. $variantId = (int) $variant->id;
  26. $this->artisan('products:repair-variant-skus', [
  27. '--sku' => 'REPAIR-WIG-001',
  28. ])->assertSuccessful();
  29. $fresh = ProductVariant::query()->find($variantId);
  30. $this->assertNotNull($fresh);
  31. $this->assertSame($variantId, (int) $fresh->id);
  32. $this->assertSame('REPAIR-WIG-001-12IN-1B', $fresh->sku);
  33. $this->assertSame('REPAIR-WIG-001', $product->fresh()->sku);
  34. }
  35. public function test_dry_run_does_not_write_skus(): void
  36. {
  37. [, $variant] = $this->seedVariant('REPAIR-WIG-002', 'REPAIR-WIG-002-1-1', 991900002);
  38. $this->artisan('products:repair-variant-skus', [
  39. '--sku' => 'REPAIR-WIG-002',
  40. '--dry-run' => true,
  41. ])->assertSuccessful();
  42. $this->assertSame('REPAIR-WIG-002-1-1', $variant->fresh()->sku);
  43. }
  44. public function test_it_leaves_products_that_were_not_migrated_from_asteria(): void
  45. {
  46. [, $variant] = $this->seedVariant('REPAIR-WIG-003', 'REPAIR-WIG-003-1-1', null);
  47. $this->artisan('products:repair-variant-skus', [
  48. '--sku' => 'REPAIR-WIG-003',
  49. ])->assertSuccessful();
  50. $this->assertSame('REPAIR-WIG-003-1-1', $variant->fresh()->sku);
  51. }
  52. public function test_it_rewrites_order_invoice_and_shipment_skus(): void
  53. {
  54. [$product, $variant] = $this->seedVariant('REPAIR-WIG-004', 'REPAIR-WIG-004-1-1', 991900004);
  55. $newSku = 'REPAIR-WIG-004-12IN-1B';
  56. $now = now();
  57. $orderId = DB::table('orders')->insertGetId([
  58. 'increment_id' => 'REPAIR-ORD-004',
  59. 'status' => 'processing',
  60. 'is_guest' => 1,
  61. 'created_at' => $now,
  62. 'updated_at' => $now,
  63. ]);
  64. $nativeId = DB::table('order_items')->insertGetId([
  65. 'order_id' => $orderId,
  66. 'product_id' => $product->id,
  67. 'sku' => 'REPAIR-WIG-004-1-1',
  68. 'name' => 'Wig',
  69. 'qty_ordered' => 1,
  70. 'additional' => json_encode([
  71. 'variant_id' => $variant->id,
  72. 'variant_sku' => 'REPAIR-WIG-004-1-1',
  73. 'attributes' => [
  74. 'hair_length' => [
  75. 'attribute_name' => 'Hair Length',
  76. 'option_label' => 'Hair Length',
  77. 'value_label' => '12"',
  78. ],
  79. 'hair_color' => [
  80. 'attribute_name' => 'Hair Color',
  81. 'option_label' => 'Hair Color',
  82. 'value_label' => 'Natural Black',
  83. ],
  84. ],
  85. ], JSON_UNESCAPED_UNICODE),
  86. 'created_at' => $now,
  87. 'updated_at' => $now,
  88. ]);
  89. $migratedId = DB::table('order_items')->insertGetId([
  90. 'order_id' => $orderId,
  91. 'product_id' => $product->id,
  92. 'sku' => 'REPAIR-WIG-004',
  93. 'name' => 'Wig',
  94. 'qty_ordered' => 1,
  95. 'additional' => json_encode([
  96. 'attributes' => [
  97. ['attribute_name' => 'Hair Length', 'option_label' => '12"', 'attribute_type' => 'drop_down'],
  98. ['attribute_name' => 'Free Gift', 'option_label' => 'Yes', 'attribute_type' => 'drop_down'],
  99. ['attribute_name' => 'Hair Color', 'option_label' => 'Natural Black', 'attribute_type' => 'drop_down'],
  100. ],
  101. ], JSON_UNESCAPED_UNICODE),
  102. 'created_at' => $now,
  103. 'updated_at' => $now,
  104. ]);
  105. $invoiceId = DB::table('invoice_items')->insertGetId([
  106. 'order_item_id' => $nativeId,
  107. 'sku' => 'REPAIR-WIG-004-1-1',
  108. 'name' => 'Wig',
  109. 'qty' => 1,
  110. 'created_at' => $now,
  111. 'updated_at' => $now,
  112. ]);
  113. $shipmentId = DB::table('shipment_items')->insertGetId([
  114. 'shipment_id' => 1,
  115. 'order_item_id' => $migratedId,
  116. 'sku' => 'REPAIR-WIG-004',
  117. 'name' => 'Wig',
  118. 'qty' => 1,
  119. 'created_at' => $now,
  120. 'updated_at' => $now,
  121. ]);
  122. $this->artisan('products:repair-variant-skus', [
  123. '--sku' => 'REPAIR-WIG-004',
  124. '--dry-run' => true,
  125. ])->assertSuccessful();
  126. $this->assertSame('REPAIR-WIG-004-1-1', DB::table('order_items')->where('id', $nativeId)->value('sku'));
  127. $this->artisan('products:repair-variant-skus', [
  128. '--sku' => 'REPAIR-WIG-004',
  129. ])->assertSuccessful();
  130. $this->assertSame($newSku, $variant->fresh()->sku);
  131. $this->assertSame($newSku, DB::table('order_items')->where('id', $nativeId)->value('sku'));
  132. $this->assertSame($newSku, DB::table('order_items')->where('id', $migratedId)->value('sku'));
  133. $this->assertSame($newSku, DB::table('invoice_items')->where('id', $invoiceId)->value('sku'));
  134. $this->assertSame($newSku, DB::table('shipment_items')->where('id', $shipmentId)->value('sku'));
  135. $nativeAdditional = json_decode((string) DB::table('order_items')->where('id', $nativeId)->value('additional'), true);
  136. $this->assertSame($newSku, $nativeAdditional['variant_sku']);
  137. $migratedAdditional = json_decode((string) DB::table('order_items')->where('id', $migratedId)->value('additional'), true);
  138. $this->assertArrayNotHasKey('variant_sku', $migratedAdditional);
  139. }
  140. /**
  141. * @return array{0: Product, 1: ProductVariant}
  142. */
  143. private function seedVariant(string $parentSku, string $variantSku, ?int $asteriaId): array
  144. {
  145. $suffix = str_replace('-', '_', strtolower($parentSku));
  146. $product = new Product;
  147. $product->sku = $parentSku;
  148. $product->type = 'flexible_variant';
  149. $product->migrated_from_asteria_id = $asteriaId;
  150. $product->save();
  151. $length = ProductOption::query()->create([
  152. 'label' => 'Hair Length',
  153. 'type' => 'select',
  154. 'code' => $suffix.'_length',
  155. ]);
  156. $color = ProductOption::query()->create([
  157. 'label' => 'Hair Color',
  158. 'type' => 'select',
  159. 'code' => $suffix.'_color',
  160. ]);
  161. $twelve = ProductOptionValue::query()->create([
  162. 'product_option_id' => $length->id,
  163. 'label' => '12"',
  164. 'code' => '12IN',
  165. 'position' => 1,
  166. ]);
  167. $black = ProductOptionValue::query()->create([
  168. 'product_option_id' => $color->id,
  169. 'label' => 'Natural Black',
  170. 'code' => '1B',
  171. 'position' => 1,
  172. ]);
  173. $now = now();
  174. DB::table('product_product_options')->insert([
  175. [
  176. 'product_id' => $product->id,
  177. 'product_option_id' => $length->id,
  178. 'position' => 0,
  179. 'is_required' => 1,
  180. 'created_at' => $now,
  181. 'updated_at' => $now,
  182. ],
  183. [
  184. 'product_id' => $product->id,
  185. 'product_option_id' => $color->id,
  186. 'position' => 1,
  187. 'is_required' => 1,
  188. 'created_at' => $now,
  189. 'updated_at' => $now,
  190. ],
  191. ]);
  192. $variant = ProductVariant::query()->create([
  193. 'product_id' => $product->id,
  194. 'sku' => $variantSku,
  195. 'price' => 99,
  196. 'quantity' => 1,
  197. 'status' => 1,
  198. 'sort_order' => 0,
  199. ]);
  200. DB::table('product_variant_option_values')->insert([
  201. [
  202. 'product_variant_id' => $variant->id,
  203. 'product_option_value_id' => $twelve->id,
  204. 'created_at' => $now,
  205. 'updated_at' => $now,
  206. ],
  207. [
  208. 'product_variant_id' => $variant->id,
  209. 'product_option_value_id' => $black->id,
  210. 'created_at' => $now,
  211. 'updated_at' => $now,
  212. ],
  213. ]);
  214. return [$product, $variant];
  215. }
  216. }