From 771c98162ee1b65f3c51cc798bdf3c84d85c97cb Mon Sep 17 00:00:00 2001 From: Mohamed Kamel Date: Fri, 20 Oct 2023 18:11:53 +0300 Subject: [PATCH] Test get through relations with customized local keys --- tests/BelongsToThroughTest.php | 12 +++++++++++ tests/Models/CustomerAddress.php | 18 +++++++++++++++++ tests/Models/VendorCustomer.php | 8 ++++++++ tests/Models/VendorCustomerAddress.php | 8 ++++++++ tests/TestCase.php | 28 ++++++++++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 tests/Models/CustomerAddress.php create mode 100644 tests/Models/VendorCustomer.php create mode 100644 tests/Models/VendorCustomerAddress.php diff --git a/tests/BelongsToThroughTest.php b/tests/BelongsToThroughTest.php index 709080f..bccf29d 100644 --- a/tests/BelongsToThroughTest.php +++ b/tests/BelongsToThroughTest.php @@ -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 { @@ -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()); + } } diff --git a/tests/Models/CustomerAddress.php b/tests/Models/CustomerAddress.php new file mode 100644 index 0000000..2002d7f --- /dev/null +++ b/tests/Models/CustomerAddress.php @@ -0,0 +1,18 @@ +belongsToThrough( + VendorCustomer::class, + VendorCustomerAddress::class, + foreignKeyLookup: [VendorCustomerAddress::class => 'id'], + localKeyLookup: [VendorCustomerAddress::class => 'customer_address_id'], + ); + } +} diff --git a/tests/Models/VendorCustomer.php b/tests/Models/VendorCustomer.php new file mode 100644 index 0000000..cb00a11 --- /dev/null +++ b/tests/Models/VendorCustomer.php @@ -0,0 +1,8 @@ +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'); + }); } /** @@ -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(); } }