Skip to content

Commit

Permalink
Episode 12 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Jan 12, 2019
1 parent a4c38bd commit 0533ad7
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 9 deletions.
23 changes: 23 additions & 0 deletions app/Http/Controllers/ProjectTasksController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

use App\Project;

class ProjectTasksController extends Controller
{
/**
* Add a task to the given project.
*
* @param \App\Project $project
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Project $project)
{
request()->validate(['body' => 'required']);

$project->addTask(request('body'));

return redirect($project->path());
}
}
21 changes: 21 additions & 0 deletions app/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,25 @@ public function owner()
{
return $this->belongsTo(User::class);
}

/**
* The tasks associated with the project.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tasks()
{
return $this->hasMany(Task::class);
}

/**
* Add a task to the project.
*
* @param string $body
* @return \Illuminate\Database\Eloquent\Model
*/
public function addTask($body)
{
return $this->tasks()->create(compact('body'));
}
}
15 changes: 15 additions & 0 deletions app/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
/**
* Attributes to guard against mass assignment.
*
* @var array
*/
protected $guarded = [];
}
9 changes: 9 additions & 0 deletions database/factories/TaskFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Faker\Generator as Faker;

$factory->define(App\Task::class, function (Faker $faker) {
return [
'body' => $faker->sentence
];
});
33 changes: 33 additions & 0 deletions database/migrations/2019_01_12_191142_create_tasks_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('project_id');
$table->text('body');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
7 changes: 3 additions & 4 deletions resources/views/projects/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
<h2 class="text-lg text-grey font-normal mb-3">Tasks</h2>

{{-- tasks --}}
<div class="card mb-3">Lorem ipsum.</div>
<div class="card mb-3">Lorem ipsum.</div>
<div class="card mb-3">Lorem ipsum.</div>
<div class="card">Lorem ipsum.</div>
@foreach ($project->tasks as $task)
<div class="card mb-3">{{ $task->body }}</div>
@endforeach
</div>

<div>
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
Route::get('/projects/{project}', 'ProjectsController@show');
Route::post('/projects', 'ProjectsController@store');

Route::post('/projects/{project}/tasks', 'ProjectTasksController@store');

Route::get('/home', 'HomeController@index')->name('home');
});

Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/ManageProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function a_user_can_create_a_project()
{
$this->withoutExceptionHandling();

$this->actingAs(factory('App\User')->create());
$this->signIn();

$this->get('/projects/create')->assertStatus(200);

Expand All @@ -45,7 +45,7 @@ public function a_user_can_create_a_project()
/** @test */
public function a_user_can_view_their_project()
{
$this->be(factory('App\User')->create());
$this->signIn();

$this->withoutExceptionHandling();

Expand All @@ -59,7 +59,7 @@ public function a_user_can_view_their_project()
/** @test */
public function an_authenticated_user_cannot_view_the_projects_of_others()
{
$this->be(factory('App\User')->create());
$this->signIn();

$project = factory('App\Project')->create();

Expand All @@ -69,7 +69,7 @@ public function an_authenticated_user_cannot_view_the_projects_of_others()
/** @test */
public function a_project_requires_a_title()
{
$this->actingAs(factory('App\User')->create());
$this->signIn();

$attributes = factory('App\Project')->raw(['title' => '']);

Expand All @@ -79,7 +79,7 @@ public function a_project_requires_a_title()
/** @test */
public function a_project_requires_a_description()
{
$this->actingAs(factory('App\User')->create());
$this->signIn();

$attributes = factory('App\Project')->raw(['description' => '']);

Expand Down
49 changes: 49 additions & 0 deletions tests/Feature/ProjectTasksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Tests\Feature;

use App\Project;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ProjectTasksTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function guests_cannot_add_tasks_to_projects()
{
$project = factory('App\Project')->create();

$this->post($project->path() . '/tasks')->assertRedirect('login');
}

/** @test */
public function a_project_can_have_tasks()
{
$this->signIn();

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

$this->post($project->path() . '/tasks', ['body' => 'Test task']);

$this->get($project->path())
->assertSee('Test task');
}

/** @test */
public function a_task_requires_a_body()
{
$this->signIn();

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

$attributes = factory('App\Task')->raw(['body' => '']);

$this->post($project->path() . '/tasks', $attributes)->assertSessionHasErrors('body');
}
}
5 changes: 5 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;

protected function signIn($user = null)
{
return $this->actingAs($user ?: factory('App\User')->create());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ public function it_belongs_to_an_owner()

$this->assertInstanceOf('App\User', $project->owner);
}

/** @test */
public function it_can_add_a_task()
{
$project = factory('App\Project')->create();

$task = $project->addTask('Test task');

$this->assertCount(1, $project->tasks);
$this->assertTrue($project->tasks->contains($task));
}
}

0 comments on commit 0533ad7

Please sign in to comment.