Skip to content
Permalink
Browse files

Episode 12 Complete

  • Loading branch information...
JeffreyWay committed Jan 12, 2019
1 parent a4c38bd commit 0533ad74b757f8447b6351ad62a5cd4bed569d86
@@ -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());
}
}
@@ -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'));
}
}
@@ -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 = [];
}
@@ -0,0 +1,9 @@
<?php
use Faker\Generator as Faker;
$factory->define(App\Task::class, function (Faker $faker) {
return [
'body' => $faker->sentence
];
});
@@ -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');
}
}
@@ -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>
@@ -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');
});
@@ -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);
@@ -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();
@@ -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();
@@ -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' => '']);
@@ -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' => '']);
@@ -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');
}
}
@@ -7,4 +7,9 @@
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function signIn($user = null)
{
return $this->actingAs($user ?: factory('App\User')->create());
}
}
@@ -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.
You can’t perform that action at this time.