Added user and organization deletion system; Added coverage annotations

This commit is contained in:
Constantin Graf
2024-06-07 19:17:40 +02:00
committed by Constantin Graf
parent 8857befc6c
commit 86f5ea47bb
65 changed files with 2651 additions and 135 deletions

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Models\Organization;
use App\Service\Import\Importers\ClockifyProjectsImporter;
use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
#[CoversClass(ClockifyProjectsImporter::class)]
#[CoversClass(ImportException::class)]
#[CoversClass(DefaultImporter::class)]
#[UsesClass(ClockifyProjectsImporter::class)]
class ClockifyProjectsImporterTest extends ImporterTestAbstract
{
public function test_import_of_test_file_succeeds(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new ClockifyProjectsImporter();
$importer->init($organization);
$data = Storage::disk('testfiles')->get('clockify_projects_import_test_1.csv');
// Act
$importer->importData($data, $timezone);
// Assert
$this->checkTestScenarioProjectsOnlyAfterImport();
}
public function test_import_of_test_file_twice_succeeds(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new ClockifyProjectsImporter();
$importer->init($organization);
$data = Storage::disk('testfiles')->get('clockify_projects_import_test_1.csv');
$importer->importData($data, $timezone);
$importer = new ClockifyProjectsImporter();
$importer->init($organization);
// Act
$importer->importData($data, $timezone);
// Assert
$this->checkTestScenarioProjectsOnlyAfterImport();
}
}

View File

@@ -0,0 +1,92 @@
<?php
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;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
#[CoversClass(ClockifyTimeEntriesImporter::class)]
#[CoversClass(ImportException::class)]
#[CoversClass(DefaultImporter::class)]
#[UsesClass(ClockifyTimeEntriesImporter::class)]
class ClockifyTimeEntriesImporterTest extends ImporterTestAbstract
{
public function test_import_of_test_file_succeeds(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new ClockifyTimeEntriesImporter();
$importer->init($organization);
$data = Storage::disk('testfiles')->get('clockify_time_entries_import_test_1.csv');
// Act
$importer->importData($data, $timezone);
// 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);
}
public function test_import_of_test_file_twice_succeeds(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new ClockifyTimeEntriesImporter();
$importer->init($organization);
$data = Storage::disk('testfiles')->get('clockify_time_entries_import_test_1.csv');
$importer->importData($data, $timezone);
$importer = new ClockifyTimeEntriesImporter();
$importer->init($organization);
// Act
$importer->importData($data, $timezone);
// 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);
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Service\Import\Importers\ClockifyProjectsImporter;
use App\Service\Import\Importers\ImporterProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Tests\TestCase;
#[CoversClass(ImporterProvider::class)]
#[UsesClass(ImporterProvider::class)]
class ImporterProviderTest extends TestCase
{
public function test_register_importer_can_register_a_new_importer_for_example_in_an_extension(): void
{
// Arrange
$provider = new ImporterProvider();
// Act
$provider->registerImporter('some_provider_importer', ClockifyProjectsImporter::class);
// Assert
$importer = $provider->getImporter('some_provider_importer');
$this->assertSame(ClockifyProjectsImporter::class, $importer::class);
}
public function test_get_importer_keys_return_the_keys_of_the_available_importers(): void
{
// Arrange
$provider = new ImporterProvider();
// Act
$keys = $provider->getImporterKeys();
// Assert
$this->assertSame([
'toggl_time_entries',
'toggl_data_importer',
'clockify_time_entries',
'clockify_projects',
], $keys);
}
}

View File

@@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Enums\Role;
use App\Models\Client;
use App\Models\Member;
use App\Models\Project;
use App\Models\Tag;
use App\Models\Task;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ImporterTestAbstract extends TestCase
{
use RefreshDatabase;
/**
* @return object{user1: User, project1: Project, project2: Project, tag1: Tag, tag2: Tag}
*/
protected function checkTestScenarioAfterImportExcludingTimeEntries(bool $detailed = false): object
{
$users = User::all();
$this->assertCount(2, $users);
$user1 = $users->firstWhere('name', 'Peter Tester');
$this->assertNotNull($user1);
$this->assertSame(null, $user1->password);
$this->assertSame('Peter Tester', $user1->name);
$this->assertSame('peter.test@email.test', $user1->email);
$members = Member::all();
$this->assertCount(1, $members);
$member1 = $members->firstWhere('user_id', $user1->getKey());
$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);
$projects = Project::with(['members'])->get();
$this->assertCount(2, $projects);
$project1 = $projects->firstWhere('name', 'Project without Client');
$this->assertNotNull($project1);
$this->assertNull($project1->client_id);
$project2 = $projects->firstWhere('name', 'Project for Big Company');
$this->assertNotNull($project2);
$this->assertSame($client1->getKey(), $project2->client_id);
if ($detailed) {
$this->assertSame(10001, $project2->billable_rate);
$projectMembersOfProject2 = $project2->members;
$this->assertCount(1, $projectMembersOfProject2);
$this->assertSame($user1->getKey(), $projectMembersOfProject2->first()->user_id);
$this->assertSame(10002, $projectMembersOfProject2->first()->billable_rate);
}
$tasks = Task::all();
$this->assertCount(1, $tasks);
$task1 = $tasks->firstWhere('name', 'Task 1');
$this->assertNotNull($task1);
$this->assertSame($project2->getKey(), $task1->project_id);
$tags = Tag::all();
$this->assertCount(2, $tags);
$tag1 = $tags->firstWhere('name', 'Development');
$tag2 = $tags->firstWhere('name', 'Backend');
$this->assertNotNull($tag1);
return (object) [
'user1' => $user1,
'project1' => $project1,
'project2' => $project2,
'tag1' => $tag1,
'tag2' => $tag2,
];
}
/**
* @return object{client1: Client, project1: Project, project2: Project, task1: Task}
*/
protected function checkTestScenarioProjectsOnlyAfterImport(): object
{
$clients = Client::all();
$this->assertCount(1, $clients);
$client1 = $clients->firstWhere('name', 'Big Company');
$this->assertNotNull($client1);
$projects = Project::all();
$this->assertCount(2, $projects);
$project1 = $projects->firstWhere('name', 'Project without Client');
$this->assertNotNull($project1);
$this->assertNull($project1->client_id);
$project2 = $projects->firstWhere('name', 'Project for Big Company');
$this->assertNotNull($project2);
$this->assertSame(10001, $project2->billable_rate);
$this->assertSame($client1->getKey(), $project2->client_id);
$tasks = Task::all();
$this->assertCount(3, $tasks);
$task1 = $tasks->firstWhere('name', 'Task 1');
$this->assertNotNull($task1);
$this->assertSame($project2->getKey(), $task1->project_id);
$task2 = $tasks->firstWhere('name', 'Task 2');
$this->assertNotNull($task2);
$this->assertSame($project2->getKey(), $task2->project_id);
$task3 = $tasks->firstWhere('name', 'Task 3');
$this->assertNotNull($task3);
$this->assertSame($project2->getKey(), $task3->project_id);
return (object) [
'client1' => $client1,
'project1' => $project1,
'project2' => $project2,
'task1' => $task1,
];
}
}

View File

@@ -0,0 +1,109 @@
<?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\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)]
#[CoversClass(DefaultImporter::class)]
#[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
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new TogglDataImporter();
$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('toggl_data_import_test_1');
$timezone = 'Europe/Vienna';
$organization = Organization::factory()->create();
$importer = new TogglDataImporter();
$importer->init($organization);
$data = file_get_contents($zipPath);
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$this->checkTestScenarioAfterImportExcludingTimeEntries(true);
$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
{
// Arrange
$zipPath = $this->createTestZip('toggl_data_import_test_1');
$timezone = 'Europe/Vienna';
$organization = Organization::factory()->create();
$importer = new TogglDataImporter();
$importer->init($organization);
$data = file_get_contents($zipPath);
$importer->importData($data, $timezone);
$importer = new TogglDataImporter();
$importer->init($organization);
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$this->checkTestScenarioAfterImportExcludingTimeEntries(true);
$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

@@ -0,0 +1,102 @@
<?php
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;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
#[CoversClass(TogglTimeEntriesImporter::class)]
#[CoversClass(ImportException::class)]
#[CoversClass(DefaultImporter::class)]
#[UsesClass(TogglTimeEntriesImporter::class)]
class TogglTimeEntriesImporterTest extends ImporterTestAbstract
{
public function test_import_of_test_file_succeeds(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new TogglTimeEntriesImporter();
$importer->init($organization);
$data = Storage::disk('testfiles')->get('toggl_time_entries_import_test_1.csv');
// 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->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->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
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new TogglTimeEntriesImporter();
$importer->init($organization);
$data = Storage::disk('testfiles')->get('toggl_time_entries_import_test_1.csv');
$importer->importData($data, $timezone);
$importer = new TogglTimeEntriesImporter();
$importer->init($organization);
// 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->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->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);
}
}