Skip to content
Permalink
Browse files

Episode 23 Complete

  • Loading branch information...
JeffreyWay committed Jan 31, 2019
1 parent d2e4e82 commit 50322ddffdd5e71c241e7e816553902b0abe639d
@@ -39,9 +39,7 @@ public function update(Project $project, Task $task)
$task->update(request()->validate(['body' => 'required']));
$method = request('completed') ? 'complete' : 'incomplete';
$task->$method();
request('completed') ? $task->complete() : $task->incomplete();
return redirect($project->path());
}
@@ -0,0 +1,30 @@
<?php
namespace App\Observers;
use App\Task;
class TaskObserver
{
/**
* Handle the task "created" event.
*
* @param \App\Task $task
* @return void
*/
public function created(Task $task)
{
$task->project->recordActivity('created_task');
}
/**
* Handle the task "deleted" event.
*
* @param \App\Task $task
* @return void
*/
public function deleted(Task $task)
{
$task->project->recordActivity('deleted_task');
}
}
@@ -3,7 +3,9 @@
namespace App\Providers;
use App\Observers\ProjectObserver;
use App\Observers\TaskObserver;
use App\Project;
use App\Task;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@@ -16,6 +18,7 @@ class AppServiceProvider extends ServiceProvider
public function boot()
{
Project::observe(ProjectObserver::class);
Task::observe(TaskObserver::class);
}
/**
@@ -29,18 +29,6 @@ class Task extends Model
'completed' => 'boolean'
];
/**
* Boot the model.
*/
protected static function boot()
{
parent::boot();
static::created(function ($task) {
$task->project->recordActivity('created_task');
});
}
/**
* Mark the task as complete.
*/
@@ -57,6 +45,8 @@ public function complete()
public function incomplete()
{
$this->update(['completed' => false]);
$this->project->recordActivity('incompleted_task');
}
/**
@@ -6,12 +6,12 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ActivityFeedTest extends TestCase
class TriggerActivityTest extends TestCase
{
use RefreshDatabase;
/** @test */
function creating_a_project_records_activity()
function creating_a_project()
{
$project = ProjectFactory::create();
@@ -20,7 +20,7 @@ function creating_a_project_records_activity()
}
/** @test */
function updating_a_project_records_activity()
function updating_a_project()
{
$project = ProjectFactory::create();
@@ -31,7 +31,7 @@ function updating_a_project_records_activity()
}
/** @test */
function creating_a_new_task_records_project_activity()
function creating_a_new_task()
{
$project = ProjectFactory::create();
@@ -42,7 +42,7 @@ function creating_a_new_task_records_project_activity()
}
/** @test */
function completing_a_new_task_records_project_activity()
function completing_a_task()
{
$project = ProjectFactory::withTasks(1)->create();
@@ -55,4 +55,39 @@ function completing_a_new_task_records_project_activity()
$this->assertCount(3, $project->activity);
$this->assertEquals('completed_task', $project->activity->last()->description);
}
/** @test */
function incompleting_a_task()
{
$project = ProjectFactory::withTasks(1)->create();
$this->actingAs($project->owner)
->patch($project->tasks[0]->path(), [
'body' => 'foobar',
'completed' => true
]);
$this->assertCount(3, $project->activity);
$this->patch($project->tasks[0]->path(), [
'body' => 'foobar',
'completed' => false
]);
$project->refresh();
$this->assertCount(4, $project->activity);
$this->assertEquals('incompleted_task', $project->activity->last()->description);
}
/** @test */
function deleting_a_task()
{
$project = ProjectFactory::withTasks(1)->create();
$project->tasks[0]->delete();
$this->assertCount(3, $project->activity);
}
}

0 comments on commit 50322dd

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