Magento1CustomerReaderTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use App\Services\Asteria\Magento1CustomerReader;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. use Tests\TestCase;
  7. class Magento1CustomerReaderTest extends TestCase
  8. {
  9. private string $sqlitePath;
  10. private string $connection = 'asteria_test';
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. $this->sqlitePath = sys_get_temp_dir().'/asteria_reader_'.uniqid('', true).'.sqlite';
  15. touch($this->sqlitePath);
  16. config()->set('database.connections.'.$this->connection, [
  17. 'driver' => 'sqlite',
  18. 'database' => $this->sqlitePath,
  19. 'prefix' => '',
  20. 'foreign_key_constraints' => false,
  21. ]);
  22. DB::purge($this->connection);
  23. MagentoSchema::create($this->connection);
  24. MagentoSchema::seedCustomer($this->connection, [
  25. 'entity_id' => 10,
  26. 'email' => 'jane@example.com',
  27. 'firstname' => 'Jane',
  28. 'lastname' => 'Doe',
  29. 'telephone' => '555111',
  30. 'gender' => 1,
  31. 'dob' => '1990-05-01 00:00:00',
  32. 'password_hash' => md5('abcsecret12').':abc',
  33. 'is_active' => 1,
  34. 'group_id' => 99,
  35. 'source' => 20,
  36. ]);
  37. MagentoSchema::seedAddress($this->connection, [
  38. 'entity_id' => 21,
  39. 'parent_id' => 10,
  40. 'firstname' => 'Jane',
  41. 'lastname' => 'Doe',
  42. 'street' => "123 Main St\nApt 4",
  43. 'city' => 'Austin',
  44. 'region' => 'TX',
  45. 'postcode' => '78701',
  46. 'country_id' => 'US',
  47. 'telephone' => '555111',
  48. 'company' => 'Acme',
  49. ]);
  50. }
  51. protected function tearDown(): void
  52. {
  53. DB::purge($this->connection);
  54. if (is_file($this->sqlitePath)) {
  55. @unlink($this->sqlitePath);
  56. }
  57. parent::tearDown();
  58. }
  59. public function test_it_flattens_customer_and_address_eav_attributes(): void
  60. {
  61. $reader = new Magento1CustomerReader($this->connection);
  62. $customers = $reader->fetchCustomers(0, 50);
  63. $this->assertCount(1, $customers);
  64. $customer = $customers->first();
  65. $this->assertSame(10, $customer['entity_id']);
  66. $this->assertSame('jane@example.com', $customer['email']);
  67. $this->assertSame('Jane', $customer['firstname']);
  68. $this->assertSame('Doe', $customer['lastname']);
  69. $this->assertSame('555111', $customer['telephone']);
  70. $this->assertSame('1', (string) $customer['gender']);
  71. $this->assertSame('1990-05-01 00:00:00', $customer['dob']);
  72. $this->assertSame(md5('abcsecret12').':abc', $customer['password_hash']);
  73. $this->assertSame('20', (string) $customer['source']);
  74. $addresses = $reader->fetchAddresses([10]);
  75. $this->assertCount(1, $addresses);
  76. $address = $addresses->first();
  77. $this->assertSame(21, $address['entity_id']);
  78. $this->assertSame(10, $address['parent_id']);
  79. $this->assertSame("123 Main St\nApt 4", $address['street']);
  80. $this->assertSame('Austin', $address['city']);
  81. $this->assertSame('US', $address['country_id']);
  82. $this->assertSame('Acme', $address['company']);
  83. }
  84. public function test_it_pages_from_the_last_entity_id(): void
  85. {
  86. MagentoSchema::seedCustomer($this->connection, [
  87. 'entity_id' => 11,
  88. 'email' => 'second@example.com',
  89. 'firstname' => 'Second',
  90. 'lastname' => 'User',
  91. ]);
  92. $reader = new Magento1CustomerReader($this->connection);
  93. $this->assertCount(1, $reader->fetchCustomers(10, 50));
  94. $this->assertSame('second@example.com', $reader->fetchCustomers(10, 50)->first()['email']);
  95. }
  96. public function test_it_reads_newsletter_subscribers_by_customer_id_and_email(): void
  97. {
  98. MagentoSchema::seedSubscriber($this->connection, [
  99. 'subscriber_id' => 1,
  100. 'customer_id' => 10,
  101. 'subscriber_email' => 'Jane@example.com',
  102. 'subscriber_status' => 1,
  103. ]);
  104. MagentoSchema::seedSubscriber($this->connection, [
  105. 'subscriber_id' => 2,
  106. 'customer_id' => 0,
  107. 'subscriber_email' => 'guest@example.com',
  108. 'subscriber_status' => 1,
  109. ]);
  110. $reader = new Magento1CustomerReader($this->connection);
  111. $forCustomer = $reader->fetchSubscribers([10], ['jane@example.com']);
  112. $this->assertCount(1, $forCustomer);
  113. $this->assertSame(1, $forCustomer->first()['subscriber_id']);
  114. $this->assertSame(1, $forCustomer->first()['subscriber_status']);
  115. $paged = $reader->fetchSubscribersAfter(0, 50);
  116. $this->assertCount(2, $paged);
  117. $this->assertSame(2, $reader->fetchSubscribersAfter(1, 50)->first()['subscriber_id']);
  118. }
  119. public function test_it_returns_no_subscribers_when_the_table_is_missing(): void
  120. {
  121. Schema::connection($this->connection)->drop('newsletter_subscriber');
  122. $reader = new Magento1CustomerReader($this->connection);
  123. $this->assertTrue($reader->fetchSubscribers([10], ['jane@example.com'])->isEmpty());
  124. $this->assertTrue($reader->fetchSubscribersAfter(0, 50)->isEmpty());
  125. }
  126. }