Add ability to update billable rate of existing time entries

This commit is contained in:
Constantin Graf
2024-06-14 16:46:39 +02:00
committed by Gregor Vostrak
parent 7c26cee1ea
commit 2184b3c835
22 changed files with 1001 additions and 25 deletions

View File

@@ -78,6 +78,26 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
$response->assertForbidden();
}
public function test_import_fails_if_data_can_not_be_base64_decoded(): void
{
$user = $this->createUserWithPermission([
'import',
]);
Passport::actingAs($user->user);
// Act
$response = $this->postJson(route('api.v1.import.import', ['organization' => $user->organization->getKey()]), [
'type' => 'toggl_time_entries',
'data' => 'some invalid data ...',
]);
// Assert
$response->assertStatus(400);
$response->assertExactJson([
'message' => 'Invalid base64 encoded data',
]);
}
public function test_import_return_error_message_if_import_fails(): void
{
$user = $this->createUserWithPermission([

View File

@@ -12,7 +12,9 @@ use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\TimeEntry;
use App\Models\User;
use App\Service\BillableRateService;
use Laravel\Passport\Passport;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\UsesClass;
#[UsesClass(MemberController::class)]
@@ -91,6 +93,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([
'members:update',
]);
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
@@ -107,6 +110,32 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
$this->assertSame(Role::Employee->value, $member->role);
}
public function test_update_member_can_update_billable_rate_of_member_and_update_time_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'members:update',
]);
$this->mock(BillableRateService::class, function (MockInterface $mock) use ($data) {
$mock->shouldReceive('updateTimeEntriesBillableRateForMember')
->once()
->withArgs(fn (Member $memberArg) => $memberArg->is($data->member) && $memberArg->billable_rate === 10001);
});
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.members.update', [$data->organization->getKey(), $data->member]), [
'billable_rate' => 10001,
'billable_rate_update_time_entries' => 'true',
]);
// Assert
$response->assertStatus(200);
$member = $data->member;
$member->refresh();
$this->assertSame(10001, $member->billable_rate);
}
public function test_invite_placeholder_succeeds_if_data_is_valid(): void
{
$data = $this->createUserWithPermission([

View File

@@ -6,7 +6,9 @@ namespace Tests\Unit\Endpoint\Api\V1;
use App\Http\Controllers\Api\V1\OrganizationController;
use App\Models\Organization;
use App\Service\BillableRateService;
use Laravel\Passport\Passport;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\UsesClass;
#[UsesClass(OrganizationController::class)]
@@ -45,8 +47,8 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_organizations(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$data = $this->createUserWithPermission();
$this->assertBillableRateServiceIsUnused();
$organizationFake = Organization::factory()->make();
Passport::actingAs($data->user);
@@ -65,6 +67,7 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([
'organizations:update',
]);
$this->assertBillableRateServiceIsUnused();
$organizationFake = Organization::factory()->make();
Passport::actingAs($data->user);
@@ -88,6 +91,7 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([
'organizations:update',
]);
$this->assertBillableRateServiceIsUnused();
$organizationFake = Organization::factory()->make();
Passport::actingAs($data->user);
@@ -104,4 +108,34 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
'billable_rate' => $organizationFake->billable_rate,
]);
}
public function test_update_endpoint_can_update_billable_rate_of_organization_and_update_time_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'organizations:update',
]);
$billableRate = 111;
$organizationFake = Organization::factory()->billableRate($billableRate)->make();
$this->mock(BillableRateService::class, function (MockInterface $mock) use ($data, $billableRate) {
$mock->shouldReceive('updateTimeEntriesBillableRateForOrganization')
->once()
->withArgs(fn (Organization $organization) => $organization->is($data->organization) && $organization->billable_rate === $billableRate);
});
Passport::actingAs($data->user);
// Act
$response = $this->withoutExceptionHandling()->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
'billable_rate_update_time_entries' => 'true',
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Organization::class, [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
]);
}
}

View File

@@ -11,7 +11,9 @@ use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\Task;
use App\Models\TimeEntry;
use App\Service\BillableRateService;
use Laravel\Passport\Passport;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\UsesClass;
#[UsesClass(ProjectController::class)]
@@ -200,6 +202,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
'projects:create',
]);
$projectFake = Project::factory()->forOrganization($data->organization)->make();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
@@ -230,6 +233,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$otherOrganization = Organization::factory()->create();
$project = Project::factory()->forOrganization($otherOrganization)->create();
$projectFake = Project::factory()->make();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
@@ -246,10 +250,10 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_projects(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$data = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($data->organization)->create();
$projectFake = Project::factory()->make();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
@@ -272,6 +276,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$project = Project::factory()->forOrganization($data->organization)->create();
$projectFake = Project::factory()->make();
$client = Client::factory()->forOrganization($data->organization)->create();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
@@ -299,6 +304,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectFake = Project::factory()->make();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
@@ -318,6 +324,39 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_update_endpoint_can_update_projects_billable_rate_and_update_time_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'projects:update',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectFake = Project::factory()->make();
$this->mock(BillableRateService::class, function (MockInterface $mock) use ($project): void {
$mock->shouldReceive('updateTimeEntriesBillableRateForProject')
->once()
->withArgs(fn (Project $projectArg) => $projectArg->is($project) && $projectArg->billable_rate === 10003);
});
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
'name' => $projectFake->name,
'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
'billable_rate' => 10003,
'billable_rate_update_time_entries' => 'true',
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Project::class, [
'name' => $projectFake->name,
'color' => $projectFake->color,
'billable_rate' => 10003,
]);
}
public function test_destroy_endpoint_fails_if_user_is_not_part_of_project_organization(): void
{
// Arrange

View File

@@ -9,7 +9,9 @@ use App\Models\Member;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\User;
use App\Service\BillableRateService;
use Laravel\Passport\Passport;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\UsesClass;
#[UsesClass(ProjectMemberController::class)]
@@ -289,20 +291,52 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
'project-members:update',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$billableRate = 1001;
$projectMember = ProjectMember::factory()->forProject($project)->create();
$projectMemberFake = ProjectMember::factory()->make();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.project-members.update', [$data->organization->getKey(), $projectMember->getKey()]), [
'billable_rate' => $projectMemberFake->billable_rate,
'billable_rate' => $billableRate,
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(ProjectMember::class, [
'id' => $projectMember->getKey(),
'billable_rate' => $projectMemberFake->billable_rate,
'billable_rate' => $billableRate,
'member_id' => $projectMember->member_id,
]);
}
public function test_update_endpoints_can_update_billable_rate_and_update_time_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:update',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$billableRate = 1001;
$projectMember = ProjectMember::factory()->forProject($project)->create();
$this->mock(BillableRateService::class, function (MockInterface $mock) use ($projectMember, $billableRate): void {
$mock->shouldReceive('updateTimeEntriesBillableRateForProjectMember')
->once()
->withArgs(fn (ProjectMember $projectMemberArg) => $projectMemberArg->is($projectMember) && $projectMemberArg->billable_rate === $billableRate);
});
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.project-members.update', [$data->organization->getKey(), $projectMember->getKey()]), [
'billable_rate' => $billableRate,
'billable_rate_update_time_entries' => 'true',
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(ProjectMember::class, [
'id' => $projectMember->getKey(),
'billable_rate' => $billableRate,
'member_id' => $projectMember->member_id,
]);
}

View File

@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Member;
use App\Models\Organization;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\User;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
#[CoversClass(Member::class)]
#[UsesClass(Member::class)]
class MemberModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_user(): void
{
// Arrange
$user = User::factory()->create();
$member = Member::factory()->forUser($user)->create();
// Act
$member->refresh();
$userRel = $member->user;
// Assert
$this->assertNotNull($userRel);
$this->assertTrue($userRel->is($user));
}
public function test_it_belongs_to_a_organization(): void
{
// Arrange
$organization = Organization::factory()->create();
$member = Member::factory()->forOrganization($organization)->create();
// Act
$member->refresh();
$organizationRel = $member->organization;
// Assert
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
public function test_it_has_many_project_members(): void
{
// Arrange
$member = Member::factory()->create();
$project1 = Project::factory()->create();
$project2 = Project::factory()->create();
$projectMember1 = ProjectMember::factory()->forMember($member)->forProject($project1)->create();
$projectMember2 = ProjectMember::factory()->forMember($member)->forProject($project2)->createMany();
// Act
$member->refresh();
$projectMembersRel = $member->projectMembers;
// Assert
$this->assertNotNull($projectMembersRel);
$this->assertCount(2, $projectMembersRel);
$this->assertEqualsCanonicalizing([
$projectMember1->getKey(),
$projectMember2->first()->getKey(),
], $projectMembersRel->pluck('id')->all());
}
}

View File

@@ -11,14 +11,15 @@ use App\Models\ProjectMember;
use App\Models\TimeEntry;
use App\Models\User;
use App\Service\BillableRateService;
use DB;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Tests\TestCase;
use Tests\TestCaseWithDatabase;
#[CoversClass(BillableRateService::class)]
#[UsesClass(BillableRateService::class)]
class BillableRateServiceTest extends TestCase
class BillableRateServiceTest extends TestCaseWithDatabase
{
use RefreshDatabase;
@@ -30,6 +31,516 @@ class BillableRateServiceTest extends TestCase
$this->billableRateService = app(BillableRateService::class);
}
/*
* Function: updateTimeEntriesBillableRateForProjectMember
*/
public function test_update_time_entries_billable_rate_for_project_member_updates_time_entries_of_project_member(): void
{
// Arrange
$user = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($user->organization)->create();
$projectMember = ProjectMember::factory()->forMember($user->member)->forProject($project)->create([
'billable_rate' => 123,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProjectMember($projectMember);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 123,
]);
}
public function test_update_time_entries_billable_rate_for_project_member_updates_time_entries_of_project_member_even_if_all_other_billable_rates_are_set(): void
{
// Arrange
$user = $this->createUserWithPermission();
$organization = $user->organization;
$member = $user->member;
$organization->billable_rate = 111;
$organization->save();
$member->billable_rate = 222;
$member->save();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => 321,
]);
$projectMember = ProjectMember::factory()->forMember($user->member)->forProject($project)->create([
'billable_rate' => 123,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProjectMember($projectMember);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 123,
]);
}
public function test_update_time_entries_billable_rate_for_project_member_ignores_time_entries_of_other_member(): void
{
// Arrange
$user = $this->createUserWithPermission();
$otherUser = User::factory()->create();
$otherMember = Member::factory()->forUser($otherUser)->forOrganization($user->organization)->create();
$project = Project::factory()->forOrganization($user->organization)->create();
$projectMember = ProjectMember::factory()->forMember($user->member)->forProject($project)->create([
'billable_rate' => 123,
]);
$otherProjectMember = ProjectMember::factory()->forMember($otherMember)->forProject($project)->create([
'billable_rate' => 321,
]);
$timeEntry = TimeEntry::factory()->forMember($otherMember)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProjectMember($projectMember);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
/*
* Function: updateTimeEntriesBillableRateForProject
*/
public function test_update_time_entries_billable_rate_for_project_updates_time_entries_of_project(): void
{
// Arrange
$user = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => 321,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProject($project);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 321,
]);
}
public function test_update_time_entries_billable_rate_for_project_updates_time_entries_of_project_all_other_billable_rates_null(): void
{
// Arrange
$user = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => 321,
]);
$projectMember = ProjectMember::factory()->forMember($user->member)->forProject($project)->create([
'billable_rate' => null,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProject($project);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 321,
]);
}
public function test_update_time_entries_billable_rate_for_project_ignores_time_entries_that_are_not_billable(): void
{
// Arrange
$user = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => 321,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->notBillable()->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProject($project);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => null,
]);
}
public function test_update_time_entries_billable_rate_for_project_ignores_time_entries_that_have_project_member_with_billable_rate(): void
{
// Arrange
$user = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => 321,
]);
$projectMember = ProjectMember::factory()->forMember($user->member)->forProject($project)->create([
'billable_rate' => 123,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProject($project);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
public function test_update_time_entries_billable_rate_for_project_ignores_time_entries_of_that_project_but_are_incorrectly_attached_to_other_organization(): void
{
// Arrange
$user = $this->createUserWithPermission();
$userInOtherOrga = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => 321,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->billableRate(1)->create();
$brokenTimeEntryInOtherOrganizationButSameProject = TimeEntry::factory()->forMember($userInOtherOrga->member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForProject($project);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 2);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 321,
]);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $brokenTimeEntryInOtherOrganizationButSameProject->getKey(),
'billable_rate' => 1,
]);
}
/*
* Function: updateTimeEntriesBillableRateForMember
*/
public function test_update_time_entries_billable_rate_for_member_updates_time_entries_of_member(): void
{
// Arrange
$user = $this->createUserWithPermission();
$member = $user->member;
$member->billable_rate = 567;
$member->save();
$timeEntry = TimeEntry::factory()->forMember($member)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForMember($member);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 567,
]);
}
public function test_update_time_entries_billable_rate_for_member_updates_time_entries_of_member_all_other_billable_rates_null(): void
{
// Arrange
$user = $this->createUserWithPermission();
$member = $user->member;
$member->billable_rate = 110;
$member->save();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => null,
]);
$projectMember = ProjectMember::factory()->forMember($member)->forProject($project)->create([
'billable_rate' => null,
]);
$timeEntry = TimeEntry::factory()->forMember($member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForMember($member);
// Assert
$queryLog = DB::getQueryLog();
$this->assertCount(1, $queryLog);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 110,
]);
}
public function test_update_time_entries_billable_rate_for_member_ignores_time_entries_that_have_project_member_with_billable_rate(): void
{
// Arrange
$user = $this->createUserWithPermission();
$member = $user->member;
$member->billable_rate = 110;
$member->save();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => null,
]);
$projectMember = ProjectMember::factory()->forMember($member)->forProject($project)->create([
'billable_rate' => 123,
]);
$timeEntry = TimeEntry::factory()->forMember($member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForMember($member);
// Assert
$queryLog = DB::getQueryLog();
$this->assertCount(1, $queryLog);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
public function test_update_time_entries_billable_rate_for_member_ignores_time_entries_that_have_project_with_billable_rate(): void
{
// Arrange
$user = $this->createUserWithPermission();
$member = $user->member;
$member->billable_rate = 110;
$member->save();
$project = Project::factory()->forOrganization($user->organization)->create([
'billable_rate' => 123,
]);
$timeEntry = TimeEntry::factory()->forMember($member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForMember($member);
// Assert
$queryLog = DB::getQueryLog();
$this->assertCount(1, $queryLog);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
/*
* Function: updateTimeEntriesBillableRateForOrganization
*/
public function test_update_time_entries_billable_rate_for_organization_updates_time_entries_of_organization(): void
{
// Arrange
$user = $this->createUserWithPermission();
$organization = $user->organization;
$organization->billable_rate = 110;
$organization->save();
$timeEntry = TimeEntry::factory()->forMember($user->member)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForOrganization($user->organization);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 110,
]);
}
public function test_update_time_entries_billable_rate_for_organization_updates_time_entries_of_organization_all_other_billable_rates_null(): void
{
// Arrange
$user = $this->createUserWithPermission();
$member = $user->member;
$organization = $user->organization;
$organization->billable_rate = 110;
$organization->save();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => null,
]);
$projectMember = ProjectMember::factory()->forProject($project)->forMember($member)->create([
'billable_rate' => null,
]);
$timeEntry = TimeEntry::factory()->forMember($user->member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForOrganization($user->organization);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 110,
]);
}
public function test_update_time_entries_billable_rate_for_organization_ignores_time_entries_that_are_not_billable(): void
{
// Arrange
$user = $this->createUserWithPermission();
$organization = $user->organization;
$organization->billable_rate = 110;
$organization->save();
$timeEntry = TimeEntry::factory()->forMember($user->member)->notBillable()->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForOrganization($organization);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => null,
]);
}
public function test_update_time_entries_billable_rate_for_organization_ignores_time_entries_of_organization(): void
{
// Arrange
$user = $this->createUserWithPermission();
$organization = $user->organization;
$organization->billable_rate = 110;
$organization->save();
$otherUser = $this->createUserWithPermission();
$timeEntry = TimeEntry::factory()->forMember($otherUser->member)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForOrganization($organization);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
public function test_update_time_entries_billable_rate_for_organization_ignores_time_entries_with_member_with_billable_rate(): void
{
// Arrange
$user = $this->createUserWithPermission();
$member = $user->member;
$member->billable_rate = 120;
$member->save();
$organization = $user->organization;
$organization->billable_rate = 110;
$organization->save();
$timeEntry = TimeEntry::factory()->forMember($member)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForOrganization($organization);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
public function test_update_time_entries_billable_rate_for_organization_ignores_time_entries_with_project_with_billable_rate(): void
{
// Arrange
$user = $this->createUserWithPermission();
$member = $user->member;
$organization = $user->organization;
$organization->billable_rate = 110;
$organization->save();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => 120,
]);
$timeEntry = TimeEntry::factory()->forMember($member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForOrganization($organization);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
public function test_update_time_entries_billable_rate_for_organization_ignores_time_entries_with_project_member_with_billable_rate(): void
{
$user = $this->createUserWithPermission();
$member = $user->member;
$organization = $user->organization;
$organization->billable_rate = 110;
$organization->save();
$project = Project::factory()->forOrganization($organization)->create([
'billable_rate' => null,
]);
$projectMember = ProjectMember::factory()->forProject($project)->forMember($member)->create([
'billable_rate' => 120,
]);
$timeEntry = TimeEntry::factory()->forMember($member)->forProject($project)->billableRate(1)->create();
$this->enableQueryLog();
// Act
$this->billableRateService->updateTimeEntriesBillableRateForOrganization($organization);
// Assert
$this->assertQueryCount(1);
$this->assertDatabaseCount(TimeEntry::class, 1);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry->getKey(),
'billable_rate' => 1,
]);
}
/*
* Function: getBillableRateForTimeEntryWithGivenRelations
*/
public function test_billable_rate_is_null_if_time_entry_is_not_billable(): void
{
// Arrange
@@ -277,6 +788,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forProject($project)->forMember($member)->forOrganization($organization)->create([
'billable' => false,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -288,6 +800,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(null, $billableRate);
}
@@ -310,6 +823,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forProject($project)->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -321,6 +835,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(4004, $billableRate);
}
@@ -343,6 +858,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forProject($project)->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -354,6 +870,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(3003, $billableRate);
}
@@ -373,6 +890,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forProject($project)->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -384,6 +902,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(3003, $billableRate);
}
@@ -406,6 +925,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forProject($project)->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -417,6 +937,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(2002, $billableRate);
}
@@ -433,6 +954,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -444,6 +966,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(2002, $billableRate);
}
@@ -466,6 +989,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forProject($project)->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -477,6 +1001,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(1001, $billableRate);
}
@@ -493,6 +1018,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -504,6 +1030,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(1001, $billableRate);
}
@@ -526,6 +1053,7 @@ class BillableRateServiceTest extends TestCase
$timeEntry = TimeEntry::factory()->forProject($project)->forMember($member)->forOrganization($organization)->create([
'billable' => true,
]);
$this->enableQueryLog();
// Act
$billableRate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
@@ -537,6 +1065,7 @@ class BillableRateServiceTest extends TestCase
);
// Assert
$this->assertQueryCount(0);
$this->assertSame(null, $billableRate);
}
}