From 3c34261b7b522674a71456c66ffe9651d502e150 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 4 Dec 2018 20:40:48 +0800 Subject: [PATCH 1/3] Order person marriages by marriage_date --- app/User.php | 4 ++-- tests/Unit/UserTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/User.php b/app/User.php index cfdf3e2..17a63c9 100644 --- a/app/User.php +++ b/app/User.php @@ -179,10 +179,10 @@ class User extends Authenticatable 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() diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index f9848ec..0908055 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -47,6 +47,34 @@ 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($wife1, '1990-02-13'); + $husband->addWife($wife2, '1999-04-21'); + + $marriages = $husband->fresh()->marriages; + $this->assertEquals('1990-02-13', $marriages->first()->marriage_date); + $this->assertEquals('1999-04-21', $marriages->last()->marriage_date); + } + + /** @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($husband1, '1980-02-13'); + $wife->addHusband($husband2, '1989-04-21'); + + $marriages = $wife->fresh()->marriages; + $this->assertEquals('1980-02-13', $marriages->first()->marriage_date); + $this->assertEquals('1989-04-21', $marriages->last()->marriage_date); + } + + /** @test */ public function user_can_ony_marry_same_person_once() { $husband = factory(User::class)->states('male')->create(); From fbdad8a371c70a15a1180eaabeb42baab6d12b6f Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 4 Dec 2018 20:47:30 +0800 Subject: [PATCH 2/3] Order user couples by marriage_date Order female husbands by marriage_date Order male wifes by marriage_date --- app/User.php | 8 ++++---- tests/Unit/UserTest.php | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/User.php b/app/User.php index 17a63c9..c172f5e 100644 --- a/app/User.php +++ b/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,10 +170,10 @@ 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() diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 0908055..3966602 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -55,9 +55,16 @@ class UserTest extends TestCase $husband->addWife($wife1, '1990-02-13'); $husband->addWife($wife2, '1999-04-21'); - $marriages = $husband->fresh()->marriages; + $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 */ @@ -69,9 +76,16 @@ class UserTest extends TestCase $wife->addHusband($husband1, '1980-02-13'); $wife->addHusband($husband2, '1989-04-21'); - $marriages = $wife->fresh()->marriages; + $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 */ From acb609e95e64117f554082fb42265b0a8a203e19 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 4 Dec 2018 20:50:50 +0800 Subject: [PATCH 3/3] Swap addHusband and addWife action Just to make sure couple addition does not interfere the order of person marriage --- tests/Unit/UserTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 3966602..96c839d 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -52,8 +52,8 @@ class UserTest extends TestCase $husband = factory(User::class)->states('male')->create(); $wife1 = factory(User::class)->states('female')->create(); $wife2 = factory(User::class)->states('female')->create(); - $husband->addWife($wife1, '1990-02-13'); $husband->addWife($wife2, '1999-04-21'); + $husband->addWife($wife1, '1990-02-13'); $husband = $husband->fresh(); $marriages = $husband->marriages; @@ -73,8 +73,8 @@ class UserTest extends TestCase $wife = factory(User::class)->states('female')->create(); $husband1 = factory(User::class)->states('male')->create(); $husband2 = factory(User::class)->states('male')->create(); - $wife->addHusband($husband1, '1980-02-13'); $wife->addHusband($husband2, '1989-04-21'); + $wife->addHusband($husband1, '1980-02-13'); $wife = $wife->fresh(); $marriages = $wife->marriages;