From 9ebf4f7519a1dee9a0c1081ed6be1328d749e6a3 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 3 Oct 2018 22:09:42 +0800 Subject: [PATCH] Add work_duration on project model --- app/Entities/Projects/Project.php | 5 +++++ app/Entities/Projects/ProjectPresenter.php | 15 ++++++++++----- tests/Unit/Models/ProjectTest.php | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/Entities/Projects/Project.php b/app/Entities/Projects/Project.php index 38f86f3..d01cbfd 100755 --- a/app/Entities/Projects/Project.php +++ b/app/Entities/Projects/Project.php @@ -224,4 +224,9 @@ class Project extends Model ->with('worker', 'tasks') ->get(); } + + public function getWorkDurationAttribute() + { + return $this->present()->workDuration; + } } diff --git a/app/Entities/Projects/ProjectPresenter.php b/app/Entities/Projects/ProjectPresenter.php index f48ca05..e0253e9 100644 --- a/app/Entities/Projects/ProjectPresenter.php +++ b/app/Entities/Projects/ProjectPresenter.php @@ -24,15 +24,20 @@ class ProjectPresenter extends Presenter public function workDuration() { - if (is_null($this->entity->end_date)) { + $startDate = $this->entity->start_date; + $endDate = $this->entity->end_date; + + if (is_null($endDate)) { return '-'; } - $workDuration = dateDifference($this->entity->start_date, $this->entity->end_date); - if ((int) $workDuration > 30) { - return dateDifference($this->entity->start_date, $this->entity->end_date, '%m Bulan %d Hari'); + $workDuration = dateDifference($startDate, $endDate); + if ((int) $workDuration > 365) { + return dateDifference($startDate, $endDate, '%y Year(s) %m Month(s) %d Day(s)'); + } elseif ((int) $workDuration > 30) { + return dateDifference($startDate, $endDate, '%m Month(s) %d Day(s)'); } - return $workDuration.' Hari'; + return $workDuration.' Day(s)'; } } diff --git a/tests/Unit/Models/ProjectTest.php b/tests/Unit/Models/ProjectTest.php index 5811409..b1af1cd 100644 --- a/tests/Unit/Models/ProjectTest.php +++ b/tests/Unit/Models/ProjectTest.php @@ -209,4 +209,26 @@ class ProjectTest extends TestCase $this->assertInstanceOf(Collection::class, $project->comments); $this->assertInstanceOf(Comment::class, $project->comments->first()); } + + /** @test */ + public function project_has_work_duration_attribute() + { + $project = factory(Project::class)->create([ + 'start_date' => '2016-06-10', + 'end_date' => '2016-07-21', + ]); + + $this->assertEquals('1 Month(s) 11 Day(s)', $project->work_duration); + } + + /** @test */ + public function project_work_duration_attribute_returns_proper_multi_years_work_duration() + { + $project = factory(Project::class)->create([ + 'start_date' => '2015-04-10', + 'end_date' => '2017-07-21', + ]); + + $this->assertEquals('2 Year(s) 3 Month(s) 11 Day(s)', $project->work_duration); + } }