-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the
default_billing_address_id
& `default_shipping_address_id…
…` fields to the customer table
- Loading branch information
1 parent
b7ac09a
commit 6c72deb
Showing
5 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...atabase/migrations/2023_10_18_173544_add_default_address_fields_to_the_customer_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::table('customers', function (Blueprint $table) { | ||
$table->unsignedBigInteger('default_billing_address_id')->nullable(); | ||
$table->unsignedBigInteger('default_shipping_address_id')->nullable(); | ||
|
||
$table->foreign('default_billing_address_id')->references('id')->on('addresses')->nullOnDelete(); | ||
$table->foreign('default_shipping_address_id')->references('id')->on('addresses')->nullOnDelete(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('customers', function (Blueprint $table) { | ||
$table->dropForeign('customers_default_billing_address_id_foreign'); | ||
$table->dropForeign('customers_default_shipping_address_id_foreign'); | ||
|
||
$table->dropColumn('default_billing_address_id'); | ||
$table->dropColumn('default_shipping_address_id'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Contains the AddressDefaultsTest class. | ||
* | ||
* @copyright Copyright (c) 2023 Vanilo UG | ||
* @author Attila Fulop | ||
* @license MIT | ||
* @since 2023-10-18 | ||
* | ||
*/ | ||
|
||
namespace Konekt\Customer\Tests; | ||
|
||
use Illuminate\Support\Collection; | ||
use Konekt\Address\Models\Address; | ||
use Konekt\Customer\Models\Customer; | ||
|
||
class AddressDefaultsTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function customer_has_no_default_addresses_by_default() | ||
{ | ||
$customer = Customer::create([]); | ||
|
||
$this->assertFalse($customer->hasDefaultBillingAddress()); | ||
$this->assertFalse($customer->hasDefaultShippingAddress()); | ||
|
||
$this->assertNull($customer->defaultBillingAddress()); | ||
$this->assertNull($customer->defaultShippingAddress()); | ||
|
||
$this->assertNull($customer->default_billing_address_id); | ||
$this->assertNull($customer->default_shipping_address_id); | ||
|
||
$this->assertNull($customer->default_billing_address); | ||
$this->assertNull($customer->default_shipping_address); | ||
} | ||
|
||
/** @test */ | ||
public function default_shipping_address_can_be_set() | ||
{ | ||
$customer = Customer::create([])->fresh(); | ||
$address = Address::create(['name' => 'Xyz', 'country_id' => 'UK', 'address' => 'asdqwe']); | ||
$customer->setDefaultShippingAddress($address); | ||
|
||
$this->assertTrue($customer->hasDefaultShippingAddress()); | ||
$this->assertInstanceOf(Address::class, $customer->defaultShippingAddress()); | ||
$this->assertEquals($address->id, $customer->default_shipping_address_id); | ||
$this->assertInstanceOf(Address::class, $customer->default_shipping_address); | ||
} | ||
|
||
/** @test */ | ||
public function default_billing_address_can_be_set() | ||
{ | ||
$customer = Customer::create([])->fresh(); | ||
$address = Address::create(['name' => 'Xyz', 'country_id' => 'UK', 'address' => 'asdqwe']); | ||
$customer->setDefaultBillingAddress($address); | ||
|
||
$this->assertTrue($customer->hasDefaultBillingAddress()); | ||
$this->assertInstanceOf(Address::class, $customer->defaultBillingAddress()); | ||
$this->assertEquals($address->id, $customer->default_billing_address_id); | ||
$this->assertInstanceOf(Address::class, $customer->default_billing_address); | ||
} | ||
} |