Browse Source

Merge branch 'person-marriages-order'

pull/21/head
Nafies Luthfi 7 years ago
parent
commit
b1380e9dfe
  1. 12
      app/User.php
  2. 42
      tests/Unit/UserTest.php

12
app/User.php

@ -128,7 +128,7 @@ class User extends Authenticatable
public function wifes()
{
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps();
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date');
}
public function addWife(User $wife, $marriageDate = null)
@ -146,7 +146,7 @@ class User extends Authenticatable
public function husbands()
{
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps();
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date');
}
public function addHusband(User $husband, $marriageDate = null)
@ -170,19 +170,19 @@ class User extends Authenticatable
public function couples()
{
if ($this->gender_id == 1) {
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps();
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date');
}
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps();
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date');
}
public function marriages()
{
if ($this->gender_id == 1) {
return $this->hasMany(Couple::class, 'husband_id');
return $this->hasMany(Couple::class, 'husband_id')->orderBy('marriage_date');
}
return $this->hasMany(Couple::class, 'wife_id');
return $this->hasMany(Couple::class, 'wife_id')->orderBy('marriage_date');
}
public function siblings()

42
tests/Unit/UserTest.php

@ -47,6 +47,48 @@ class UserTest extends TestCase
}
/** @test */
public function male_person_marriages_ordered_by_marriage_date()
{
$husband = factory(User::class)->states('male')->create();
$wife1 = factory(User::class)->states('female')->create();
$wife2 = factory(User::class)->states('female')->create();
$husband->addWife($wife2, '1999-04-21');
$husband->addWife($wife1, '1990-02-13');
$husband = $husband->fresh();
$marriages = $husband->marriages;
$this->assertEquals('1990-02-13', $marriages->first()->marriage_date);
$this->assertEquals('1999-04-21', $marriages->last()->marriage_date);
$this->assertEquals($wife1->name, $husband->couples->first()->name);
$this->assertEquals($wife2->name, $husband->couples->last()->name);
$this->assertEquals($wife1->name, $husband->wifes->first()->name);
$this->assertEquals($wife2->name, $husband->wifes->last()->name);
}
/** @test */
public function female_person_marriages_ordered_by_marriage_date()
{
$wife = factory(User::class)->states('female')->create();
$husband1 = factory(User::class)->states('male')->create();
$husband2 = factory(User::class)->states('male')->create();
$wife->addHusband($husband2, '1989-04-21');
$wife->addHusband($husband1, '1980-02-13');
$wife = $wife->fresh();
$marriages = $wife->marriages;
$this->assertEquals('1980-02-13', $marriages->first()->marriage_date);
$this->assertEquals('1989-04-21', $marriages->last()->marriage_date);
$this->assertEquals($husband1->name, $wife->couples->first()->name);
$this->assertEquals($husband2->name, $wife->couples->last()->name);
$this->assertEquals($husband1->name, $wife->husbands->first()->name);
$this->assertEquals($husband2->name, $wife->husbands->last()->name);
}
/** @test */
public function user_can_ony_marry_same_person_once()
{
$husband = factory(User::class)->states('male')->create();

Loading…
Cancel
Save