Create and trigger events when tags are created and updated

parent ebefec72
<?php
namespace Modules\Tag\Events;
use Modules\Tag\Entities\Tag;
class TagWasCreated
{
/**
* @var Tag
*/
public $tag;
public function __construct(Tag $tag)
{
$this->tag = $tag;
}
}
<?php
namespace Modules\Tag\Events;
use Modules\Tag\Entities\Tag;
class TagWasUpdated
{
/**
* @var Tag
*/
public $tag;
public function __construct(Tag $tag)
{
$this->tag = $tag;
}
}
......@@ -3,6 +3,8 @@
namespace Modules\Tag\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Tag\Events\TagWasCreated;
use Modules\Tag\Events\TagWasUpdated;
use Modules\Tag\Repositories\TagRepository;
class EloquentTagRepository extends EloquentBaseRepository implements TagRepository
......@@ -16,4 +18,23 @@ class EloquentTagRepository extends EloquentBaseRepository implements TagReposit
{
return $this->model->with('translations')->where('namespace', $namespace)->get();
}
public function create($data)
{
$tag = $this->model->create($data);
event(new TagWasCreated($tag));
return $tag;
}
public function update($tag, $data)
{
$tag->update($data);
event(new TagWasUpdated($tag));
return $tag;
}
}
......@@ -2,6 +2,9 @@
namespace Modules\Tag\Tests\Integration;
use Illuminate\Support\Facades\Event;
use Modules\Tag\Events\TagWasCreated;
use Modules\Tag\Events\TagWasUpdated;
use Modules\Tag\Repositories\TagRepository;
use Modules\Tag\Tests\BaseTestCase;
......@@ -46,4 +49,41 @@ class EloquentTagRepositoryTest extends BaseTestCase
$this->assertCount(1, $this->tag->allForNamespace('asgardcms/blog'));
}
/** @test */
public function it_triggers_event_when_tag_was_created()
{
Event::fake();
$tag = $this->tag->create([
'namespace' => 'asgardcms/media',
'en' => [
'slug' => 'media-tag',
'name' => 'media tag',
],
]);
Event::assertDispatched(TagWasCreated::class, function ($e) use ($tag) {
return $e->tag->id === $tag->id;
});
}
/** @test */
public function it_triggers_event_when_tag_was_updated()
{
Event::fake();
$tag = $this->tag->create([
'namespace' => 'asgardcms/media',
'en' => [
'slug' => 'media-tag',
'name' => 'media tag',
],
]);
$this->tag->update($tag, []);
Event::assertDispatched(TagWasUpdated::class, function ($e) use ($tag) {
return $e->tag->id === $tag->id;
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment