Skip to content

Commit

Permalink
Episode 20 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Jan 24, 2019
1 parent 816d49a commit 1a0bb81
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/Activity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

namespace App\Observers;

use App\Activity;
use App\Project;

class ProjectObserver
{
/**
* Handle the project "created" event.
*
* @param \App\Project $project
* @return void
*/
public function created(Project $project)
{
$this->recordActivity('created', $project);
}

/**
* Handle the project "updated" event.
*
* @param \App\Project $project
* @return void
*/
public function updated(Project $project)
{
$this->recordActivity('updated', $project);
}

protected function recordActivity($type, $project)
{
Activity::create([
'project_id' => $project->id,
'description' => $type
]);
}
}
10 changes: 10 additions & 0 deletions app/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ public function addTask($body)
{
return $this->tasks()->create(compact('body'));
}

/**
* The activity feed for the project.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function activity()
{
return $this->hasMany(Activity::class);
}
}
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Observers\ProjectObserver;
use App\Project;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -13,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions database/migrations/2019_01_24_143508_create_activities_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

class CreateActivitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('activities', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('project_id');
$table->string('description');
$table->timestamps();

$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activities');
}
}
33 changes: 33 additions & 0 deletions tests/Feature/ActivityFeedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Feature;

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

class ActivityFeedTest extends TestCase
{
use RefreshDatabase;

/** @test */
function creating_a_project_generates_activity()
{
$project = ProjectFactory::create();

$this->assertCount(1, $project->activity);
$this->assertEquals('created', $project->activity[0]->description);
}

/** @test */
function updating_a_project_generates_activity()
{
$project = ProjectFactory::create();

$project->update(['title' => 'Changed']);

$this->assertCount(2, $project->activity);
$this->assertEquals('updated', $project->activity->last()->description);

}
}

0 comments on commit 1a0bb81

Please sign in to comment.