| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace Webkul\BagistoApi\Tests\Unit\Migration;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Schema;
- use Webkul\BagistoApi\Tests\BagistoApiTestCase;
- use Webkul\Customer\Models\Customer;
- use Webkul\Customer\Models\CustomerAddress;
- use Webkul\Customer\Models\CustomerGroup;
- class MigrateAsteriaCustomersCommandTest extends BagistoApiTestCase
- {
- private string $sqlitePath;
- private string $connection = 'asteria_test';
- public function setUp(): void
- {
- parent::setUp();
- if (! Schema::hasColumn('customers', 'migrated_from_asteria_id')
- || ! Schema::hasColumn('customers', 'legacy_password')) {
- $this->markTestSkipped('Run php artisan migrate to add Asteria customer columns.');
- }
- $this->seedRequiredData();
- Cache::forget('migrate_asteria_customers_last_id');
- $this->sqlitePath = sys_get_temp_dir().'/asteria_cmd_'.uniqid('', true).'.sqlite';
- touch($this->sqlitePath);
- config()->set('database.connections.'.$this->connection, [
- 'driver' => 'sqlite',
- 'database' => $this->sqlitePath,
- 'prefix' => '',
- 'foreign_key_constraints' => false,
- ]);
- DB::purge($this->connection);
- MagentoSchema::create($this->connection);
- }
- public function tearDown(): void
- {
- DB::purge($this->connection);
- if (isset($this->sqlitePath) && is_file($this->sqlitePath)) {
- @unlink($this->sqlitePath);
- }
- parent::tearDown();
- }
- public function test_it_migrates_a_customer_and_address_into_the_general_group(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 10,
- 'email' => 'jane@example.com',
- 'firstname' => 'Jane',
- 'lastname' => 'Doe',
- 'telephone' => '5551112222',
- 'gender' => 1,
- 'dob' => '1990-05-01 00:00:00',
- 'password_hash' => md5('abcsecret12').':abc',
- 'group_id' => 99,
- 'default_billing' => 21,
- 'default_shipping' => 21,
- ]);
- MagentoSchema::seedAddress($this->connection, [
- 'entity_id' => 21,
- 'parent_id' => 10,
- 'firstname' => 'Jane',
- 'lastname' => 'Doe',
- 'street' => "123 Main St\nApt 4",
- 'city' => 'Austin',
- 'region' => 'TX',
- 'postcode' => '78701',
- 'country_id' => 'US',
- 'telephone' => '5551112222',
- 'company' => 'Acme',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'jane@example.com')->first();
- $this->assertNotNull($customer);
- $this->assertSame(10, (int) $customer->migrated_from_asteria_id);
- $this->assertSame('Jane', $customer->first_name);
- $this->assertSame('Doe', $customer->last_name);
- $this->assertSame('Male', $customer->gender);
- $this->assertSame('1990-05-01', $customer->date_of_birth);
- $this->assertSame('5551112222', $customer->phone);
- $this->assertSame(md5('abcsecret12').':abc', $customer->legacy_password);
- $this->assertSame(1, (int) $customer->is_verified);
- $generalId = CustomerGroup::query()->where('code', 'general')->value('id');
- $this->assertSame((int) $generalId, (int) $customer->customer_group_id);
- $address = CustomerAddress::query()->where('customer_id', $customer->id)->first();
- $this->assertNotNull($address);
- $this->assertSame('123 Main St, Apt 4', $address->address);
- $this->assertSame('Austin', $address->city);
- $this->assertSame('US', $address->country);
- $this->assertTrue((bool) $address->default_address);
- $this->assertTrue((bool) $address->use_for_shipping);
- $this->assertSame(21, $this->asteriaAddressId($address));
- }
- public function test_dry_run_does_not_write_customers(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 10,
- 'email' => 'dry-run@example.com',
- 'firstname' => 'Dry',
- 'lastname' => 'Run',
- ]);
- $before = Customer::query()->count();
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--dry-run' => true,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $this->assertSame($before, Customer::query()->count());
- $this->assertNull(Customer::query()->where('email', 'dry-run@example.com')->first());
- }
- public function test_it_links_an_existing_email_without_overwriting_the_password(): void
- {
- $existing = $this->createCustomer([
- 'email' => 'existing@example.com',
- 'password' => Hash::make('bagisto-secret'),
- 'first_name'=> 'Keep',
- ]);
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 44,
- 'email' => 'existing@example.com',
- 'firstname' => 'Magento',
- 'lastname' => 'Name',
- 'password_hash' => md5('abcsecret12').':abc',
- ]);
- MagentoSchema::seedAddress($this->connection, [
- 'entity_id' => 45,
- 'parent_id' => 44,
- 'firstname' => 'Magento',
- 'lastname' => 'Name',
- 'street' => '9 Oak Rd',
- 'city' => 'Dallas',
- 'country_id' => 'US',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $existing->refresh();
- $this->assertSame(44, (int) $existing->migrated_from_asteria_id);
- $this->assertSame('Keep', $existing->first_name);
- $this->assertTrue(Hash::check('bagisto-secret', $existing->password));
- $this->assertNull($existing->legacy_password);
- $this->assertSame(1, Customer::query()->where('email', 'existing@example.com')->count());
- $this->assertSame(1, CustomerAddress::query()->where('customer_id', $existing->id)->count());
- }
- public function test_it_nulls_a_conflicting_phone_number(): void
- {
- $this->createCustomer([
- 'email' => 'owner@example.com',
- 'phone' => '5550001111',
- ]);
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 70,
- 'email' => 'other@example.com',
- 'firstname' => 'Other',
- 'lastname' => 'Person',
- 'telephone' => '5550001111',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $migrated = Customer::query()->where('email', 'other@example.com')->first();
- $this->assertNotNull($migrated);
- $this->assertNull($migrated->phone);
- }
- public function test_it_is_idempotent_on_a_second_run(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 80,
- 'email' => 'once@example.com',
- 'firstname' => 'Once',
- 'lastname' => 'Only',
- ]);
- MagentoSchema::seedAddress($this->connection, [
- 'entity_id' => 81,
- 'parent_id' => 80,
- 'firstname' => 'Once',
- 'lastname' => 'Only',
- 'street' => '1 Repeat Ln',
- 'city' => 'Miami',
- 'country_id' => 'US',
- ]);
- $options = [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ];
- $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
- $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
- $this->assertSame(1, Customer::query()->where('email', 'once@example.com')->count());
- $customer = Customer::query()->where('email', 'once@example.com')->first();
- $this->assertSame(1, CustomerAddress::query()->where('customer_id', $customer->id)->count());
- }
- private function asteriaAddressId(CustomerAddress $address): ?int
- {
- $additional = $address->additional;
- if (is_string($additional) && $additional !== '') {
- $additional = json_decode($additional, true);
- }
- return is_array($additional) ? (int) ($additional['asteria_address_id'] ?? 0) : null;
- }
- }
|