Added basic models and factories

This commit is contained in:
Constantin Graf
2024-01-21 18:35:29 +01:00
parent 28f7b43577
commit 6377df1f44
29 changed files with 1476 additions and 116 deletions

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Client;
use App\Models\Team;
class ClientModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_organization(): void
{
// Arrange
$organization = Team::factory()->create();
$client = Client::factory()->forOrganization($organization)->create();
// Act
$client->refresh();
$organizationRel = $client->organization;
// Assert
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
}

View File

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

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Client;
use App\Models\Project;
use App\Models\Task;
use App\Models\Team;
class ProjectModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_organization(): void
{
// Arrange
$organization = Team::factory()->create();
$project = Project::factory()->forOrganization($organization)->create();
// Act
$project->refresh();
$organizationRel = $project->organization;
// Assert
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
public function test_it_can_belong_to_a_client(): void
{
// Arrange
$client = Client::factory()->create();
$project = Project::factory()->forClient($client)->create();
// Act
$project->refresh();
$clientRel = $project->client;
// Assert
$this->assertNotNull($clientRel);
$this->assertTrue($clientRel->is($client));
}
public function test_it_can_belong_to_no_client(): void
{
// Arrange
$project = Project::factory()->forClient(null)->create();
// Act
$project->refresh();
$clientRel = $project->client;
// Assert
$this->assertNull($clientRel);
}
public function test_it_has_many_tasks(): void
{
// Arrange
$project = Project::factory()->create();
$tasks = Task::factory()->forProject($project)->createMany(3);
// Act
$project->refresh();
$tasksRel = $project->tasks;
// Assert
$this->assertNotNull($tasksRel);
$this->assertCount(3, $tasksRel);
$this->assertTrue($tasksRel->first()->is($tasks->first()));
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Tag;
use App\Models\Team;
class TagModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_organization(): void
{
// Arrange
$organization = Team::factory()->create();
$task = Tag::factory()->forOrganization($organization)->create();
// Act
$task->refresh();
$organizationRel = $task->organization;
// Assert
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Project;
use App\Models\Task;
use App\Models\Team;
class TaskModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_organization(): void
{
// Arrange
$organization = Team::factory()->create();
$task = Task::factory()->forOrganization($organization)->create();
// Act
$task->refresh();
$organizationRel = $task->organization;
// Assert
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
public function test_it_belongs_to_a_project(): void
{
// Arrange
$project = Project::factory()->create();
$task = Task::factory()->forProject($project)->create();
// Act
$task->refresh();
$projectRel = $task->project;
// Assert
$this->assertNotNull($projectRel);
$this->assertTrue($projectRel->is($project));
}
}

View File

@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Project;
use App\Models\Task;
use App\Models\Team;
use App\Models\TimeEntry;
use App\Models\User;
class TimeEntryModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_user(): void
{
// Arrange
$user = User::factory()->create();
$timeEntry = TimeEntry::factory()->forUser($user)->create();
// Act
$timeEntry->refresh();
$userRel = $timeEntry->user;
// Assert
$this->assertNotNull($userRel);
$this->assertTrue($userRel->is($user));
}
public function test_it_belongs_to_a_organization(): void
{
// Arrange
$organization = Team::factory()->create();
$timeEntry = TimeEntry::factory()->forOrganization($organization)->create();
// Act
$timeEntry->refresh();
$organizationRel = $timeEntry->organization;
// Assert
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
public function test_it_can_belong_to_a_project(): void
{
// Arrange
$project = Project::factory()->create();
$timeEntry = TimeEntry::factory()->forProject($project)->create();
// Act
$timeEntry->refresh();
$projectRel = $timeEntry->project;
// Assert
$this->assertNotNull($projectRel);
$this->assertTrue($projectRel->is($project));
}
public function test_it_can_belong_to_no_project(): void
{
// Arrange
$timeEntry = TimeEntry::factory()->forProject(null)->create();
// Act
$timeEntry->refresh();
$project = $timeEntry->project;
// Assert
$this->assertNull($project);
}
public function test_it_can_belong_to_a_task(): void
{
// Arrange
$task = Task::factory()->create();
$timeEntry = TimeEntry::factory()->forTask($task)->create();
// Act
$timeEntry->refresh();
$taskRel = $timeEntry->task;
// Assert
$this->assertNotNull($taskRel);
$this->assertTrue($taskRel->is($task));
}
public function test_it_can_belong_to_no_task(): void
{
// Arrange
$timeEntry = TimeEntry::factory()->forTask(null)->create();
// Act
$timeEntry->refresh();
$taskRel = $timeEntry->task;
// Assert
$this->assertNull($taskRel);
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\User;
use App\Providers\Filament\AdminPanelProvider;
use Filament\Panel;
class UserModelTest extends ModelTestAbstract
{
public function test_normal_user_can_not_access_admin_panel(): void
{
// Arrange
$user = User::factory()->create();
$panelProvider = new AdminPanelProvider(app());
$mainPanel = $panelProvider->panel(Panel::make());
// Act
$canAccess = $user->canAccessPanel($mainPanel);
// Assert
$this->assertFalse($canAccess);
}
}