MigrateAsteriaCustomersCommandTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Hash;
  6. use Illuminate\Support\Facades\Schema;
  7. use Webkul\BagistoApi\Tests\BagistoApiTestCase;
  8. use Webkul\Customer\Models\Customer;
  9. use Webkul\Customer\Models\CustomerAddress;
  10. use Webkul\Customer\Models\CustomerGroup;
  11. class MigrateAsteriaCustomersCommandTest extends BagistoApiTestCase
  12. {
  13. private string $sqlitePath;
  14. private string $connection = 'asteria_test';
  15. public function setUp(): void
  16. {
  17. parent::setUp();
  18. if (! Schema::hasColumn('customers', 'migrated_from_asteria_id')
  19. || ! Schema::hasColumn('customers', 'legacy_password')) {
  20. $this->markTestSkipped('Run php artisan migrate to add Asteria customer columns.');
  21. }
  22. $this->seedRequiredData();
  23. Cache::forget('migrate_asteria_customers_last_id');
  24. $this->sqlitePath = sys_get_temp_dir().'/asteria_cmd_'.uniqid('', true).'.sqlite';
  25. touch($this->sqlitePath);
  26. config()->set('database.connections.'.$this->connection, [
  27. 'driver' => 'sqlite',
  28. 'database' => $this->sqlitePath,
  29. 'prefix' => '',
  30. 'foreign_key_constraints' => false,
  31. ]);
  32. DB::purge($this->connection);
  33. MagentoSchema::create($this->connection);
  34. }
  35. public function tearDown(): void
  36. {
  37. DB::purge($this->connection);
  38. if (isset($this->sqlitePath) && is_file($this->sqlitePath)) {
  39. @unlink($this->sqlitePath);
  40. }
  41. parent::tearDown();
  42. }
  43. public function test_it_migrates_a_customer_and_address_into_the_general_group(): void
  44. {
  45. MagentoSchema::seedCustomer($this->connection, [
  46. 'entity_id' => 10,
  47. 'email' => 'jane@example.com',
  48. 'firstname' => 'Jane',
  49. 'lastname' => 'Doe',
  50. 'telephone' => '5551112222',
  51. 'gender' => 1,
  52. 'dob' => '1990-05-01 00:00:00',
  53. 'password_hash' => md5('abcsecret12').':abc',
  54. 'group_id' => 99,
  55. 'default_billing' => 21,
  56. 'default_shipping' => 21,
  57. ]);
  58. MagentoSchema::seedAddress($this->connection, [
  59. 'entity_id' => 21,
  60. 'parent_id' => 10,
  61. 'firstname' => 'Jane',
  62. 'lastname' => 'Doe',
  63. 'street' => "123 Main St\nApt 4",
  64. 'city' => 'Austin',
  65. 'region' => 'TX',
  66. 'postcode' => '78701',
  67. 'country_id' => 'US',
  68. 'telephone' => '5551112222',
  69. 'company' => 'Acme',
  70. ]);
  71. $this->artisan('customers:migrate-asteria', [
  72. '--connection' => $this->connection,
  73. '--reset-progress' => true,
  74. ])->assertSuccessful();
  75. $customer = Customer::query()->where('email', 'jane@example.com')->first();
  76. $this->assertNotNull($customer);
  77. $this->assertSame(10, (int) $customer->migrated_from_asteria_id);
  78. $this->assertSame('Jane', $customer->first_name);
  79. $this->assertSame('Doe', $customer->last_name);
  80. $this->assertSame('Male', $customer->gender);
  81. $this->assertSame('1990-05-01', $customer->date_of_birth);
  82. $this->assertSame('5551112222', $customer->phone);
  83. $this->assertSame(md5('abcsecret12').':abc', $customer->legacy_password);
  84. $this->assertSame(1, (int) $customer->is_verified);
  85. $generalId = CustomerGroup::query()->where('code', 'general')->value('id');
  86. $this->assertSame((int) $generalId, (int) $customer->customer_group_id);
  87. $address = CustomerAddress::query()->where('customer_id', $customer->id)->first();
  88. $this->assertNotNull($address);
  89. $this->assertSame('123 Main St, Apt 4', $address->address);
  90. $this->assertSame('Austin', $address->city);
  91. $this->assertSame('US', $address->country);
  92. $this->assertTrue((bool) $address->default_address);
  93. $this->assertTrue((bool) $address->use_for_shipping);
  94. $this->assertSame(21, $this->asteriaAddressId($address));
  95. }
  96. public function test_dry_run_does_not_write_customers(): void
  97. {
  98. MagentoSchema::seedCustomer($this->connection, [
  99. 'entity_id' => 10,
  100. 'email' => 'dry-run@example.com',
  101. 'firstname' => 'Dry',
  102. 'lastname' => 'Run',
  103. ]);
  104. $before = Customer::query()->count();
  105. $this->artisan('customers:migrate-asteria', [
  106. '--connection' => $this->connection,
  107. '--dry-run' => true,
  108. '--reset-progress' => true,
  109. ])->assertSuccessful();
  110. $this->assertSame($before, Customer::query()->count());
  111. $this->assertNull(Customer::query()->where('email', 'dry-run@example.com')->first());
  112. }
  113. public function test_it_links_an_existing_email_without_overwriting_the_password(): void
  114. {
  115. $existing = $this->createCustomer([
  116. 'email' => 'existing@example.com',
  117. 'password' => Hash::make('bagisto-secret'),
  118. 'first_name'=> 'Keep',
  119. ]);
  120. MagentoSchema::seedCustomer($this->connection, [
  121. 'entity_id' => 44,
  122. 'email' => 'existing@example.com',
  123. 'firstname' => 'Magento',
  124. 'lastname' => 'Name',
  125. 'password_hash' => md5('abcsecret12').':abc',
  126. ]);
  127. MagentoSchema::seedAddress($this->connection, [
  128. 'entity_id' => 45,
  129. 'parent_id' => 44,
  130. 'firstname' => 'Magento',
  131. 'lastname' => 'Name',
  132. 'street' => '9 Oak Rd',
  133. 'city' => 'Dallas',
  134. 'country_id' => 'US',
  135. ]);
  136. $this->artisan('customers:migrate-asteria', [
  137. '--connection' => $this->connection,
  138. '--reset-progress' => true,
  139. ])->assertSuccessful();
  140. $existing->refresh();
  141. $this->assertSame(44, (int) $existing->migrated_from_asteria_id);
  142. $this->assertSame('Keep', $existing->first_name);
  143. $this->assertTrue(Hash::check('bagisto-secret', $existing->password));
  144. $this->assertNull($existing->legacy_password);
  145. $this->assertSame(1, Customer::query()->where('email', 'existing@example.com')->count());
  146. $this->assertSame(1, CustomerAddress::query()->where('customer_id', $existing->id)->count());
  147. }
  148. public function test_it_nulls_a_conflicting_phone_number(): void
  149. {
  150. $this->createCustomer([
  151. 'email' => 'owner@example.com',
  152. 'phone' => '5550001111',
  153. ]);
  154. MagentoSchema::seedCustomer($this->connection, [
  155. 'entity_id' => 70,
  156. 'email' => 'other@example.com',
  157. 'firstname' => 'Other',
  158. 'lastname' => 'Person',
  159. 'telephone' => '5550001111',
  160. ]);
  161. $this->artisan('customers:migrate-asteria', [
  162. '--connection' => $this->connection,
  163. '--reset-progress' => true,
  164. ])->assertSuccessful();
  165. $migrated = Customer::query()->where('email', 'other@example.com')->first();
  166. $this->assertNotNull($migrated);
  167. $this->assertNull($migrated->phone);
  168. }
  169. public function test_it_is_idempotent_on_a_second_run(): void
  170. {
  171. MagentoSchema::seedCustomer($this->connection, [
  172. 'entity_id' => 80,
  173. 'email' => 'once@example.com',
  174. 'firstname' => 'Once',
  175. 'lastname' => 'Only',
  176. ]);
  177. MagentoSchema::seedAddress($this->connection, [
  178. 'entity_id' => 81,
  179. 'parent_id' => 80,
  180. 'firstname' => 'Once',
  181. 'lastname' => 'Only',
  182. 'street' => '1 Repeat Ln',
  183. 'city' => 'Miami',
  184. 'country_id' => 'US',
  185. ]);
  186. $options = [
  187. '--connection' => $this->connection,
  188. '--reset-progress' => true,
  189. ];
  190. $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
  191. $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
  192. $this->assertSame(1, Customer::query()->where('email', 'once@example.com')->count());
  193. $customer = Customer::query()->where('email', 'once@example.com')->first();
  194. $this->assertSame(1, CustomerAddress::query()->where('customer_id', $customer->id)->count());
  195. }
  196. private function asteriaAddressId(CustomerAddress $address): ?int
  197. {
  198. $additional = $address->additional;
  199. if (is_string($additional) && $additional !== '') {
  200. $additional = json_decode($additional, true);
  201. }
  202. return is_array($additional) ? (int) ($additional['asteria_address_id'] ?? 0) : null;
  203. }
  204. }