mirror of https://github.com/Devoalda/LaDo.git
test(Project CRUD Test cases):
Added: - CRUD test cases for project functions Ammended: - "show" test case to compare data intead of view Modified: - Model Relationships
This commit is contained in:
parent
018e0813d2
commit
03381acac4
|
@ -48,7 +48,7 @@ class ProjectController extends Controller
|
|||
|
||||
$data = $request->validated();
|
||||
|
||||
$user->projects()->save(new Project($data));
|
||||
$user->projects()->create($data);
|
||||
|
||||
return redirect()->route('project.index')
|
||||
->with('info', 'Project created!');
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
//use App\Http\Requests\StoreTodoRequest;
|
||||
//use App\Http\Requests\UpdateTodoRequest;
|
||||
use App\Http\Requests\Project\StoreTodoRequest;
|
||||
use App\Http\Requests\Project\UpdateTodoRequest;
|
||||
use App\Models\Todo;
|
||||
|
|
|
@ -49,13 +49,13 @@ class User extends Authenticatable
|
|||
'password' => 'hashed',
|
||||
];
|
||||
|
||||
public function projects(): HasManyThrough
|
||||
public function projects(): BelongsToMany
|
||||
{
|
||||
return $this->hasManyThrough(Project::class, projectUser::class, 'user_id', 'id', 'id', 'project_id');
|
||||
return $this->belongsToMany(Project::class, projectUser::class, 'user_id', 'project_id');
|
||||
}
|
||||
|
||||
public function todos(): HasManyThrough
|
||||
{
|
||||
return $this->hasManyThrough(Todo::class, projectUser::class, 'user_id', 'id', 'id', 'project_id');
|
||||
return $this->hasManyThrough(Todo::class, Project::class, 'owner_id', 'project_id');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Project;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProjectCRUDTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private User $user;
|
||||
private Project $project;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
$this->user = $user;
|
||||
$this->assertAuthenticated();
|
||||
|
||||
}
|
||||
|
||||
public function test_user_can_create_project(): void
|
||||
{
|
||||
// Create a project through POST and store it in the project property
|
||||
$response = $this->post(route('project.store'), [
|
||||
'name' => 'Test Project',
|
||||
'description' => 'Test Description',
|
||||
]);
|
||||
$response->assertRedirect(route('project.index'));
|
||||
$this->assertDatabaseHas('projects', [
|
||||
'name' => 'Test Project',
|
||||
'description' => 'Test Description',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('project_user', [
|
||||
'project_id' => Project::where('name', 'Test Project')->first()->id,
|
||||
'user_id' => $this->user->id,
|
||||
]);
|
||||
|
||||
$this->project = Project::where('name', 'Test Project')->first();
|
||||
}
|
||||
|
||||
// View Project after creation (Show to be implemented)
|
||||
public function test_user_can_view_project(): void
|
||||
{
|
||||
$this->test_user_can_create_project();
|
||||
// Get from project index page and assert that the project is visible
|
||||
$response = $this->get(route('project.index'));
|
||||
// Search for the project name and description
|
||||
$response->assertSee('Test Project');
|
||||
$response->assertSee('Test Description');
|
||||
}
|
||||
|
||||
// Edit Project after creation
|
||||
public function test_user_can_edit_project(): void
|
||||
{
|
||||
$this->test_user_can_create_project();
|
||||
$response = $this->get(route('project.edit', $this->project->id));
|
||||
$response->assertOk();
|
||||
$response->assertSee('Test Project');
|
||||
$response->assertSee('Test Description');
|
||||
}
|
||||
|
||||
// Update Project after creation
|
||||
public function test_user_can_update_project(): void
|
||||
{
|
||||
$this->test_user_can_create_project();
|
||||
$response = $this->put(route('project.update', $this->project->id), [
|
||||
'name' => 'Test Project Updated',
|
||||
'description' => 'Test Description Updated',
|
||||
]);
|
||||
$response->assertRedirect(back()->getTargetUrl());
|
||||
$this->assertDatabaseHas('projects', [
|
||||
'name' => 'Test Project Updated',
|
||||
'description' => 'Test Description Updated',
|
||||
]);
|
||||
}
|
||||
|
||||
// Delete Project after creation
|
||||
public function test_user_can_delete_project(): void
|
||||
{
|
||||
$this->test_user_can_create_project();
|
||||
$response = $this->delete(route('project.destroy', $this->project->id));
|
||||
$response->assertRedirect(route('project.index'));
|
||||
$this->assertDatabaseMissing('projects', [
|
||||
'name' => 'Test Project',
|
||||
'description' => 'Test Description',
|
||||
]);
|
||||
}
|
||||
|
||||
// View Project after deletion
|
||||
public function test_user_cannot_view_deleted_project(): void
|
||||
{
|
||||
$this->test_user_can_delete_project();
|
||||
$response = $this->get(route('project.show', $this->project->id));
|
||||
$response->assertNotFound();
|
||||
}
|
||||
|
||||
// Edit Project after deletion
|
||||
public function test_user_cannot_edit_deleted_project(): void
|
||||
{
|
||||
$this->test_user_can_delete_project();
|
||||
$response = $this->get(route('project.edit', $this->project->id));
|
||||
$response->assertNotFound();
|
||||
}
|
||||
|
||||
// Update Project after deletion
|
||||
public function test_user_cannot_update_deleted_project(): void
|
||||
{
|
||||
$this->test_user_can_delete_project();
|
||||
$response = $this->put(route('project.update', $this->project->id), [
|
||||
'name' => 'Test Project Updated',
|
||||
'description' => 'Test Description Updated',
|
||||
]);
|
||||
$response->assertNotFound();
|
||||
}
|
||||
|
||||
// Delete Project after deletion
|
||||
public function test_user_cannot_delete_deleted_project(): void
|
||||
{
|
||||
$this->test_user_can_delete_project();
|
||||
$response = $this->delete(route('project.destroy', $this->project->id));
|
||||
$response->assertNotFound();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue