From f58d49066f0c8734611135a3f33f6c2a86777dd1 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 21 Dec 2020 17:31:51 +0800 Subject: [PATCH] Resolve error exception if a couple husband or wife was deleted --- app/Couple.php | 4 ++-- tests/Unit/CoupleTest.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/Couple.php b/app/Couple.php index e20b942..0a2f40a 100644 --- a/app/Couple.php +++ b/app/Couple.php @@ -23,12 +23,12 @@ class Couple extends Model public function husband() { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withDefault(['name' => 'N/A']); } public function wife() { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withDefault(['name' => 'N/A']); } public function childs() diff --git a/tests/Unit/CoupleTest.php b/tests/Unit/CoupleTest.php index d8d1967..577ad32 100644 --- a/tests/Unit/CoupleTest.php +++ b/tests/Unit/CoupleTest.php @@ -20,6 +20,16 @@ class CoupleTest extends TestCase } /** @test */ + public function a_couples_husband_or_wife_has_a_default_name() + { + $couple = factory(Couple::class)->create(); + $couple->husband->delete(); + $couple->wife->delete(); + $this->assertEquals($couple->fresh()->husband->name, 'N/A'); + $this->assertEquals($couple->fresh()->wife->name, 'N/A'); + } + + /** @test */ public function a_couple_can_have_many_childs() { $couple = factory(Couple::class)->create();