Magento1OrderReaderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $items = $reader->fetchItems([10]);
  92. $this->assertCount(1, $items);
  93. $this->assertSame(101, $items->first()['item_id']);
  94. $this->assertSame('WIG-001', $items->first()['sku']);
  95. $this->assertNull($items->first()['parent_item_id']);
  96. $addresses = $reader->fetchAddresses([10]);
  97. $this->assertCount(2, $addresses);
  98. $this->assertSame("123 Main St\nApt 4", $addresses->first()['street']);
  99. $payments = $reader->fetchPayments([10]);
  100. $this->assertCount(1, $payments);
  101. $this->assertSame('paypal_express', $payments->first()['method']);
  102. $this->assertSame('ABC123', $payments->first()['last_trans_id']);
  103. }
  104. public function test_it_pages_from_the_last_entity_id(): void
  105. {
  106. MagentoSchema::seedOrder($this->connection, [
  107. 'entity_id' => 11,
  108. 'increment_id' => '100000011',
  109. 'customer_email'=> 'second@example.com',
  110. ]);
  111. $reader = new Magento1OrderReader($this->connection);
  112. $page = $reader->fetchOrders(10, 50);
  113. $this->assertCount(1, $page);
  114. $this->assertSame('100000011', $page->first()['increment_id']);
  115. }
  116. }