Magento1CustomerReaderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use App\Services\Asteria\Magento1CustomerReader;
  4. use Illuminate\Support\Facades\DB;
  5. use Tests\TestCase;
  6. class Magento1CustomerReaderTest 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_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::seedCustomer($this->connection, [
  24. 'entity_id' => 10,
  25. 'email' => 'jane@example.com',
  26. 'firstname' => 'Jane',
  27. 'lastname' => 'Doe',
  28. 'telephone' => '555111',
  29. 'gender' => 1,
  30. 'dob' => '1990-05-01 00:00:00',
  31. 'password_hash' => md5('abcsecret12').':abc',
  32. 'is_active' => 1,
  33. 'group_id' => 99,
  34. ]);
  35. MagentoSchema::seedAddress($this->connection, [
  36. 'entity_id' => 21,
  37. 'parent_id' => 10,
  38. 'firstname' => 'Jane',
  39. 'lastname' => 'Doe',
  40. 'street' => "123 Main St\nApt 4",
  41. 'city' => 'Austin',
  42. 'region' => 'TX',
  43. 'postcode' => '78701',
  44. 'country_id' => 'US',
  45. 'telephone' => '555111',
  46. 'company' => 'Acme',
  47. ]);
  48. }
  49. protected function tearDown(): void
  50. {
  51. DB::purge($this->connection);
  52. if (is_file($this->sqlitePath)) {
  53. @unlink($this->sqlitePath);
  54. }
  55. parent::tearDown();
  56. }
  57. public function test_it_flattens_customer_and_address_eav_attributes(): void
  58. {
  59. $reader = new Magento1CustomerReader($this->connection);
  60. $customers = $reader->fetchCustomers(0, 50);
  61. $this->assertCount(1, $customers);
  62. $customer = $customers->first();
  63. $this->assertSame(10, $customer['entity_id']);
  64. $this->assertSame('jane@example.com', $customer['email']);
  65. $this->assertSame('Jane', $customer['firstname']);
  66. $this->assertSame('Doe', $customer['lastname']);
  67. $this->assertSame('555111', $customer['telephone']);
  68. $this->assertSame('1', (string) $customer['gender']);
  69. $this->assertSame('1990-05-01 00:00:00', $customer['dob']);
  70. $this->assertSame(md5('abcsecret12').':abc', $customer['password_hash']);
  71. $addresses = $reader->fetchAddresses([10]);
  72. $this->assertCount(1, $addresses);
  73. $address = $addresses->first();
  74. $this->assertSame(21, $address['entity_id']);
  75. $this->assertSame(10, $address['parent_id']);
  76. $this->assertSame("123 Main St\nApt 4", $address['street']);
  77. $this->assertSame('Austin', $address['city']);
  78. $this->assertSame('US', $address['country_id']);
  79. $this->assertSame('Acme', $address['company']);
  80. }
  81. public function test_it_pages_from_the_last_entity_id(): void
  82. {
  83. MagentoSchema::seedCustomer($this->connection, [
  84. 'entity_id' => 11,
  85. 'email' => 'second@example.com',
  86. 'firstname' => 'Second',
  87. 'lastname' => 'User',
  88. ]);
  89. $reader = new Magento1CustomerReader($this->connection);
  90. $this->assertCount(1, $reader->fetchCustomers(10, 50));
  91. $this->assertSame('second@example.com', $reader->fetchCustomers(10, 50)->first()['email']);
  92. }
  93. }