Added export endpoint and solidtime import; Enhanced toggl import

This commit is contained in:
Constantin Graf
2024-08-22 17:50:04 +02:00
committed by Constantin Graf
parent 056a63e193
commit ee77de04ef
43 changed files with 1304 additions and 110 deletions

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service\Export;
use App\Models\Client;
use App\Models\Member;
use App\Models\Organization;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\Task;
use App\Models\TimeEntry;
use App\Models\User;
use App\Service\Export\ExportService;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Tests\TestCaseWithDatabase;
#[CoversClass(ExportService::class)]
#[UsesClass(ExportService::class)]
class ExportServiceTest extends TestCaseWithDatabase
{
private function getFullOrganization(): Organization
{
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$organization = Organization::factory()->withOwner($user1)->create();
$member1 = Member::factory()->forUser($user1)->forOrganization($organization)->create();
$member2 = Member::factory()->forUser($user2)->forOrganization($organization)->create();
$timeEntry1 = TimeEntry::factory()->forMember($member1)->create();
$timeEntry2 = TimeEntry::factory()->forMember($member1)->create();
$project1 = Project::factory()->forOrganization($organization)->create();
$project2 = Project::factory()->forOrganization($organization)->create();
$task1 = Task::factory()->forOrganization($organization)->forProject($project1)->create();
$task2 = Task::factory()->forOrganization($organization)->forProject($project1)->create();
$task3 = Task::factory()->forOrganization($organization)->forProject($project2)->create();
$projectMember1 = ProjectMember::factory()->forMember($member1)->forProject($project1)->create();
$projectMember2 = ProjectMember::factory()->forMember($member2)->forProject($project1)->create();
$client1 = Client::factory()->forOrganization($organization)->create();
$client2 = Client::factory()->forOrganization($organization)->create();
return $organization;
}
public function test_export_creates_zip_with_all_the_data_of_the_organization(): void
{
// Arrange
$organization1 = $this->getFullOrganization();
$organization2 = $this->getFullOrganization();
// Act
$exportService = app(ExportService::class);
$zip = $exportService->export($organization1);
// Assert
Storage::disk('local')->assertExists($zip);
}
}

View File

@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Models\Organization;
use App\Models\TimeEntry;
use App\Service\Import\Importers\ClockifyTimeEntriesImporter;
use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
@@ -30,27 +29,17 @@ class ClockifyTimeEntriesImporterTest extends ImporterTestAbstract
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
$timeEntries = TimeEntry::all();
$this->assertCount(2, $timeEntries);
$timeEntry1 = $timeEntries->firstWhere('description', '');
$this->assertNotNull($timeEntry1);
$this->assertSame('', $timeEntry1->description);
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->start->toDateTimeString());
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->end->toDateTimeString());
$this->assertFalse($timeEntry1->billable);
$this->assertTrue($timeEntry1->is_imported);
$this->assertSame([$testScenario->tag1->getKey(), $testScenario->tag2->getKey()], $timeEntry1->tags);
$timeEntry2 = $timeEntries->firstWhere('description', 'Working hard');
$this->assertNotNull($timeEntry2);
$this->assertSame('Working hard', $timeEntry2->description);
$this->assertSame('2024-03-04 09:23:00', $timeEntry2->start->toDateTimeString());
$this->assertSame('2024-03-04 10:23:01', $timeEntry2->end->toDateTimeString());
$this->assertTrue($timeEntry2->billable);
$this->assertTrue($timeEntry2->is_imported);
$this->assertSame([], $timeEntry2->tags);
$this->checkTimeEntries($testScenario);
$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
@@ -67,26 +56,16 @@ class ClockifyTimeEntriesImporterTest extends ImporterTestAbstract
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
$timeEntries = TimeEntry::all();
$this->assertCount(4, $timeEntries);
$timeEntry1 = $timeEntries->firstWhere('description', '');
$this->assertNotNull($timeEntry1);
$this->assertSame('', $timeEntry1->description);
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->start->toDateTimeString());
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->end->toDateTimeString());
$this->assertFalse($timeEntry1->billable);
$this->assertTrue($timeEntry1->is_imported);
$this->assertSame([$testScenario->tag1->getKey(), $testScenario->tag2->getKey()], $timeEntry1->tags);
$timeEntry2 = $timeEntries->firstWhere('description', 'Working hard');
$this->assertNotNull($timeEntry2);
$this->assertSame('Working hard', $timeEntry2->description);
$this->assertSame('2024-03-04 09:23:00', $timeEntry2->start->toDateTimeString());
$this->assertSame('2024-03-04 10:23:01', $timeEntry2->end->toDateTimeString());
$this->assertTrue($timeEntry2->billable);
$this->assertTrue($timeEntry2->is_imported);
$this->assertSame([], $timeEntry2->tags);
$this->checkTimeEntries($testScenario, true);
$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

@@ -41,6 +41,7 @@ class ImporterProviderTest extends TestCase
'toggl_data_importer',
'clockify_time_entries',
'clockify_projects',
'solidtime',
], $keys);
}
}

View File

@@ -10,9 +10,14 @@ use App\Models\Member;
use App\Models\Project;
use App\Models\Tag;
use App\Models\Task;
use App\Models\TimeEntry;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use Tests\TestCase;
use ZipArchive;
class ImporterTestAbstract extends TestCase
{
@@ -36,29 +41,82 @@ class ImporterTestAbstract extends TestCase
$this->assertNotNull($member1);
$this->assertSame(Role::Placeholder->value, $member1->role);
$clients = Client::all();
$this->assertCount(1, $clients);
$client1 = $clients->firstWhere('name', 'Big Company');
$this->assertNotNull($client1);
if ($detailed) {
$this->assertCount(2, $clients);
$client1 = $clients->firstWhere('name', 'Big Company');
$this->assertNotNull($client1);
$this->assertNull($client1->archived_at);
$client2 = $clients->firstWhere('name', 'Other Company (Archived)');
$this->assertNotNull($client2);
$this->assertNotNull($client2->archived_at);
} else {
$this->assertCount(1, $clients);
$client1 = $clients->firstWhere('name', 'Big Company');
$this->assertNotNull($client1);
$this->assertNull($client1->archived_at);
}
$projects = Project::with(['members'])->get();
$this->assertCount(2, $projects);
if ($detailed) {
$this->assertCount(3, $projects);
} else {
$this->assertCount(2, $projects);
}
/** @var Project|null $project1 */
$project1 = $projects->firstWhere('name', 'Project without Client');
$this->assertNotNull($project1);
$this->assertNull($project1->client_id);
/** @var Project|null $project2 */
$project2 = $projects->firstWhere('name', 'Project for Big Company');
$this->assertNotNull($project2);
$this->assertSame($client1->getKey(), $project2->client_id);
$project3 = null;
if ($detailed) {
/** @var Project|null $project3 */
$project3 = $projects->firstWhere('name', 'Project (Archived)');
$this->assertNotNull($project3);
// Project without Client
$this->assertSame(false, $project1->is_billable);
$this->assertSame(false, $project1->is_public);
$this->assertSame('#ef5350', $project1->color);
$this->assertSame(null, $project1->billable_rate);
// Project for Big Company
$this->assertSame(true, $project2->is_billable);
$this->assertSame(false, $project2->is_public);
$this->assertSame('#ec407a', $project2->color);
$this->assertSame(10001, $project2->billable_rate);
// Project (Archived)
$this->assertSame(true, $project3->is_billable);
$this->assertSame(true, $project3->is_public);
$this->assertSame('#6a407f', $project3->color);
$this->assertSame(null, $project3->billable_rate);
$this->assertSame($client2->getKey(), $project3->client_id);
// Project members
$projectMembersOfProject2 = $project2->members;
$this->assertCount(1, $projectMembersOfProject2);
$this->assertSame($user1->getKey(), $projectMembersOfProject2->first()->user_id);
$this->assertSame(10002, $projectMembersOfProject2->first()->billable_rate);
} else {
// Project without Client
$this->assertSame(false, $project1->is_public);
// Project for Big Company
$this->assertSame(false, $project2->is_public);
}
$tasks = Task::all();
$this->assertCount(1, $tasks);
if ($detailed) {
$this->assertCount(2, $tasks);
} else {
$this->assertCount(1, $tasks);
}
$task1 = $tasks->firstWhere('name', 'Task 1');
$this->assertNotNull($task1);
$this->assertNull($task1->done_at);
$this->assertSame($project2->getKey(), $task1->project_id);
if ($detailed) {
$task2 = $tasks->firstWhere('name', 'Task 2');
$this->assertNotNull($task1);
$this->assertSame($project2->getKey(), $task2->project_id);
$this->assertNotNull($task2->done_at);
}
$tags = Tag::all();
$this->assertCount(2, $tags);
$tag1 = $tags->firstWhere('name', 'Development');
@@ -69,6 +127,7 @@ class ImporterTestAbstract extends TestCase
'user1' => $user1,
'project1' => $project1,
'project2' => $project2,
'project3' => $project3,
'tag1' => $tag1,
'tag2' => $tag2,
];
@@ -111,4 +170,47 @@ class ImporterTestAbstract extends TestCase
'task1' => $task1,
];
}
/**
* @param object{user1: User, project1: Project, project2: Project, tag1: Tag, tag2: Tag} $testScenario
*/
protected function checkTimeEntries(object $testScenario, bool $secondRun = false): void
{
$timeEntries = TimeEntry::all();
if ($secondRun) {
$this->assertCount(4, $timeEntries);
} else {
$this->assertCount(2, $timeEntries);
}
$timeEntry1 = $timeEntries->firstWhere('description', '');
$this->assertNotNull($timeEntry1);
$this->assertSame('', $timeEntry1->description);
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->start->toDateTimeString());
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->end->toDateTimeString());
$this->assertFalse($timeEntry1->billable);
$this->assertTrue($timeEntry1->is_imported);
$this->assertSame([$testScenario->tag1->getKey(), $testScenario->tag2->getKey()], $timeEntry1->tags);
$timeEntry2 = $timeEntries->firstWhere('description', 'Working hard');
$this->assertNotNull($timeEntry2);
$this->assertSame('Working hard', $timeEntry2->description);
$this->assertSame('2024-03-04 09:23:00', $timeEntry2->start->toDateTimeString());
$this->assertSame('2024-03-04 10:23:01', $timeEntry2->end->toDateTimeString());
$this->assertTrue($timeEntry2->billable);
$this->assertTrue($timeEntry2->is_imported);
$this->assertSame([], $timeEntry2->tags);
}
protected function createTestZip(string $folder): string
{
$tempDir = TemporaryDirectory::make();
$zipPath = $tempDir->path('test.zip');
$zip = new ZipArchive();
$zip->open($zipPath, ZipArchive::CREATE);
foreach (Storage::disk('testfiles')->allFiles($folder) as $file) {
$zip->addFile(Storage::disk('testfiles')->path($file), Str::of($file)->after($folder.'/')->value());
}
$zip->close();
return $zipPath;
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Models\Organization;
use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
use App\Service\Import\Importers\SolidtimeImporter;
use Exception;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
#[CoversClass(SolidtimeImporter::class)]
#[CoversClass(ImportException::class)]
#[CoversClass(DefaultImporter::class)]
#[UsesClass(SolidtimeImporter::class)]
class SolidtimeImporterTest extends ImporterTestAbstract
{
public function test_import_throws_exception_if_data_is_not_zip(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new SolidtimeImporter();
$importer->init($organization);
// Act
try {
$importer->importData('not a zip', $timezone);
} catch (Exception $e) {
$this->assertInstanceOf(ImportException::class, $e);
$this->assertSame('Invalid ZIP, error code: 19', $e->getMessage());
return;
}
$this->fail();
}
public function test_import_of_test_file_succeeds(): void
{
// Arrange
$zipPath = $this->createTestZip('solidtime_import_test_1');
$timezone = 'Europe/Vienna';
$organization = Organization::factory()->create();
$importer = new SolidtimeImporter();
$importer->init($organization);
$data = file_get_contents($zipPath);
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries(true);
$this->checkTimeEntries($testScenario);
$this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated);
$this->assertSame(2, $report->tasksCreated);
$this->assertSame(1, $report->usersCreated);
$this->assertSame(3, $report->projectsCreated);
$this->assertSame(2, $report->clientsCreated);
}
public function test_import_of_test_file_twice_succeeds(): void
{
// Arrange
$zipPath = $this->createTestZip('solidtime_import_test_1');
$timezone = 'Europe/Vienna';
$organization = Organization::factory()->create();
$importer = new SolidtimeImporter();
$importer->init($organization);
$data = file_get_contents($zipPath);
$importer->importData($data, $timezone);
$importer = new SolidtimeImporter();
$importer->init($organization);
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries(true);
$this->checkTimeEntries($testScenario, true);
$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

@@ -9,12 +9,8 @@ use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
use App\Service\Import\Importers\TogglDataImporter;
use Exception;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use ZipArchive;
#[CoversClass(TogglDataImporter::class)]
#[CoversClass(ImportException::class)]
@@ -22,20 +18,6 @@ use ZipArchive;
#[UsesClass(TogglDataImporter::class)]
class TogglDataImporterTest extends ImporterTestAbstract
{
private function createTestZip(string $folder): string
{
$tempDir = TemporaryDirectory::make();
$zipPath = $tempDir->path('test.zip');
$zip = new ZipArchive();
$zip->open($zipPath, ZipArchive::CREATE);
foreach (Storage::disk('testfiles')->allFiles($folder) as $file) {
$zip->addFile(Storage::disk('testfiles')->path($file), Str::of($file)->after($folder.'/')->value());
}
$zip->close();
return $zipPath;
}
public function test_import_throws_exception_if_data_is_not_zip(): void
{
// Arrange
@@ -74,10 +56,10 @@ class TogglDataImporterTest extends ImporterTestAbstract
$this->checkTestScenarioAfterImportExcludingTimeEntries(true);
$this->assertSame(0, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
$this->assertSame(2, $report->tasksCreated);
$this->assertSame(1, $report->usersCreated);
$this->assertSame(2, $report->projectsCreated);
$this->assertSame(1, $report->clientsCreated);
$this->assertSame(3, $report->projectsCreated);
$this->assertSame(2, $report->clientsCreated);
}
public function test_import_of_test_file_twice_succeeds(): void

View File

@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Models\Organization;
use App\Models\TimeEntry;
use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
use App\Service\Import\Importers\TogglTimeEntriesImporter;
@@ -34,22 +33,7 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
$timeEntries = TimeEntry::all();
$this->assertCount(2, $timeEntries);
$timeEntry1 = $timeEntries->firstWhere('description', '');
$this->assertNotNull($timeEntry1);
$this->assertSame('', $timeEntry1->description);
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->start->toDateTimeString());
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->end->toDateTimeString());
$this->assertFalse($timeEntry1->billable);
$this->assertSame([$testScenario->tag1->getKey(), $testScenario->tag2->getKey()], $timeEntry1->tags);
$timeEntry2 = $timeEntries->firstWhere('description', 'Working hard');
$this->assertNotNull($timeEntry2);
$this->assertSame('Working hard', $timeEntry2->description);
$this->assertSame('2024-03-04 09:23:00', $timeEntry2->start->toDateTimeString());
$this->assertSame('2024-03-04 10:23:01', $timeEntry2->end->toDateTimeString());
$this->assertTrue($timeEntry2->billable);
$this->assertSame([], $timeEntry2->tags);
$this->checkTimeEntries($testScenario);
$this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
@@ -76,22 +60,7 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
// Assert
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
$timeEntries = TimeEntry::all();
$this->assertCount(4, $timeEntries);
$timeEntry1 = $timeEntries->firstWhere('description', '');
$this->assertNotNull($timeEntry1);
$this->assertSame('', $timeEntry1->description);
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->start->toDateTimeString());
$this->assertSame('2024-03-04 09:23:52', $timeEntry1->end->toDateTimeString());
$this->assertFalse($timeEntry1->billable);
$this->assertSame([$testScenario->tag1->getKey(), $testScenario->tag2->getKey()], $timeEntry1->tags);
$timeEntry2 = $timeEntries->firstWhere('description', 'Working hard');
$this->assertNotNull($timeEntry2);
$this->assertSame('Working hard', $timeEntry2->description);
$this->assertSame('2024-03-04 09:23:00', $timeEntry2->start->toDateTimeString());
$this->assertSame('2024-03-04 10:23:01', $timeEntry2->end->toDateTimeString());
$this->assertTrue($timeEntry2->billable);
$this->assertSame([], $timeEntry2->tags);
$this->checkTimeEntries($testScenario, true);
$this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(0, $report->tagsCreated);
$this->assertSame(0, $report->tasksCreated);