mirror of
https://github.com/flarum/core.git
synced 2025-07-31 13:40:20 +02:00
Add event extender (used for domain events) (#2097)
This commit is contained in:
committed by
GitHub
parent
ffa56595c3
commit
3ac5e58fa1
84
tests/integration/extenders/EventTest.php
Normal file
84
tests/integration/extenders/EventTest.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\extenders;
|
||||
|
||||
use Flarum\Extend;
|
||||
use Flarum\Group\Event\Created;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\Tests\integration\TestCase;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Contracts\Translation\Translator;
|
||||
|
||||
class EventTest extends TestCase
|
||||
{
|
||||
protected function buildGroup()
|
||||
{
|
||||
$events = $this->app()->getContainer()->make(Dispatcher::class);
|
||||
|
||||
$group = Group::build('test group', 'test groups', '#000000', 'fas fa-crown');
|
||||
$group->save();
|
||||
|
||||
$events->dispatch(new Created($group));
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_listener_doesnt_work_by_default()
|
||||
{
|
||||
$group = $this->buildGroup();
|
||||
|
||||
$this->assertEquals($group->name_singular, 'test group');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_listener_works_with_closure()
|
||||
{
|
||||
$this->extend((new Extend\Event)->listen(Created::class, function (Created $event) {
|
||||
$event->group->name_singular = 'modified group';
|
||||
}));
|
||||
|
||||
$group = $this->buildGroup();
|
||||
|
||||
$this->assertEquals($group->name_singular, 'modified group');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_listener_works_with_class_with_handle_method_and_can_inject_stuff()
|
||||
{
|
||||
// Because it injects a translator, this also tests that stuff can be injected into this callback.
|
||||
$this->extend((new Extend\Event)->listen(Created::class, CustomListener::class));
|
||||
|
||||
$group = $this->buildGroup();
|
||||
|
||||
$this->assertEquals($group->name_singular, 'core.group.admin');
|
||||
}
|
||||
}
|
||||
|
||||
class CustomListener
|
||||
{
|
||||
protected $translator;
|
||||
|
||||
public function __construct(Translator $translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function handle(Created $event)
|
||||
{
|
||||
$event->group->name_singular = $this->translator->trans('core.group.admin');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user