Skip to content
Permalink
Browse files

Added dispatchEvent() wrapper method.

  • Loading branch information...
robertpustulka committed Sep 9, 2014
1 parent 6a58b98 commit 87d5dda5a0f464254f5ecedabc8f1964e475a7dc
@@ -28,6 +28,13 @@ trait EventManagerTrait {
*/
protected $_eventManager = null;
/**
* Default class name for new event objects.
*
* @var string
*/
protected $_eventClass = '\Cake\Event\Event';
/**
* Returns the Cake\Event\EventManager manager instance for this object.
*
@@ -46,4 +53,29 @@ public function eventManager(EventManager $eventManager = null) {
return $this->_eventManager;
}
/**
* Wrapper for creating and dispatching events.
*
* Returns a dispatched event.
*
* @param string $name Name of the event.
* @param array $data Any value you wish to be transported with this event to
* it can be read by listeners.
*
* @param object $subject The object that this event applies to
* ($this by default).
*
* @return \Cake\Event\Event
*/
public function dispatchEvent($name, $data = null, $subject = null) {
if ($subject === null) {
$subject = $this;
}
$event = new $this->_eventClass($name, $subject, $data);
$this->eventManager()->dispatch($event);
return $event;
}
}
@@ -368,6 +368,7 @@ public function testExportVar() {
[protected] _currentType => ''
[protected] _stack => []
[protected] _eventManager => object(Cake\Event\EventManager) {}
[protected] _eventClass => '\Cake\Event\Event'
}
TEXT;
@@ -57,4 +57,19 @@ public function testSettingEventManager() {
$this->assertSame($eventManager, $this->subject->eventManager());
}
/**
* testDispatchEvent
*
* @return void
*/
public function testDispatchEvent() {
$event = $this->subject->dispatchEvent('some.event', ['foo' => 'bar']);
$this->assertInstanceOf('\Cake\Event\Event', $event);
$this->assertSame($this->subject, $event->subject);
$this->assertEquals('some.event', $event->name);
$this->assertEquals(['foo' => 'bar'], $event->data);
}
}

0 comments on commit 87d5dda

Please sign in to comment.
You can’t perform that action at this time.