-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
816d49a
commit 1a0bb81
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
database/migrations/2019_01_24_143508_create_activities_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |