mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 17:57:04 +08:00
1d953b3514
[ci skip] [skip ci]
100 lines
2.5 KiB
PHP
100 lines
2.5 KiB
PHP
<?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\Command\CreateGroup;
|
|
use Flarum\Group\Event\Created;
|
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
|
use Flarum\Tests\integration\TestCase;
|
|
use Flarum\User\User;
|
|
use Illuminate\Contracts\Bus\Dispatcher;
|
|
use Illuminate\Contracts\Translation\Translator;
|
|
|
|
class EventTest extends TestCase
|
|
{
|
|
use RetrievesAuthorizedUsers;
|
|
|
|
protected function buildGroup()
|
|
{
|
|
$this->prepareDatabase([
|
|
'groups' => [
|
|
$this->adminGroup(),
|
|
],
|
|
'users' => [
|
|
$this->adminUser(),
|
|
],
|
|
]);
|
|
|
|
$bus = $this->app()->getContainer()->make(Dispatcher::class);
|
|
|
|
return $bus->dispatch(
|
|
new CreateGroup(User::find(1), ['attributes' => [
|
|
'nameSingular' => 'test group',
|
|
'namePlural' => 'test groups',
|
|
'color' => '#000000',
|
|
'icon' => 'fas fa-crown',
|
|
]])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function custom_listener_doesnt_work_by_default()
|
|
{
|
|
$group = $this->buildGroup();
|
|
|
|
$this->assertEquals('test group', $group->name_singular);
|
|
}
|
|
|
|
/**
|
|
* @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('modified group', $group->name_singular);
|
|
}
|
|
|
|
/**
|
|
* @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('core.group.admin', $group->name_singular);
|
|
}
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|