Browse Source

Added prevention to marriage same person twice

pull/3/head
Nafies Luthfi 8 years ago
parent
commit
34ce296dfa
  1. 9
      app/User.php
  2. 2
      database/migrations/2017_06_27_151536_create_couples_table.php
  3. 4
      tests/Feature/ManageUserFamiliesTest.php
  4. 12
      tests/Unit/UserTest.php

9
app/User.php

@ -113,7 +113,7 @@ class User extends Authenticatable
public function addWife(User $wife) public function addWife(User $wife)
{ {
if ($this->gender_id == 1) {
if ($this->gender_id == 1 && ! $this->hasBeenMarriedTo($wife)) {
$this->wifes()->save($wife); $this->wifes()->save($wife);
return $wife; return $wife;
} }
@ -128,7 +128,7 @@ class User extends Authenticatable
public function addHusband(User $husband) public function addHusband(User $husband)
{ {
if ($this->gender_id == 2) {
if ($this->gender_id == 2 && ! $this->hasBeenMarriedTo($husband)) {
$this->husbands()->save($husband); $this->husbands()->save($husband);
return $husband; return $husband;
} }
@ -136,6 +136,11 @@ class User extends Authenticatable
return false; return false;
} }
public function hasBeenMarriedTo(User $user)
{
return $this->marriages->contains($user);
}
public function marriages() public function marriages()
{ {
if ($this->gender_id == 1) if ($this->gender_id == 1)

2
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('marriege_date')->nullable();
$table->date('divorce_date')->nullable(); $table->date('divorce_date')->nullable();
$table->timestamps(); $table->timestamps();
$table->unique(['husband_id', 'wife_id']);
}); });
} }

4
tests/Feature/ManageUserFamiliesTest.php

@ -89,7 +89,7 @@ class ManageUserFamiliesTest extends TestCase
$wife = factory(User::class)->states('female')->create(); $wife = factory(User::class)->states('female')->create();
$husband->addWife($wife); $husband->addWife($wife);
$marriageId = $husband->wifes->first()->pivot->id;
$marriageId = $husband->fresh()->wifes->first()->pivot->id;
$this->actingAs($husband); $this->actingAs($husband);
$this->visit(route('profile')); $this->visit(route('profile'));
@ -265,7 +265,7 @@ class ManageUserFamiliesTest extends TestCase
$wife = factory(User::class)->states('female')->create(); $wife = factory(User::class)->states('female')->create();
$husband->addWife($wife); $husband->addWife($wife);
$marriageId = $husband->wifes->first()->pivot->id;
$marriageId = $husband->fresh()->wifes->first()->pivot->id;
$this->visit(route('profile')); $this->visit(route('profile'));
$this->click('Set Orang Tua'); $this->click('Set Orang Tua');

12
tests/Unit/UserTest.php

@ -24,12 +24,24 @@ class UserTest extends TestCase
$wife = factory(User::class)->states('female')->create(); $wife = factory(User::class)->states('female')->create();
$husband->addWife($wife); $husband->addWife($wife);
$husband = $husband->fresh();
$this->assertCount(1, $husband->wifes); $this->assertCount(1, $husband->wifes);
$this->assertCount(1, $wife->husbands); $this->assertCount(1, $wife->husbands);
$this->assertCount(1, $husband->marriages); $this->assertCount(1, $husband->marriages);
} }
/** @test */ /** @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() public function user_have_father_link_method()
{ {
$father = factory(User::class)->create(); $father = factory(User::class)->create();

Loading…
Cancel
Save