Added tasks endpoints; Added more test

This commit is contained in:
Constantin Graf
2024-03-21 21:26:42 +01:00
parent 33eff16b6b
commit 4edfa7e941
33 changed files with 810 additions and 52 deletions

View File

@@ -18,11 +18,16 @@ class ApiEndpointTestAbstract extends TestCase
* @param array<string> $permissions
* @return object{user: User, organization: Organization}
*/
protected function createUserWithPermission(array $permissions): object
protected function createUserWithPermission(array $permissions, bool $isOwner = false): object
{
Jetstream::role('custom-test', 'Custom Test', $permissions)->description('Role custom for testing');
$organization = Organization::factory()->create();
Jetstream::role('custom-test', 'Custom Test', $permissions)
->description('Role custom for testing');
$user = User::factory()->create();
if ($isOwner) {
$organization = Organization::factory()->withOwner($user)->create();
} else {
$organization = Organization::factory()->create();
}
$organization->users()->attach($user, [
'role' => 'custom-test',
]);

View File

@@ -23,7 +23,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.clients.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_index_endpoint_returns_list_of_all_clients_of_organization_ordered_by_created_at_desc_per_default(): void
@@ -66,7 +66,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_store_endpoint_creates_new_client(): void
@@ -106,7 +106,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_fails_if_user_is_not_part_of_client_organization(): void
@@ -126,7 +126,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
$this->assertDatabaseHas(Client::class, [
'id' => $client->getKey(),
'name' => $client->name,
@@ -173,7 +173,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.clients.destroy', [$data->organization->getKey(), $client->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_destroy_endpoint_fails_if_user_is_not_part_of_client_organization(): void
@@ -190,7 +190,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.clients.destroy', [$data->organization->getKey(), $client->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
$this->assertDatabaseHas(Client::class, [
'id' => $client->getKey(),
'name' => $client->name,

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Models\Organization;
use App\Service\Import\Importers\ImportException;
use App\Service\Import\Importers\ReportDto;
use App\Service\Import\ImportService;
use Laravel\Passport\Passport;
@@ -28,7 +29,35 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_import_return_error_message_if_import_fails(): void
{
$user = $this->createUserWithPermission([
'import',
]);
$this->mock(ImportService::class, function (MockInterface $mock) use (&$user): void {
$mock->shouldReceive('import')
->withArgs(function (Organization $organization, string $importerType, string $data) use (&$user): bool {
return $organization->is($user->organization) && $importerType === 'toggl_time_entries' && $data === 'some data';
})
->andThrow(new ImportException('This is a test error!'))
->once();
});
Passport::actingAs($user->user);
// Act
$response = $this->postJson(route('api.v1.import.import', ['organization' => $user->organization->id]), [
'type' => 'toggl_time_entries',
'data' => 'some data',
]);
// Assert
$response->assertStatus(400);
$response->assertExactJson([
'message' => 'This is a test error!',
]);
}
public function test_import_calls_import_service_if_user_has_permission(): void

View File

@@ -8,7 +8,7 @@ use App\Models\Organization;
use App\Models\User;
use Laravel\Passport\Passport;
class UserEndpointTest extends ApiEndpointTestAbstract
class MemberEndpointTest extends ApiEndpointTestAbstract
{
public function test_index_returns_members_of_organization(): void
{
@@ -25,6 +25,29 @@ class UserEndpointTest extends ApiEndpointTestAbstract
$response->assertStatus(200);
}
public function test_invite_placeholder_succeeds_if_data_is_valid(): void
{
$data = $this->createUserWithPermission([
'users:invite-placeholder',
], true);
$user = User::factory()->create([
'is_placeholder' => true,
]);
$data->organization->users()->attach($user, [
'role' => 'placeholder',
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.users.invite-placeholder', [
'organization' => $data->organization->id,
'user' => $user->id,
]));
// Assert
$response->assertStatus(204);
}
public function test_invite_placeholder_fails_if_user_does_not_have_permission(): void
{
// Arrange
@@ -40,7 +63,7 @@ class UserEndpointTest extends ApiEndpointTestAbstract
$response = $this->postJson(route('api.v1.users.invite-placeholder', ['organization' => $data->organization->id, 'user' => $user->id]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_invite_placeholder_fails_if_user_is_not_part_of_organization(): void
@@ -60,7 +83,7 @@ class UserEndpointTest extends ApiEndpointTestAbstract
$response = $this->postJson(route('api.v1.users.invite-placeholder', ['organization' => $data->organization->id, 'user' => $user->id]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_invite_placeholder_returns_400_if_user_is_not_placeholder(): void

View File

@@ -20,7 +20,7 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.organizations.show', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_show_endpoint_returns_organization(): void
@@ -53,7 +53,7 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_updates_project(): void

View File

@@ -24,7 +24,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.projects.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_index_endpoint_returns_list_of_all_projects_of_organization(): void
@@ -58,7 +58,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.projects.show', [$data->organization->getKey(), $project->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_show_endpoint_fails_if_user_has_no_permission_to_view_projects(): void
@@ -73,7 +73,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.projects.show', [$data->organization->getKey(), $project->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_show_endpoint_returns_project(): void
@@ -108,7 +108,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_store_endpoint_creates_new_project(): void
@@ -180,7 +180,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_projects(): void
@@ -199,7 +199,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_updates_project(): void
@@ -240,7 +240,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.projects.destroy', [$data->organization->getKey(), $project->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_projects(): void
@@ -255,7 +255,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.projects.destroy', [$data->organization->getKey(), $project->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_destroy_endpoint_deletes_project(): void

View File

@@ -23,7 +23,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.tags.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_index_endpoint_returns_list_of_all_tags_of_organization_ordered_by_created_at_desc_per_default(): void
@@ -66,7 +66,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_store_endpoint_creates_new_tag(): void
@@ -106,7 +106,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_fails_if_user_is_not_part_of_tag_organization(): void
@@ -126,7 +126,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
$this->assertDatabaseHas(Tag::class, [
'id' => $tag->getKey(),
'name' => $tag->name,
@@ -173,7 +173,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.tags.destroy', [$data->organization->getKey(), $tag->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_destroy_endpoint_fails_if_user_is_not_part_of_tag_organization(): void
@@ -190,7 +190,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.tags.destroy', [$data->organization->getKey(), $tag->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
$this->assertDatabaseHas(Tag::class, [
'id' => $tag->getKey(),
'name' => $tag->name,

View File

@@ -0,0 +1,243 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Models\Project;
use App\Models\Task;
use Laravel\Passport\Passport;
class TaskEndpointTest extends ApiEndpointTestAbstract
{
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_tasks(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
Task::factory()->forOrganization($data->organization)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.tasks.index', [$data->organization->getKey()]));
// Assert
$response->assertForbidden();
}
public function test_index_endpoint_returns_list_of_all_tasks_of_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'tasks:view',
]);
$tasks = Task::factory()->forOrganization($data->organization)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.tasks.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(200);
$response->assertJsonCount(4, 'data');
}
public function test_index_endpoint_returns_list_of_all_tasks_of_organization_filtered_by_project(): void
{
// Arrange
$data = $this->createUserWithPermission([
'tasks:view',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
Task::factory()->forOrganization($data->organization)->createMany(4);
Task::factory()->forOrganization($data->organization)->forProject($project)->createMany(2);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.tasks.index', [
$data->organization->getKey(),
'project_id' => $project->getKey(),
]));
// Assert
$response->assertStatus(200);
$response->assertJsonCount(2, 'data');
}
public function test_index_endpoint_validation_fails_if_project_id_does_not_belong_to_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'tasks:view',
]);
$otherData = $this->createUserWithPermission([
'tasks:view',
]);
$project = Project::factory()->forOrganization($otherData->organization)->create();
Task::factory()->forOrganization($data->organization)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.tasks.index', [
$data->organization->getKey(),
'project_id' => $project->getKey(),
]));
// Assert
$response->assertStatus(422);
$response->assertInvalid([
'project_id',
]);
}
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_tags()
{
// Arrange
$data = $this->createUserWithPermission([
]);
$project = Project::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.tasks.store', [$data->organization->getKey()]), [
'name' => 'Task 1',
'project_id' => $project->getKey(),
]);
// Assert
$response->assertForbidden();
$this->assertDatabaseMissing(Task::class, [
'name' => 'Task 1',
]);
}
public function test_store_endpoint_creates_new_task_if_user_has_permission_to_create_tasks()
{
// Arrange
$data = $this->createUserWithPermission([
'tasks:create',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.tasks.store', [$data->organization->getKey()]), [
'name' => 'Task 1',
'project_id' => $project->getKey(),
]);
// Assert
$response->assertStatus(201);
$this->assertDatabaseHas(Task::class, [
'name' => 'Task 1',
'project_id' => $project->getKey(),
'organization_id' => $data->organization->id,
]);
}
public function test_update_endpoint_fails_if_user_has_no_permission(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$task = Task::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.tasks.update', [$data->organization->getKey(), $task->getKey()]), [
'name' => 'Updated Task',
]);
// Assert
$response->assertForbidden();
$this->assertDatabaseHas(Task::class, [
'id' => $task->getKey(),
'name' => $task->name,
]);
$this->assertDatabaseMissing(Task::class, [
'id' => $task->getKey(),
'name' => 'Updated Task',
]);
}
public function test_update_endpoint_updates_task_if_user_has_permission(): void
{
// Arrange
$data = $this->createUserWithPermission([
'tasks:update',
]);
$task = Task::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.tasks.update', [$data->organization->getKey(), $task->getKey()]), [
'name' => 'Updated Task',
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Task::class, [
'id' => $task->getKey(),
'name' => 'Updated Task',
]);
}
public function test_delete_endpoint_deletes_tasks_if_user_has_permission(): void
{
// Arrange
$data = $this->createUserWithPermission([
'tasks:delete',
]);
$task = Task::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.tasks.destroy', [$data->organization->getKey(), $task->getKey()]));
// Assert
$response->assertStatus(204);
$this->assertDatabaseMissing(Task::class, [
'id' => $task->getKey(),
]);
}
public function test_delete_endpoint_fails_if_user_has_no_permission_to_delete_tasks(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$task = Task::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.tasks.destroy', [$data->organization->getKey(), $task->getKey()]));
// Assert
$response->assertForbidden();
$this->assertDatabaseHas(Task::class, [
'id' => $task->getKey(),
]);
}
public function test_delete_endpoint_fails_if_task_does_not_belong_to_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'tasks:delete',
]);
$otherData = $this->createUserWithPermission([
'tasks:delete',
]);
$task = Task::factory()->forOrganization($otherData->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.tasks.destroy', [$data->organization->getKey(), $task->getKey()]));
// Assert
$response->assertForbidden();
$this->assertDatabaseHas(Task::class, [
'id' => $task->getKey(),
]);
}
}

View File

@@ -26,7 +26,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.time-entries.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_time_entries_for_others_but_wants_all_entries(): void
@@ -41,7 +41,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$response = $this->getJson(route('api.v1.time-entries.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_index_endpoint_returns_time_entries_for_current_user(): void
@@ -323,7 +323,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_store_endpoint_fails_if_user_already_has_active_time_entry_and_tries_to_start_new_one(): void
@@ -463,7 +463,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_store_endpoint_creates_new_time_entry_for_other_user_in_organization(): void
@@ -520,7 +520,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_fails_if_user_is_not_part_of_time_entry_organization(): void
@@ -547,7 +547,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_time_entries_for_other_users_in_organization(): void
@@ -575,7 +575,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
]);
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_update_endpoint_updates_time_entry_for_current_user(): void
@@ -656,7 +656,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.time-entries.destroy', [$data->organization->getKey(), $timeEntry->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_destroy_endpoint_fails_if_user_tries_to_delete_non_existing_time_entry(): void
@@ -686,7 +686,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.time-entries.destroy', [$data->organization->getKey(), $timeEntry->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_time_entries_for_other_users_in_organization(): void
@@ -706,7 +706,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$response = $this->deleteJson(route('api.v1.time-entries.destroy', [$data->organization->getKey(), $timeEntry->getKey()]));
// Assert
$response->assertStatus(403);
$response->assertForbidden();
}
public function test_destroy_endpoint_deletes_own_time_entry(): void

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Web;
use App\Models\User;
class DashboardEndpointTest extends EndpointTestAbstract
{
public function test_showing_dashboard_succeeds_for_empty_user_with_no_data_entries(): void
{
// Arrange
$user = User::factory()->withPersonalOrganization()->create();
$this->actingAs($user);
// Act
$response = $this->get('/dashboard');
// Assert
$response->assertSuccessful();
}
}

View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Web;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
abstract class EndpointTestAbstract extends TestCase
{
use RefreshDatabase;
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service\Import;
use App\Models\Organization;
use App\Service\Import\ImportService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ImportServiceTest extends TestCase
{
use RefreshDatabase;
public function test_import_gets_importer_from_provider_runs_importer_and_returns_report(): void
{
// Arrange
$organization = Organization::factory()->create();
$data = file_get_contents(storage_path('tests/toggl_time_entries_import_test_1.csv'));
// Act
$importService = app(ImportService::class);
$report = $importService->import($organization, 'toggl_time_entries', $data);
// Assert
$this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
$this->assertSame(1, $report->usersCreated);
$this->assertSame(2, $report->projectsCreated);
$this->assertSame(1, $report->clientsCreated);
}
}

View File

@@ -18,7 +18,7 @@ class ClockifyProjectsImporterTest extends ImporterTestAbstract
$data = file_get_contents(storage_path('tests/clockify_projects_import_test_1.csv'));
// Act
$importer->importData($data, []);
$importer->importData($data);
// Assert
$this->checkTestScenarioProjectsOnlyAfterImport();
@@ -31,12 +31,12 @@ class ClockifyProjectsImporterTest extends ImporterTestAbstract
$importer = new ClockifyProjectsImporter();
$importer->init($organization);
$data = file_get_contents(storage_path('tests/clockify_projects_import_test_1.csv'));
$importer->importData($data, []);
$importer->importData($data);
$importer = new ClockifyProjectsImporter();
$importer->init($organization);
// Act
$importer->importData($data, []);
$importer->importData($data);
// Assert
$this->checkTestScenarioProjectsOnlyAfterImport();

View File

@@ -19,7 +19,7 @@ class ClockifyTimeEntriesImporterTest extends ImporterTestAbstract
$data = file_get_contents(storage_path('tests/clockify_time_entries_import_test_1.csv'));
// Act
$importer->importData($data, []);
$importer->importData($data);
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
@@ -48,12 +48,12 @@ class ClockifyTimeEntriesImporterTest extends ImporterTestAbstract
$importer = new ClockifyTimeEntriesImporter();
$importer->init($organization);
$data = file_get_contents(storage_path('tests/clockify_time_entries_import_test_1.csv'));
$importer->importData($data, []);
$importer->importData($data);
$importer = new ClockifyTimeEntriesImporter();
$importer->init($organization);
// Act
$importer->importData($data, []);
$importer->importData($data);
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();

View File

@@ -38,9 +38,17 @@ class TogglDataImporterTest extends ImporterTestAbstract
// Act
$importer->importData($data);
$report = $importer->getReport();
// Assert
$this->checkTestScenarioAfterImportExcludingTimeEntries();
$this->assertSame(0, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
$this->assertSame(1, $report->usersCreated);
$this->assertSame(2, $report->projectsCreated);
$this->assertSame(1, $report->clientsCreated);
}
public function test_import_of_test_file_twice_succeeds(): void
@@ -57,8 +65,15 @@ class TogglDataImporterTest extends ImporterTestAbstract
// Act
$importer->importData($data);
$report = $importer->getReport();
// Assert
$this->checkTestScenarioAfterImportExcludingTimeEntries();
$this->assertSame(0, $report->timeEntriesCreated);
$this->assertSame(0, $report->tagsCreated);
$this->assertSame(0, $report->tasksCreated);
$this->assertSame(0, $report->usersCreated);
$this->assertSame(0, $report->projectsCreated);
$this->assertSame(0, $report->clientsCreated);
}
}

View File

@@ -19,7 +19,8 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
$data = file_get_contents(storage_path('tests/toggl_time_entries_import_test_1.csv'));
// Act
$importer->importData($data, []);
$importer->importData($data);
$report = $importer->getReport();
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
@@ -39,6 +40,12 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
$this->assertSame('2024-03-04 11:23:01', $timeEntry2->end->toDateTimeString());
$this->assertTrue($timeEntry2->billable);
$this->assertSame([], $timeEntry2->tags);
$this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
$this->assertSame(1, $report->usersCreated);
$this->assertSame(2, $report->projectsCreated);
$this->assertSame(1, $report->clientsCreated);
}
public function test_import_of_test_file_twice_succeeds(): void
@@ -53,7 +60,8 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
$importer->init($organization);
// Act
$importer->importData($data, []);
$importer->importData($data);
$report = $importer->getReport();
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
@@ -73,5 +81,11 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
$this->assertSame('2024-03-04 11:23:01', $timeEntry2->end->toDateTimeString());
$this->assertTrue($timeEntry2->billable);
$this->assertSame([], $timeEntry2->tags);
$this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(0, $report->tagsCreated);
$this->assertSame(0, $report->tasksCreated);
$this->assertSame(0, $report->usersCreated);
$this->assertSame(0, $report->projectsCreated);
$this->assertSame(0, $report->clientsCreated);
}
}

View File

@@ -4,14 +4,18 @@ declare(strict_types=1);
namespace Tests\Unit\Service;
use App\Models\User;
use App\Service\TimezoneService;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
use TiMacDonald\Log\LogEntry;
class TimezoneServiceTest extends TestCase
{
public function test_get_timezones_returns_all_available_timezones(): void
{
// Arrange
$service = new \App\Service\TimezoneService();
$service = app(TimezoneService::class);
// Act
$result = $service->getTimezones();
@@ -23,4 +27,42 @@ class TimezoneServiceTest extends TestCase
$this->assertContains('Europe/Berlin', $result);
$this->assertContains('Europe/London', $result);
}
public function test_get_timezone_from_user_returns_timezone_of_user_as_carbon_timezone(): void
{
// Arrange
$user = User::factory()->create([
'timezone' => 'Europe/Berlin',
]);
/** @var TimezoneService $service */
$service = app(TimezoneService::class);
// Act
$result = $service->getTimezoneFromUser($user);
// Assert
$this->assertEquals('Europe/Berlin', $result->getName());
}
public function test_get_timezone_from_user_falls_back_to_utc_and_logs_this_failure_if_timezone_in_db_is_corrupt(): void
{
// Arrange
$corruptTimezone = 'Invalid/Timezone';
$user = User::factory()->create([
'timezone' => $corruptTimezone,
]);
/** @var TimezoneService $service */
$service = app(TimezoneService::class);
// Act
$result = $service->getTimezoneFromUser($user);
// Assert
$this->assertEquals('UTC', $result->getName());
Log::assertLogged(fn (LogEntry $log) => $log->level === 'error'
&& $log->message === 'User has a invalid timezone'
);
}
}