Skip to content

Commit

Permalink
Added the model polymorphic relationship to the Address model
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 15, 2023
1 parent c310688 commit 8b476a6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
- Changed minimum Enum version to v4.1
- BC: Changed all enum interfaces to extend the root `EnumInterface`
- BC: added the `iso2Code()` and the `getName()` methods to the `Country` interface
- BC: added the `model()` method to the `Address` interface
- Added firstname, lastname, company_name, email, phone, address2, access_code, tax_nr and registration_nr fields to the Address table/model
- Added a generic, optional and polymorphic `model` relationship to the Address model

## 2.8.0
##### 2023-04-08
Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
namespace Konekt\Address\Contracts;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

interface Address
{
public function country(): BelongsTo;

public function province(): BelongsTo;

public function model(): MorphTo;
}
12 changes: 7 additions & 5 deletions src/Models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Konekt\Address\Contracts\Address as AddressContract;
use Konekt\Enum\Eloquent\CastsEnums;

Expand All @@ -39,6 +40,7 @@
* @property string|null $tax_nr
* @property string|null $registration_nr
* @property string|null $access_code
* @property Model|null $model
*
* @property-read Country $country
* @property-read null|Province $province
Expand All @@ -54,13 +56,13 @@ class Address extends Model implements AddressContract
'type' => AddressType::class
];

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'addresses';

public function model(): MorphTo
{
return $this->morphTo();
}

public function country(): BelongsTo
{
return $this->belongsTo(CountryProxy::modelClass(), 'country_id');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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('addresses', function (Blueprint $table) {
$table->nullableMorphs('model');
});
}

public function down(): void
{
Schema::table('addresses', function (Blueprint $table) {
$table->dropMorphs('model');
});
}
};
32 changes: 32 additions & 0 deletions tests/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Konekt\Address\Models\AddressProxy;
use Konekt\Address\Models\AddressType;
use Konekt\Address\Models\Country;
use Konekt\Address\Models\Organization;
use Konekt\Address\Models\Person;
use Konekt\Address\Models\Province;
use Konekt\Address\Models\ProvinceType;
use Konekt\Address\Seeds\CountiesOfRomania;
Expand Down Expand Up @@ -226,6 +228,36 @@ public function it_properly_saves_all_the_fields()
$this->assertTrue(AddressType::BUSINESS()->equals($address->type));
}

/** @test */
public function it_can_be_assigned_to_an_arbitrary_model()
{
$mrTeufel = Person::create(['firstname' => 'Fritz', 'lastname' => 'Teufel']);
$teufelsAddress = Address::create([
'name' => 'Fritz Teufel',
'country_id' => 'JP',
'address' => 'Mishiazavu 123.'
]);

$teufelsAddress->model()->associate($mrTeufel);
$teufelsAddress->save();

$intermouse = Organization::create(['name' => 'Intermouse Inc.']);
$hqAddress = Address::create(['name' => 'Intermouse', 'country_id' => 'JP', 'address' => '123']);
$hqAddress->model()->associate($intermouse);
$hqAddress->save();

$teufelsAddress = $teufelsAddress->fresh('model');
$this->assertInstanceOf(Person::class, $teufelsAddress->model);
$this->assertEquals($mrTeufel->id, $teufelsAddress->model->id);
$this->assertEquals('Fritz', $teufelsAddress->model->firstname);
$this->assertEquals('Teufel', $teufelsAddress->model->lastname);

$hqAddress = $hqAddress->fresh('model');
$this->assertInstanceOf(Organization::class, $hqAddress->model);
$this->assertEquals($intermouse->id, $hqAddress->model->id);
$this->assertEquals('Intermouse Inc.', $hqAddress->model->name);
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 8b476a6

Please sign in to comment.