mirror of
https://github.com/openvk/chandler.git
synced 2025-04-01 05:54:00 +03:00
Testing the Event Dispatcher class.
This commit is contained in:
parent
1f789219d9
commit
c4e87066e2
2 changed files with 86 additions and 2 deletions
|
@ -11,22 +11,35 @@ use Chandler\Patterns\TSimpleSingleton;
|
|||
*/
|
||||
class EventDispatcher
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $hooks = [];
|
||||
|
||||
/**
|
||||
* @param mixed $hook
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function addListener($hook): bool
|
||||
{
|
||||
$this->hooks[] = $hook;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Chandler\Eventing\Events\Event $event
|
||||
*
|
||||
* @return \Chandler\Eventing\Events\Event
|
||||
*/
|
||||
function pushEvent(Events\Event $event): Events\Event
|
||||
{
|
||||
foreach ($hooks as $hook) {
|
||||
foreach ($this->hooks as $hook) {
|
||||
if ($event instanceof Events\Cancelable)
|
||||
if ($event->isCancelled())
|
||||
break;
|
||||
$method = "on" . str_replace("Event", "", get_class($event));
|
||||
if (!method_exists($hook, $methodName)) continue;
|
||||
if (!method_exists($hook, $method)) continue;
|
||||
$hook->$method($event);
|
||||
}
|
||||
return $event;
|
||||
|
|
71
tests/Chandler/Eventing/EventDispatcherTest.php
Normal file
71
tests/Chandler/Eventing/EventDispatcherTest.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Chandler\Tests\Chandler\Eventing;
|
||||
|
||||
use Chandler\Eventing\EventDispatcher;
|
||||
use Chandler\Eventing\Events\Event;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @package Chandler\Tests\Chandler\Eventing
|
||||
*/
|
||||
class EventDispatcherTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function provideMethodAddListener(): array // IMPROVE: Add more values.
|
||||
{
|
||||
return [
|
||||
[
|
||||
null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function provideMethodPushEvent(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
new Event(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideMethodAddListener
|
||||
*
|
||||
* @param mixed $hook
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMethodAddListener($hook): void
|
||||
{
|
||||
$this->assertTrue(EventDispatcher::i()->addListener($hook));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testMethodI(): void
|
||||
{
|
||||
$this->assertSame(EventDispatcher::i(), EventDispatcher::i());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideMethodPushEvent
|
||||
*
|
||||
* @param \Chandler\Eventing\Events\Event $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMethodPushEvent(Event $event): void
|
||||
{
|
||||
$this->assertSame($event, EventDispatcher::i()->pushEvent($event));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue