Skip to content

Commit

Permalink
Episode 17 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Jan 20, 2019
1 parent 29c159e commit 9b18bb3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 50 deletions.
30 changes: 10 additions & 20 deletions tests/Feature/ManageProjectsTest.php
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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'])
Expand All @@ -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);
}
Expand All @@ -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 */
Expand Down
46 changes: 17 additions & 29 deletions tests/Feature/ProjectTasksTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature;

use App\Project;
use Facades\Tests\Setup\ProjectFactory;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -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');
Expand All @@ -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',
Expand All @@ -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');
}
}
69 changes: 69 additions & 0 deletions tests/Setup/ProjectFactory.php
@@ -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;
}
}

6 changes: 5 additions & 1 deletion tests/TestCase.php
Expand Up @@ -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.