Browse Source

Change age attribute to age_detail

pull/27/head
Nafies Luthfi 7 years ago
parent
commit
e2daa6abb8
  1. 16
      app/User.php
  2. 10
      tests/Unit/UserTest.php

16
app/User.php

@ -232,27 +232,27 @@ class User extends Authenticatable
return $this->hasMany(Couple::class, 'manager_id');
}
public function getAgeAttribute()
public function getAgeDetailAttribute()
{
$age = null;
$ageDetail = null;
$yearOnlySuffix = Carbon::now()->format('-m-d');
if ($this->dob && !$this->dod) {
$age = Carbon::parse($this->dob)->timespan();
$ageDetail = Carbon::parse($this->dob)->timespan();
}
if (!$this->dob && $this->yob) {
$age = Carbon::parse($this->yob.$yearOnlySuffix)->timespan();
$ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->timespan();
}
if ($this->dob && $this->dod) {
$age = Carbon::parse($this->dob)->timespan($this->dod);
$ageDetail = Carbon::parse($this->dob)->timespan($this->dod);
}
if (!$this->dob && $this->yob && !$this->dod && $this->yod) {
$age = Carbon::parse($this->yob.$yearOnlySuffix)->timespan($this->yod.$yearOnlySuffix);
$ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->timespan($this->yod.$yearOnlySuffix);
}
if ($this->dob && $this->yob && $this->dod && $this->yod) {
$age = Carbon::parse($this->dob)->timespan($this->dod);
$ageDetail = Carbon::parse($this->dob)->timespan($this->dod);
}
return $age;
return $ageDetail;
}
}

10
tests/Unit/UserTest.php

@ -149,27 +149,27 @@ class UserTest extends TestCase
/**
* @test
* @dataProvider userAgeDataProvider
* @dataProvider userAgeDetailDataProvider
*/
public function user_has_age_attribute($today, $dob, $yob, $dod, $yod, $age)
public function user_has_age_detail_attribute($today, $dob, $yob, $dod, $yod, $age)
{
Carbon::setTestNow($today);
$user = factory(User::class)->make([
'dob' => $dob, 'yob' => $yob, 'dod' => $dod, 'yod' => $yod,
]);
$this->assertEquals($age, $user->age);
$this->assertEquals($age, $user->age_detail);
Carbon::setTestNow();
}
/**
* Provide data for calculating user age.
* Provide data for calculating user age detail.
* Returning array of today, dob, yob, dod, yod, and age.
*
* @return array
*/
public function userAgeDataProvider()
public function userAgeDetailDataProvider()
{
return [
['2018-02-02', '1997-01-01', '1997', null, null, '21 tahun, 1 bulan, 1 hari'],

Loading…
Cancel
Save