Browse Source

Add work_duration on project model

pull/18/head
Nafies Luthfi 7 years ago
parent
commit
9ebf4f7519
  1. 5
      app/Entities/Projects/Project.php
  2. 15
      app/Entities/Projects/ProjectPresenter.php
  3. 22
      tests/Unit/Models/ProjectTest.php

5
app/Entities/Projects/Project.php

@ -224,4 +224,9 @@ class Project extends Model
->with('worker', 'tasks') ->with('worker', 'tasks')
->get(); ->get();
} }
public function getWorkDurationAttribute()
{
return $this->present()->workDuration;
}
} }

15
app/Entities/Projects/ProjectPresenter.php

@ -24,15 +24,20 @@ class ProjectPresenter extends Presenter
public function workDuration() 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 '-'; 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)';
} }
} }

22
tests/Unit/Models/ProjectTest.php

@ -209,4 +209,26 @@ class ProjectTest extends TestCase
$this->assertInstanceOf(Collection::class, $project->comments); $this->assertInstanceOf(Collection::class, $project->comments);
$this->assertInstanceOf(Comment::class, $project->comments->first()); $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);
}
} }
Loading…
Cancel
Save