Skip to content

Commit

Permalink
imporove test code
Browse files Browse the repository at this point in the history
  • Loading branch information
abbasfisal committed Aug 15, 2021
1 parent 644ad77 commit 6fbbcfd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/ProjectTasksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function store(Project $project)

public function update(Project $project, Task $task)
{

$this->authorize('update',$project);

$task->update([
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"Tests\\": "tests/" ,
"Setup\\": "tests/Setup"
}
},
"scripts": {
Expand Down
48 changes: 31 additions & 17 deletions tests/Feature/ProjectTask_FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Facades\Setup\ProjectFactory_Setup;
use Tests\TestCase;

class ProjectTask_FeatureTest extends TestCase
Expand All @@ -27,31 +29,44 @@ public function test_a_project_can_have_tasks()
public function test_a_project_can_update_task()
{
//$this->withoutExceptionHandling();
$this->signIn();

/*$this->signIn();
$project = Project::factory()->create(['user_id' => auth()->id()]);
$task = $project->addTask('test Task');
$task = $project->addTask('test Task');*/

$project =
ProjectFactory_Setup::
ownedBy($this->signIn())
->withTasks(1)
->create();

$this->patch($task->path(), [

$this->patch($project->tasks()->first()->path(), [
'body' => 'changed',
'completed' => true
]);
$this->assertDatabaseHas(Task::class , [
'body'=>'changed' ,
'completed'=>true
$this->assertDatabaseHas(Task::class, [
'body' => 'changed',
'completed' => true
]);
}

public function test_a_task_require_a_body()
{
$this->signIn();

$project = auth()->user()->projects()->create(Project::factory()->raw());
$project = auth()->user()
->projects()->create(Project::factory()->raw());

$attribute = Task::factory()->raw(['body' => '', 'project_id' => $project->id]);
$attribute = Task::factory()->raw([
'body' => '',
'project_id' => $project->id
]);

$this->post($project->path() . '/tasks', $attribute)->assertSessionHasErrors('body');
$this->post($project->path() . '/tasks', $attribute)
->assertSessionHasErrors('body');

}

Expand All @@ -70,15 +85,14 @@ public function test_only_owner_of_project_can_add_tasks()

public function test_only_owner_of_project_can_update_tasks()
{
//$this->withoutExceptionHandling();
$this->signIn();

$project = Project::factory()->create();
$task =$project->addTask('test Task');

$this->patch($task->path(),[
'body'=>'changed'
$project =
ProjectFactory_Setup::withTasks(1)
->create();

])->assertStatus(403);
$this->actingAs($this->signIn())
->patch($project->tasks()->first()->path(), [
'body' => 'changed'
])->assertStatus(403);
}
}
1 change: 0 additions & 1 deletion tests/Feature/Project_FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function a_user_can_create_a_project()

$this->withoutExceptionHandling();


$this->signIn();

$data = [
Expand Down
50 changes: 50 additions & 0 deletions tests/Setup/ProjectFactory_Setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php


namespace Setup;


use App\Models\Project;
use App\Models\Task;
use App\Models\User;

class ProjectFactory_Setup
{
private $TaskCount =0;
private $user;

public function withTasks($TaskCount)
{

$this->TaskCount = $TaskCount;
return $this;
}

public function ownedBy( $user = null)
{

$this->user = $user;

return $this;
}

public function create()
{


$project = Project::factory()->create([
'user_id' => $this->user ?? User::factory()
]);

$task =Task::factory()->count($this->TaskCount)->create([
'project_id'=>$project->id
]);

return $project ;

}
}




5 changes: 3 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ abstract class TestCase extends BaseTestCase

public function signIn($user = null)
{

return $this->actingAs($user ?: User::factory()->create());
$user = $user ?: User::factory()->create();
$this->actingAs($user);
return $user;
}
}

0 comments on commit 6fbbcfd

Please sign in to comment.