Browse Source

Use timespan for age attribute

pull/27/head
Nafies Luthfi 7 years ago
parent
commit
79173ee269
  1. 18
      app/User.php
  2. 18
      tests/Unit/UserTest.php

18
app/User.php

@ -236,10 +236,20 @@ class User extends Authenticatable
{
$age = null;
if ($this->dob) {
$age = Carbon::parse($this->dob)->diffInYears($this->dod);
} elseif (!$this->dob && $this->yob) {
$age = date('Y') - $this->yob;
if ($this->dob && !$this->dod) {
$age = Carbon::parse($this->dob)->timespan();
}
if (!$this->dob && $this->yob) {
$age = (date('Y') - $this->yob).' tahun';
}
if ($this->dob && $this->dod) {
$age = Carbon::parse($this->dob)->timespan($this->dod);
}
if (!$this->dob && $this->yob && !$this->dod && $this->yod) {
$age = ($this->yod - $this->yob).' tahun';
}
if ($this->dob && $this->yob && $this->dod && $this->yod) {
$age = Carbon::parse($this->dob)->timespan($this->dod);
}
return $age;

18
tests/Unit/UserTest.php

@ -151,9 +151,9 @@ class UserTest extends TestCase
* @test
* @dataProvider userAgeDataProvider
*/
public function user_has_age_attribute($now, $dob, $yob, $dod, $yod, $age)
public function user_has_age_attribute($today, $dob, $yob, $dod, $yod, $age)
{
Carbon::setTestNow($now);
Carbon::setTestNow($today);
$user = factory(User::class)->make([
'dob' => $dob, 'yob' => $yob, 'dod' => $dod, 'yod' => $yod,
]);
@ -163,12 +163,20 @@ class UserTest extends TestCase
Carbon::setTestNow();
}
/**
* Provide data for calculating user age.
* Returning array of today, dob, yob, dod, yod, and age.
*
* @return array
*/
public function userAgeDataProvider()
{
// returning array of now, dob, yob, dod, yod, age.
return [
['2018-02-02 01:00:00', '1997-01-01', '1997', null, null, 21],
['2018-02-02 01:00:00', null, '1997', null, null, 22],
['2018-02-02', '1997-01-01', '1997', null, null, '21 tahun, 1 bulan, 1 hari'],
['2018-02-02', '1997-01-01', null, null, null, '21 tahun, 1 bulan, 1 hari'],
['2018-02-02', null, '1997', null, null, '22 tahun'],
['2018-02-02', '1997-01-01', '1997', '2017-01-01', '2017', '20 tahun'],
['2018-02-02', null, '1997', null, '2017', '20 tahun'],
];
}
}
Loading…
Cancel
Save