Skip to content
Permalink
Browse files

Episode 17 Complete

  • Loading branch information...
JeffreyWay committed Jan 20, 2019
1 parent 29c159e commit 9b18bb34afafbe61116a43d407feadf47be75680
Showing with 101 additions and 50 deletions.
  1. +10 −20 tests/Feature/ManageProjectsTest.php
  2. +17 −29 tests/Feature/ProjectTasksTest.php
  3. +69 −0 tests/Setup/ProjectFactory.php
  4. +5 −1 tests/TestCase.php
@@ -3,6 +3,7 @@
namespace Tests\Feature;
use App\Project;
use Facades\Tests\Setup\ProjectFactory;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -25,8 +26,6 @@ public function guests_cannot_manage_projects()
/** @test */
public function a_user_can_create_a_project()
{
$this->withoutExceptionHandling();
$this->signIn();
$this->get('/projects/create')->assertStatus(200);
@@ -43,8 +42,6 @@ public function a_user_can_create_a_project()
$response->assertRedirect($project->path());
$this->assertDatabaseHas('projects', $attributes);
$this->get($project->path())
->assertSee($attributes['title'])
->assertSee($attributes['description'])
@@ -54,29 +51,22 @@ public function a_user_can_create_a_project()
/** @test */
function a_user_can_update_a_project()
{
$this->signIn();
$this->withoutExceptionHandling();
$project = factory('App\Project')->create(['owner_id' => auth()->id()]);
$project = ProjectFactory::create();
$this->patch($project->path(), [
'notes' => 'Changed'
])->assertRedirect($project->path());
$this->actingAs($project->owner)
->patch($project->path(), $attributes = ['notes' => 'Changed'])
->assertRedirect($project->path());
$this->assertDatabaseHas('projects', ['notes' => 'Changed']);
$this->assertDatabaseHas('projects', $attributes);
}
/** @test */
public function a_user_can_view_their_project()
{
$this->signIn();
$this->withoutExceptionHandling();
$project = ProjectFactory::create();
$project = factory('App\Project')->create(['owner_id' => auth()->id()]);
$this->get($project->path())
$this->actingAs($project->owner)
->get($project->path())
->assertSee($project->title)
->assertSee($project->description);
}
@@ -98,7 +88,7 @@ public function an_authenticated_user_cannot_update_the_projects_of_others()
$project = factory('App\Project')->create();
$this->patch($project->path(), [])->assertStatus(403);
$this->patch($project->path())->assertStatus(403);
}
/** @test */
@@ -3,6 +3,7 @@
namespace Tests\Feature;
use App\Project;
use Facades\Tests\Setup\ProjectFactory;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -36,25 +37,21 @@ function only_the_owner_of_a_project_may_update_a_task()
{
$this->signIn();
$project = factory('App\Project')->create();
$task = $project->addTask('test task');
$project = ProjectFactory::withTasks(1)->create();
$this->patch($task->path(), ['body' => 'changed'])
->assertStatus(403);
$this->patch($project->tasks[0]->path(), ['body' => 'changed'])
->assertStatus(403);
$this->assertDatabaseMissing('tasks', ['body' => 'changed']);
}
/** @test */
public function a_project_can_have_tasks()
{
$this->signIn();
$project = auth()->user()->projects()->create(
factory(Project::class)->raw()
);
$project = ProjectFactory::create();
$this->post($project->path() . '/tasks', ['body' => 'Test task']);
$this->actingAs($project->owner)
->post($project->path() . '/tasks', ['body' => 'Test task']);
$this->get($project->path())
->assertSee('Test task');
@@ -63,20 +60,13 @@ public function a_project_can_have_tasks()
/** @test */
function a_task_can_be_updated()
{
$this->withoutExceptionHandling();
$this->signIn();
$project = auth()->user()->projects()->create(
factory(Project::class)->raw()
);
$project = ProjectFactory::withTasks(1)->create();
$task = $project->addTask('test task');
$this->patch($project->path() . '/tasks/' . $task->id, [
'body' => 'changed',
'completed' => true
]);
$this->actingAs($project->owner)
->patch($project->tasks[0]->path(), [
'body' => 'changed',
'completed' => true
]);
$this->assertDatabaseHas('tasks', [
'body' => 'changed',
@@ -87,14 +77,12 @@ function a_task_can_be_updated()
/** @test */
public function a_task_requires_a_body()
{
$this->signIn();
$project = auth()->user()->projects()->create(
factory(Project::class)->raw()
);
$project = ProjectFactory::create();
$attributes = factory('App\Task')->raw(['body' => '']);
$this->post($project->path() . '/tasks', $attributes)->assertSessionHasErrors('body');
$this->actingAs($project->owner)
->post($project->path() . '/tasks', $attributes)
->assertSessionHasErrors('body');
}
}
@@ -0,0 +1,69 @@
<?php
namespace Tests\Setup;
use App\Project;
use App\Task;
use App\User;
class ProjectFactory
{
/**
* The number of tasks for the project.
*
* @var int
*/
protected $tasksCount = 0;
/**
* The owner of the project.
*
* @var User
*/
protected $user;
/**
* Set the number of tasks to create for the project.
*
* @param int $count
* @return $this
*/
public function withTasks($count)
{
$this->tasksCount = $count;
return $this;
}
/**
* Set the owner of the new project.
*
* @param User $user
* @return $this
*/
public function ownedBy($user)
{
$this->user = $user;
return $this;
}
/**
* Arrange the world.
*
* @return Project
*/
public function create()
{
$project = factory(Project::class)->create([
'owner_id' => $this->user ?? factory(User::class)
]);
factory(Task::class, $this->tasksCount)->create([
'project_id' => $project
]);
return $project;
}
}
@@ -10,6 +10,10 @@ abstract class TestCase extends BaseTestCase
protected function signIn($user = null)
{
return $this->actingAs($user ?: factory('App\User')->create());
$user = $user ?: factory('App\User')->create();
$this->actingAs($user);
return $user;
}
}

0 comments on commit 9b18bb3

Please sign in to comment.
You can’t perform that action at this time.