Skip to content

Commit

Permalink
Test get through relations with customized local keys
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammedkamel committed Oct 20, 2023
1 parent de4cbe2 commit 771c981
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Tests\Models\Comment;
use Tests\Models\Country;
use Tests\Models\CustomerAddress;
use Tests\Models\Post;
use Tests\Models\User;
use Tests\Models\VendorCustomer;

class BelongsToThroughTest extends TestCase
{
Expand Down Expand Up @@ -134,4 +136,14 @@ public function testGetThroughParents()
$this->assertInstanceOf(User::class, $throughParents[0]);
$this->assertInstanceOf(Post::class, $throughParents[1]);
}

public function testGetThroughWithCustomizedLocalKeys()
{
$addresses = CustomerAddress::with('vendorCustomer')->get();

$this->assertEquals(41, $addresses[0]->vendorCustomer->id);
$this->assertEquals(42, $addresses[1]->vendorCustomer->id);
$this->assertInstanceOf(VendorCustomer::class, $addresses[1]->vendorCustomer);
$this->assertFalse($addresses[2]->vendorCustomer()->exists());
}
}
18 changes: 18 additions & 0 deletions tests/Models/CustomerAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Models;

use Znck\Eloquent\Relations\BelongsToThrough;

class CustomerAddress extends Model
{
public function vendorCustomer(): BelongsToThrough
{
return $this->belongsToThrough(
VendorCustomer::class,
VendorCustomerAddress::class,
foreignKeyLookup: [VendorCustomerAddress::class => 'id'],
localKeyLookup: [VendorCustomerAddress::class => 'customer_address_id'],
);
}
}
8 changes: 8 additions & 0 deletions tests/Models/VendorCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tests\Models;

class VendorCustomer extends Model
{
//
}
8 changes: 8 additions & 0 deletions tests/Models/VendorCustomerAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tests\Models;

class VendorCustomerAddress extends Model
{
//
}
28 changes: 28 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
use PHPUnit\Framework\TestCase as Base;
use Tests\Models\Comment;
use Tests\Models\Country;
use Tests\Models\CustomerAddress;
use Tests\Models\Post;
use Tests\Models\User;
use Tests\Models\VendorCustomer;
use Tests\Models\VendorCustomerAddress;

abstract class TestCase extends Base
{
Expand Down Expand Up @@ -61,6 +64,20 @@ protected function migrate()
$table->unsignedInteger('custom_post_id')->nullable();
$table->unsignedInteger('parent_id')->nullable();
});

DB::schema()->create('vendor_customers', function (Blueprint $table) {
$table->increments('id');
});

DB::schema()->create('customer_addresses', function (Blueprint $table) {
$table->increments('id');
});

DB::schema()->create('vendor_customer_addresses', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('vendor_customer_id');
$table->unsignedInteger('customer_address_id');
});
}

/**
Expand Down Expand Up @@ -91,6 +108,17 @@ protected function seed()
Comment::create(['id' => 34, 'post_id' => null, 'custom_post_id' => 21, 'parent_id' => 33]);
Comment::create(['id' => 35, 'post_id' => null, 'custom_post_id' => 24, 'parent_id' => 34]);

VendorCustomer::create(['id' => 41]);
VendorCustomer::create(['id' => 42]);
VendorCustomer::create(['id' => 43]);

CustomerAddress::create(['id' => 51]);
CustomerAddress::create(['id' => 52]);
CustomerAddress::create(['id' => 53]);

VendorCustomerAddress::create(['id' => 61, 'vendor_customer_id' => 41, 'customer_address_id' => 51]);
VendorCustomerAddress::create(['id' => 62, 'vendor_customer_id' => 42, 'customer_address_id' => 52]);

Model::reguard();
}
}

0 comments on commit 771c981

Please sign in to comment.