2020-04-13 16:45:34 +08:00
|
|
|
<?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;
|
2021-01-16 09:33:29 +08:00
|
|
|
use Flarum\Foundation\Application;
|
2020-04-13 17:37:03 +08:00
|
|
|
use Flarum\Group\Command\CreateGroup;
|
2020-04-13 16:45:34 +08:00
|
|
|
use Flarum\Group\Event\Created;
|
2020-04-13 17:37:03 +08:00
|
|
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
2020-04-13 16:45:34 +08:00
|
|
|
use Flarum\Tests\integration\TestCase;
|
2020-04-13 17:37:03 +08:00
|
|
|
use Flarum\User\User;
|
2021-01-16 09:33:29 +08:00
|
|
|
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2020-10-06 04:02:12 +08:00
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
2020-04-13 16:45:34 +08:00
|
|
|
|
|
|
|
class EventTest extends TestCase
|
|
|
|
{
|
2020-04-13 17:37:03 +08:00
|
|
|
use RetrievesAuthorizedUsers;
|
|
|
|
|
2020-04-13 16:45:34 +08:00
|
|
|
protected function buildGroup()
|
|
|
|
{
|
2021-01-16 09:33:29 +08:00
|
|
|
$bus = $this->app()->getContainer()->make(BusDispatcher::class);
|
2020-04-13 17:59:07 +08:00
|
|
|
|
2020-04-13 17:37:03 +08:00
|
|
|
return $bus->dispatch(
|
|
|
|
new CreateGroup(User::find(1), ['attributes' => [
|
|
|
|
'nameSingular' => 'test group',
|
|
|
|
'namePlural' => 'test groups',
|
|
|
|
'color' => '#000000',
|
|
|
|
'icon' => 'fas fa-crown',
|
|
|
|
]])
|
|
|
|
);
|
2020-04-13 16:45:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function custom_listener_doesnt_work_by_default()
|
|
|
|
{
|
|
|
|
$group = $this->buildGroup();
|
|
|
|
|
2020-04-13 17:37:03 +08:00
|
|
|
$this->assertEquals('test group', $group->name_singular);
|
2020-04-13 16:45:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
|
2020-04-13 17:37:03 +08:00
|
|
|
$this->assertEquals('modified group', $group->name_singular);
|
2020-04-13 16:45:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
|
2020-04-13 17:37:03 +08:00
|
|
|
$this->assertEquals('core.group.admin', $group->name_singular);
|
2020-04-13 16:45:34 +08:00
|
|
|
}
|
2021-01-16 09:33:29 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function custom_subscriber_works()
|
|
|
|
{
|
|
|
|
// Because it injects a translator, this also tests that stuff can be injected into this callback.
|
|
|
|
$this->extend((new Extend\Event)->subscribe(CustomSubscriber::class));
|
|
|
|
|
|
|
|
$group = $this->buildGroup();
|
|
|
|
|
|
|
|
$this->assertEquals('core.group.admin', $group->name_singular);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function custom_subscriber_applied_after_app_booted()
|
|
|
|
{
|
|
|
|
// Because it injects a translator, this also tests that stuff can be injected into this callback.
|
|
|
|
$this->extend((new Extend\Event)->subscribe(CustomSubscriber::class));
|
|
|
|
|
|
|
|
$group = $this->buildGroup();
|
|
|
|
|
|
|
|
$this->assertEquals('booted', $group->name_plural);
|
|
|
|
}
|
2020-04-13 16:45:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class CustomListener
|
|
|
|
{
|
|
|
|
protected $translator;
|
|
|
|
|
2020-10-06 04:02:12 +08:00
|
|
|
public function __construct(TranslatorInterface $translator)
|
2020-04-13 16:45:34 +08:00
|
|
|
{
|
|
|
|
$this->translator = $translator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(Created $event)
|
|
|
|
{
|
|
|
|
$event->group->name_singular = $this->translator->trans('core.group.admin');
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 09:33:29 +08:00
|
|
|
|
|
|
|
class CustomSubscriber
|
|
|
|
{
|
|
|
|
protected $bootedAtConstruct;
|
|
|
|
protected $translator;
|
|
|
|
|
|
|
|
public function __construct(Application $app, TranslatorInterface $translator)
|
|
|
|
{
|
|
|
|
$this->bootedAtConstruct = $app->isBooted();
|
|
|
|
$this->translator = $translator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function subscribe(Dispatcher $events)
|
|
|
|
{
|
|
|
|
$events->listen(Created::class, [$this, 'whenGroupCreated']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function whenGroupCreated(Created $event)
|
|
|
|
{
|
|
|
|
$event->group->name_singular = $this->translator->trans('core.group.admin');
|
|
|
|
$event->group->name_plural = $this->bootedAtConstruct ? 'booted' : 'not booted';
|
|
|
|
}
|
|
|
|
}
|