Magento1OrderReaderTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use App\Services\Asteria\Magento1OrderReader;
  4. use Illuminate\Support\Facades\DB;
  5. use Tests\TestCase;
  6. class Magento1OrderReaderTest extends TestCase
  7. {
  8. private string $sqlitePath;
  9. private string $connection = 'asteria_test';
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->sqlitePath = sys_get_temp_dir().'/asteria_order_reader_'.uniqid('', true).'.sqlite';
  14. touch($this->sqlitePath);
  15. config()->set('database.connections.'.$this->connection, [
  16. 'driver' => 'sqlite',
  17. 'database' => $this->sqlitePath,
  18. 'prefix' => '',
  19. 'foreign_key_constraints' => false,
  20. ]);
  21. DB::purge($this->connection);
  22. MagentoSchema::create($this->connection);
  23. MagentoSchema::seedOrder($this->connection, [
  24. 'entity_id' => 10,
  25. 'increment_id' => '100000010',
  26. 'customer_id' => 5,
  27. 'customer_email' => 'jane@example.com',
  28. 'customer_firstname' => 'Jane',
  29. 'customer_lastname' => 'Doe',
  30. 'status' => 'complete',
  31. 'state' => 'complete',
  32. 'grand_total' => 110,
  33. 'subtotal' => 100,
  34. 'shipping_amount' => 10,
  35. 'items' => [
  36. [
  37. 'item_id' => 101,
  38. 'sku' => 'WIG-001',
  39. 'name' => 'Lace Wig',
  40. 'qty_ordered'=> 1,
  41. 'price' => 100,
  42. 'row_total' => 100,
  43. ],
  44. ],
  45. 'addresses' => [
  46. [
  47. 'entity_id' => 201,
  48. 'address_type' => 'billing',
  49. 'firstname' => 'Jane',
  50. 'lastname' => 'Doe',
  51. 'street' => "123 Main St\nApt 4",
  52. 'city' => 'Austin',
  53. 'country_id' => 'US',
  54. ],
  55. [
  56. 'entity_id' => 202,
  57. 'address_type' => 'shipping',
  58. 'firstname' => 'Jane',
  59. 'lastname' => 'Doe',
  60. 'street' => '9 Oak Rd',
  61. 'city' => 'Dallas',
  62. 'country_id' => 'US',
  63. ],
  64. ],
  65. 'payment' => [
  66. 'entity_id' => 301,
  67. 'method' => 'paypal_express',
  68. 'last_trans_id' => 'ABC123',
  69. ],
  70. ]);
  71. }
  72. protected function tearDown(): void
  73. {
  74. DB::purge($this->connection);
  75. if (is_file($this->sqlitePath)) {
  76. @unlink($this->sqlitePath);
  77. }
  78. parent::tearDown();
  79. }
  80. public function test_it_reads_orders_items_addresses_and_payments(): void
  81. {
  82. $reader = new Magento1OrderReader($this->connection);
  83. $orders = $reader->fetchOrders(0, 50);
  84. $this->assertCount(1, $orders);
  85. $order = $orders->first();
  86. $this->assertSame(10, $order['entity_id']);
  87. $this->assertSame('100000010', $order['increment_id']);
  88. $this->assertSame('jane@example.com', $order['customer_email']);
  89. $this->assertSame('complete', $order['status']);
  90. $this->assertEquals(110, $order['grand_total']);
  91. $this->assertArrayHasKey('remote_ip', $order);
  92. $this->assertArrayHasKey('giftcard_amount', $order);
  93. $this->assertArrayHasKey('member_amount', $order);
  94. $this->assertArrayHasKey('member_free_amount', $order);
  95. $items = $reader->fetchItems([10]);
  96. $this->assertCount(1, $items);
  97. $this->assertSame(101, $items->first()['item_id']);
  98. $this->assertSame('WIG-001', $items->first()['sku']);
  99. $this->assertNull($items->first()['parent_item_id']);
  100. $this->assertArrayHasKey('product_options', $items->first());
  101. $addresses = $reader->fetchAddresses([10]);
  102. $this->assertCount(2, $addresses);
  103. $this->assertSame("123 Main St\nApt 4", $addresses->first()['street']);
  104. $payments = $reader->fetchPayments([10]);
  105. $this->assertCount(1, $payments);
  106. $this->assertSame('paypal_express', $payments->first()['method']);
  107. $this->assertSame('ABC123', $payments->first()['last_trans_id']);
  108. $this->assertArrayHasKey('additional_information', $payments->first());
  109. }
  110. public function test_it_pages_from_the_last_entity_id(): void
  111. {
  112. MagentoSchema::seedOrder($this->connection, [
  113. 'entity_id' => 11,
  114. 'increment_id' => '100000011',
  115. 'customer_email'=> 'second@example.com',
  116. ]);
  117. $reader = new Magento1OrderReader($this->connection);
  118. $page = $reader->fetchOrders(10, 50);
  119. $this->assertCount(1, $page);
  120. $this->assertSame('100000011', $page->first()['increment_id']);
  121. }
  122. public function test_it_reads_shipments_items_and_tracks(): void
  123. {
  124. MagentoSchema::seedShipment($this->connection, [
  125. 'entity_id' => 501,
  126. 'order_id' => 10,
  127. 'increment_id' => '100000501',
  128. 'total_qty' => 1,
  129. 'items' => [
  130. [
  131. 'entity_id' => 601,
  132. 'order_item_id' => 101,
  133. 'sku' => 'WIG-001',
  134. 'qty' => 1,
  135. ],
  136. ],
  137. 'tracks' => [
  138. [
  139. 'entity_id' => 701,
  140. 'track_number' => 'JD014',
  141. 'title' => 'DHL',
  142. 'carrier_code' => 'dhlint',
  143. ],
  144. ],
  145. ]);
  146. $reader = new Magento1OrderReader($this->connection);
  147. $shipments = $reader->fetchShipments([10]);
  148. $this->assertCount(1, $shipments);
  149. $this->assertSame(501, $shipments->first()['entity_id']);
  150. $this->assertSame(10, $shipments->first()['order_id']);
  151. $items = $reader->fetchShipmentItems([501]);
  152. $this->assertCount(1, $items);
  153. $this->assertSame(101, $items->first()['order_item_id']);
  154. $this->assertSame('WIG-001', $items->first()['sku']);
  155. $tracks = $reader->fetchShipmentTracks([501]);
  156. $this->assertCount(1, $tracks);
  157. $this->assertSame('JD014', $tracks->first()['track_number']);
  158. $this->assertSame('dhlint', $tracks->first()['carrier_code']);
  159. }
  160. }