diff --git a/app/User.php b/app/User.php index 1409f07..4134be2 100644 --- a/app/User.php +++ b/app/User.php @@ -113,7 +113,7 @@ class User extends Authenticatable public function addWife(User $wife) { - if ($this->gender_id == 1) { + if ($this->gender_id == 1 && ! $this->hasBeenMarriedTo($wife)) { $this->wifes()->save($wife); return $wife; } @@ -128,7 +128,7 @@ class User extends Authenticatable public function addHusband(User $husband) { - if ($this->gender_id == 2) { + if ($this->gender_id == 2 && ! $this->hasBeenMarriedTo($husband)) { $this->husbands()->save($husband); return $husband; } @@ -136,6 +136,11 @@ class User extends Authenticatable return false; } + public function hasBeenMarriedTo(User $user) + { + return $this->marriages->contains($user); + } + public function marriages() { if ($this->gender_id == 1) diff --git a/database/migrations/2017_06_27_151536_create_couples_table.php b/database/migrations/2017_06_27_151536_create_couples_table.php index 76e1341..9668189 100644 --- a/database/migrations/2017_06_27_151536_create_couples_table.php +++ b/database/migrations/2017_06_27_151536_create_couples_table.php @@ -20,6 +20,8 @@ class CreateCouplesTable extends Migration $table->date('marriege_date')->nullable(); $table->date('divorce_date')->nullable(); $table->timestamps(); + + $table->unique(['husband_id', 'wife_id']); }); } diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index a68af5f..22ca06f 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/tests/Feature/ManageUserFamiliesTest.php @@ -89,7 +89,7 @@ class ManageUserFamiliesTest extends TestCase $wife = factory(User::class)->states('female')->create(); $husband->addWife($wife); - $marriageId = $husband->wifes->first()->pivot->id; + $marriageId = $husband->fresh()->wifes->first()->pivot->id; $this->actingAs($husband); $this->visit(route('profile')); @@ -265,7 +265,7 @@ class ManageUserFamiliesTest extends TestCase $wife = factory(User::class)->states('female')->create(); $husband->addWife($wife); - $marriageId = $husband->wifes->first()->pivot->id; + $marriageId = $husband->fresh()->wifes->first()->pivot->id; $this->visit(route('profile')); $this->click('Set Orang Tua'); diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index b2b19d7..defd3e7 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -24,12 +24,24 @@ class UserTest extends TestCase $wife = factory(User::class)->states('female')->create(); $husband->addWife($wife); + $husband = $husband->fresh(); $this->assertCount(1, $husband->wifes); $this->assertCount(1, $wife->husbands); $this->assertCount(1, $husband->marriages); } /** @test */ + public function user_can_ony_marry_same_person_once() + { + $husband = factory(User::class)->states('male')->create(); + $wife = factory(User::class)->states('female')->create(); + + $husband->addWife($wife); + + $this->assertFalse($wife->addHusband($husband), 'This couple is married!'); + } + + /** @test */ public function user_have_father_link_method() { $father = factory(User::class)->create();