mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-16 20:22:15 +01:00
Add spend_time to projects and tasks
This commit is contained in:
committed by
Gregor Vostrak
parent
2e8da98287
commit
bff766d363
58
tests/Unit/Jobs/RecalculateSpentTimeForProjectTest.php
Normal file
58
tests/Unit/Jobs/RecalculateSpentTimeForProjectTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use App\Jobs\RecalculateSpentTimeForProject;
|
||||
use App\Models\Project;
|
||||
use App\Models\TimeEntry;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCaseWithDatabase;
|
||||
|
||||
class RecalculateSpentTimeForProjectTest extends TestCaseWithDatabase
|
||||
{
|
||||
public function test_recalculates_spent_time_for_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$project = Project::factory()->create([
|
||||
'spent_time' => 0,
|
||||
]);
|
||||
TimeEntry::factory()->startWithDuration(now(), 10)->forProject($project)->create();
|
||||
TimeEntry::factory()->startWithDuration(now(), 11)->forProject($project)->create();
|
||||
|
||||
$project->refresh();
|
||||
$recalculateSpentTimeForProject = new RecalculateSpentTimeForProject($project);
|
||||
DB::enableQueryLog();
|
||||
|
||||
// Act
|
||||
$recalculateSpentTimeForProject->handle();
|
||||
|
||||
// Assert
|
||||
self::assertCount(2, DB::getQueryLog());
|
||||
$project->refresh();
|
||||
self::assertEquals(21, $project->spent_time);
|
||||
}
|
||||
|
||||
public function test_does_not_save_project_if_value_is_already_correct(): void
|
||||
{
|
||||
// Arrange
|
||||
$project = Project::factory()->create([
|
||||
'spent_time' => 21,
|
||||
]);
|
||||
TimeEntry::factory()->startWithDuration(now(), 10)->forProject($project)->create();
|
||||
TimeEntry::factory()->startWithDuration(now(), 11)->forProject($project)->create();
|
||||
|
||||
$project->refresh();
|
||||
$recalculateSpentTimeForProject = new RecalculateSpentTimeForProject($project);
|
||||
DB::enableQueryLog();
|
||||
|
||||
// Act
|
||||
$recalculateSpentTimeForProject->handle();
|
||||
|
||||
// Assert
|
||||
self::assertCount(1, DB::getQueryLog());
|
||||
$project->refresh();
|
||||
self::assertEquals(21, $project->spent_time);
|
||||
}
|
||||
}
|
||||
58
tests/Unit/Jobs/RecalculateSpentTimeForTaskTest.php
Normal file
58
tests/Unit/Jobs/RecalculateSpentTimeForTaskTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use App\Jobs\RecalculateSpentTimeForTask;
|
||||
use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCaseWithDatabase;
|
||||
|
||||
class RecalculateSpentTimeForTaskTest extends TestCaseWithDatabase
|
||||
{
|
||||
public function test_recalculates_spent_time_for_task(): void
|
||||
{
|
||||
// Arrange
|
||||
$task = Task::factory()->create([
|
||||
'spent_time' => 0,
|
||||
]);
|
||||
TimeEntry::factory()->startWithDuration(now(), 10)->forTask($task)->create();
|
||||
TimeEntry::factory()->startWithDuration(now(), 11)->forTask($task)->create();
|
||||
|
||||
$task->refresh();
|
||||
$recalculateSpentTimeForTask = new RecalculateSpentTimeForTask($task);
|
||||
DB::enableQueryLog();
|
||||
|
||||
// Act
|
||||
$recalculateSpentTimeForTask->handle();
|
||||
|
||||
// Assert
|
||||
self::assertCount(2, DB::getQueryLog());
|
||||
$task->refresh();
|
||||
self::assertEquals(21, $task->spent_time);
|
||||
}
|
||||
|
||||
public function test_does_not_save_task_if_value_is_already_correct(): void
|
||||
{
|
||||
// Arrange
|
||||
$task = Task::factory()->create([
|
||||
'spent_time' => 21,
|
||||
]);
|
||||
TimeEntry::factory()->startWithDuration(now(), 10)->forTask($task)->create();
|
||||
TimeEntry::factory()->startWithDuration(now(), 11)->forTask($task)->create();
|
||||
|
||||
$task->refresh();
|
||||
$recalculateSpentTimeForTask = new RecalculateSpentTimeForTask($task);
|
||||
DB::enableQueryLog();
|
||||
|
||||
// Act
|
||||
$recalculateSpentTimeForTask->handle();
|
||||
|
||||
// Assert
|
||||
self::assertCount(1, DB::getQueryLog());
|
||||
$task->refresh();
|
||||
self::assertEquals(21, $task->spent_time);
|
||||
}
|
||||
}
|
||||
51
tests/Unit/Jobs/Test/TestJobTest.php
Normal file
51
tests/Unit/Jobs/Test/TestJobTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Jobs\Test;
|
||||
|
||||
use App\Jobs\Test\TestJob;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Tests\TestCaseWithDatabase;
|
||||
use TiMacDonald\Log\LogEntry;
|
||||
|
||||
class TestJobTest extends TestCaseWithDatabase
|
||||
{
|
||||
public function test_logs_debug_message(): void
|
||||
{
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
$message = 'Test message';
|
||||
$job = new TestJob($user, $message);
|
||||
|
||||
// Act
|
||||
$job->handle();
|
||||
|
||||
// Assert
|
||||
Log::assertLoggedTimes(fn (LogEntry $log) => $log->level === 'debug'
|
||||
&& $log->message === 'TestJob: '.$message
|
||||
&& $log->context['user'] === $user->getKey(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
public function test_can_fail_if_parameter_fail_is_true(): void
|
||||
{
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
$message = 'Test message';
|
||||
$job = new TestJob($user, $message, true);
|
||||
|
||||
// Act
|
||||
try {
|
||||
$job->handle();
|
||||
} catch (\Exception $e) {
|
||||
// Assert
|
||||
$this->assertEquals('TestJob failed.', $e->getMessage());
|
||||
|
||||
return;
|
||||
}
|
||||
$this->fail('Expected exception not thrown');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user