mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 08:12:17 +01:00
Add spend_time to projects and tasks
This commit is contained in:
committed by
Gregor Vostrak
parent
2e8da98287
commit
bff766d363
@@ -10,6 +10,8 @@ use App\Models\Organization;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectMember;
|
||||
use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
|
||||
@@ -117,6 +119,47 @@ class ProjectModelTest extends ModelTestAbstract
|
||||
], $allProjects);
|
||||
}
|
||||
|
||||
public function test_computed_spent_time_returns_the_sum_of_all_time_entries_excl_running_timers(): void
|
||||
{
|
||||
// Arrange
|
||||
$project = Project::factory()->create();
|
||||
$otherProject = Project::factory()->create();
|
||||
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($otherProject)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($otherProject)->start(now())->active()->create();
|
||||
|
||||
// Act
|
||||
$project->refresh();
|
||||
$spentTime = $project->getSpentTimeComputed();
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(30, $spentTime);
|
||||
}
|
||||
|
||||
public function test_computed_spent_time_returns_already_computed_value_if_present(): void
|
||||
{
|
||||
// Arrange
|
||||
$project = Project::factory()->create();
|
||||
$otherProject = Project::factory()->create();
|
||||
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($project)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($otherProject)->startWithDuration(now(), 10)->create();
|
||||
TimeEntry::factory()->forProject($otherProject)->start(now())->active()->create();
|
||||
$timeEntries = Project::query()
|
||||
->withAggregate('timeEntries as spent_time_computed', DB::raw('extract(epoch from ("end" - start))'), 'sum')
|
||||
->get();
|
||||
|
||||
// Act
|
||||
$project->refresh();
|
||||
$spentTime = $timeEntries->first()->getSpentTimeComputed();
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(30, $spentTime);
|
||||
}
|
||||
|
||||
public function test_accessor_is_archived_is_true_if_archived_at_is_not_null(): void
|
||||
{
|
||||
// Arrange
|
||||
|
||||
Reference in New Issue
Block a user