2011-12-13 04:52:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 12:28:17 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-12-13 04:52:40 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-12-13 04:52:40 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 12:28:17 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-12-25 01:07:22 +00:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
|
|
|
* @package Cake.Test.Case.Event
|
|
|
|
* @since CakePHP v 2.1
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('CakeEvent', 'Event');
|
|
|
|
App::uses('CakeEventManager', 'Event');
|
2011-12-13 05:43:25 +00:00
|
|
|
App::uses('CakeEventListener', 'Event');
|
2011-12-13 04:52:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock class used to test event dispatching
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Event
|
|
|
|
*/
|
|
|
|
class CakeEventTestListener {
|
|
|
|
|
|
|
|
public $callStack = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test function to be used in event dispatching
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function listenerFunction() {
|
|
|
|
$this->callStack[] = __FUNCTION__;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test function to be used in event dispatching
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function secondListenerFunction() {
|
|
|
|
$this->callStack[] = __FUNCTION__;
|
|
|
|
}
|
2011-12-14 05:12:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Auxiliary function to help in stopPropagation testing
|
|
|
|
*
|
2012-10-16 00:19:22 +00:00
|
|
|
* @param CakeEvent $event
|
2011-12-14 05:12:05 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function stopListener($event) {
|
|
|
|
$event->stopPropagation();
|
|
|
|
}
|
2012-03-13 01:22:17 +00:00
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 05:43:25 +00:00
|
|
|
/**
|
|
|
|
* Mock used for testing the subscriber objects
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Event
|
|
|
|
*/
|
2013-10-06 00:12:57 +00:00
|
|
|
class CustomTestEventListener extends CakeEventTestListener implements CakeEventListener {
|
2011-12-13 05:43:25 +00:00
|
|
|
|
|
|
|
public function implementedEvents() {
|
|
|
|
return array(
|
|
|
|
'fake.event' => 'listenerFunction',
|
2011-12-25 01:07:22 +00:00
|
|
|
'another.event' => array('callable' => 'secondListenerFunction', 'passParams' => true),
|
|
|
|
'multiple.handlers' => array(
|
|
|
|
array('callable' => 'listenerFunction'),
|
|
|
|
array('callable' => 'thirdListenerFunction')
|
|
|
|
)
|
2011-12-13 05:43:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test function to be used in event dispatching
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function thirdListenerFunction() {
|
|
|
|
$this->callStack[] = __FUNCTION__;
|
|
|
|
}
|
2012-03-13 01:22:17 +00:00
|
|
|
|
2011-12-13 05:43:25 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
/**
|
|
|
|
* Tests the CakeEventManager class functionality
|
|
|
|
*/
|
|
|
|
class CakeEventManagerTest extends CakeTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the attach() method for a single event key in multiple queues
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAttachListeners() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach('fakeFunction', 'fake.event');
|
2011-12-13 04:52:40 +00:00
|
|
|
$expected = array(
|
|
|
|
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
|
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach('fakeFunction2', 'fake.event');
|
2011-12-13 04:52:40 +00:00
|
|
|
$expected[] = array('callable' => 'fakeFunction2', 'passParams' => false);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
|
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach('inQ5', 'fake.event', array('priority' => 5));
|
|
|
|
$manager->attach('inQ1', 'fake.event', array('priority' => 1));
|
|
|
|
$manager->attach('otherInQ5', 'fake.event', array('priority' => 5));
|
2011-12-13 04:52:40 +00:00
|
|
|
|
|
|
|
$expected = array_merge(
|
|
|
|
array(
|
|
|
|
array('callable' => 'inQ1', 'passParams' => false),
|
|
|
|
array('callable' => 'inQ5', 'passParams' => false),
|
|
|
|
array('callable' => 'otherInQ5', 'passParams' => false)
|
|
|
|
),
|
|
|
|
$expected
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the attach() method for multiple event key in multiple queues
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAttachMultipleEventKeys() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach('fakeFunction', 'fake.event');
|
|
|
|
$manager->attach('fakeFunction2', 'another.event');
|
|
|
|
$manager->attach('fakeFunction3', 'another.event', array('priority' => 1, 'passParams' => true));
|
2011-12-13 04:52:40 +00:00
|
|
|
$expected = array(
|
|
|
|
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
array('callable' => 'fakeFunction3', 'passParams' => true),
|
|
|
|
array('callable' => 'fakeFunction2', 'passParams' => false)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests detaching an event from a event key queue
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDetach() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach(array('AClass', 'aMethod'), 'fake.event');
|
|
|
|
$manager->attach(array('AClass', 'anotherMethod'), 'another.event');
|
|
|
|
$manager->attach('fakeFunction', 'another.event', array('priority' => 1));
|
2011-12-13 04:52:40 +00:00
|
|
|
|
|
|
|
$manager->detach(array('AClass', 'aMethod'), 'fake.event');
|
|
|
|
$this->assertEquals(array(), $manager->listeners('fake.event'));
|
|
|
|
|
|
|
|
$manager->detach(array('AClass', 'anotherMethod'), 'another.event');
|
|
|
|
$expected = array(
|
|
|
|
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
|
|
|
|
|
$manager->detach('fakeFunction', 'another.event');
|
|
|
|
$this->assertEquals(array(), $manager->listeners('another.event'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests detaching an event from all event queues
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDetachFromAll() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach(array('AClass', 'aMethod'), 'fake.event');
|
|
|
|
$manager->attach(array('AClass', 'aMethod'), 'another.event');
|
|
|
|
$manager->attach('fakeFunction', 'another.event', array('priority' => 1));
|
2011-12-13 04:52:40 +00:00
|
|
|
|
|
|
|
$manager->detach(array('AClass', 'aMethod'));
|
|
|
|
$expected = array(
|
|
|
|
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
|
$this->assertEquals(array(), $manager->listeners('fake.event'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests event dispatching
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public function testDispatch() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-13 04:52:40 +00:00
|
|
|
$listener = $this->getMock('CakeEventTestListener');
|
|
|
|
$anotherListener = $this->getMock('CakeEventTestListener');
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
|
2011-12-13 04:52:40 +00:00
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
|
|
|
|
$listener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
|
$anotherListener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
|
$manager->dispatch($event);
|
|
|
|
}
|
|
|
|
|
2012-01-05 17:31:00 +00:00
|
|
|
/**
|
|
|
|
* Tests event dispatching using event key name
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDispatchWithKeyName() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
|
|
|
$listener = new CakeEventTestListener();
|
2012-01-05 17:31:00 +00:00
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$event = 'fake.event';
|
|
|
|
$manager->dispatch($event);
|
|
|
|
|
|
|
|
$expected = array('listenerFunction');
|
|
|
|
$this->assertEquals($expected, $listener->callStack);
|
|
|
|
}
|
|
|
|
|
2011-12-26 02:22:12 +00:00
|
|
|
/**
|
|
|
|
* Tests event dispatching with a return value
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2011-12-26 02:22:12 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchReturnValue() {
|
2012-10-16 01:35:57 +00:00
|
|
|
$this->skipIf(
|
2012-10-16 01:48:36 +00:00
|
|
|
version_compare(PHPUnit_Runner_Version::id(), '3.7', '<'),
|
2012-10-16 01:35:57 +00:00
|
|
|
'These tests fail in PHPUnit 3.6'
|
|
|
|
);
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-26 02:22:12 +00:00
|
|
|
$listener = $this->getMock('CakeEventTestListener');
|
|
|
|
$anotherListener = $this->getMock('CakeEventTestListener');
|
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
|
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
|
2012-09-10 01:36:33 +00:00
|
|
|
$listener->expects($this->at(0))->method('listenerFunction')
|
2012-10-16 01:35:57 +00:00
|
|
|
->with($event)
|
2011-12-26 02:22:12 +00:00
|
|
|
->will($this->returnValue('something special'));
|
2012-10-16 01:35:57 +00:00
|
|
|
$anotherListener->expects($this->at(0))
|
|
|
|
->method('listenerFunction')
|
|
|
|
->with($event);
|
2011-12-26 02:22:12 +00:00
|
|
|
$manager->dispatch($event);
|
|
|
|
$this->assertEquals('something special', $event->result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that returning false in a callback stops the event
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2011-12-26 02:22:12 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchFalseStopsEvent() {
|
2012-10-16 01:35:57 +00:00
|
|
|
$this->skipIf(
|
2012-10-16 01:38:10 +00:00
|
|
|
version_compare(PHPUnit_Runner_Version::id(), '3.7', '<'),
|
2012-10-16 01:35:57 +00:00
|
|
|
'These tests fail in PHPUnit 3.6'
|
|
|
|
);
|
|
|
|
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-26 02:22:12 +00:00
|
|
|
$listener = $this->getMock('CakeEventTestListener');
|
|
|
|
$anotherListener = $this->getMock('CakeEventTestListener');
|
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
|
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
|
|
|
|
$listener->expects($this->at(0))->method('listenerFunction')
|
2012-10-16 01:35:57 +00:00
|
|
|
->with($event)
|
2011-12-26 02:22:12 +00:00
|
|
|
->will($this->returnValue(false));
|
2012-10-16 01:35:57 +00:00
|
|
|
$anotherListener->expects($this->never())
|
|
|
|
->method('listenerFunction');
|
2011-12-26 02:22:12 +00:00
|
|
|
$manager->dispatch($event);
|
|
|
|
$this->assertTrue($event->isStopped());
|
|
|
|
}
|
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
/**
|
|
|
|
* Tests event dispatching using priorities
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchPrioritized() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
|
|
|
$listener = new CakeEventTestListener();
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
|
2011-12-13 04:52:40 +00:00
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
$manager->dispatch($event);
|
|
|
|
|
|
|
|
$expected = array('secondListenerFunction', 'listenerFunction');
|
|
|
|
$this->assertEquals($expected, $listener->callStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests event dispatching with passed params
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event $this, array('some' => 'data')
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchPassingParams() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-13 04:52:40 +00:00
|
|
|
$listener = $this->getMock('CakeEventTestListener');
|
|
|
|
$anotherListener = $this->getMock('CakeEventTestListener');
|
2011-12-13 05:25:28 +00:00
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$manager->attach(array($anotherListener, 'secondListenerFunction'), 'fake.event', array('passParams' => true));
|
2011-12-13 04:52:40 +00:00
|
|
|
$event = new CakeEvent('fake.event', $this, array('some' => 'data'));
|
|
|
|
|
|
|
|
$listener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
|
$anotherListener->expects($this->once())->method('secondListenerFunction')->with('data');
|
|
|
|
$manager->dispatch($event);
|
|
|
|
}
|
2011-12-13 05:43:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests subscribing a listener object and firing the events it subscribed to
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
|
|
|
* @triggers another.event $this, array('some' => 'data')
|
|
|
|
* @triggers multiple.handlers
|
2011-12-13 05:43:25 +00:00
|
|
|
*/
|
|
|
|
public function testAttachSubscriber() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2013-10-06 00:12:57 +00:00
|
|
|
$listener = $this->getMock('CustomTestEventListener', array('secondListenerFunction'));
|
2011-12-13 05:43:25 +00:00
|
|
|
$manager->attach($listener);
|
|
|
|
$event = new CakeEvent('fake.event');
|
2012-03-13 01:22:17 +00:00
|
|
|
|
2011-12-13 05:43:25 +00:00
|
|
|
$manager->dispatch($event);
|
|
|
|
|
|
|
|
$expected = array('listenerFunction');
|
|
|
|
$this->assertEquals($expected, $listener->callStack);
|
|
|
|
|
2011-12-25 01:07:22 +00:00
|
|
|
$listener->expects($this->at(0))->method('secondListenerFunction')->with('data');
|
2011-12-13 05:43:25 +00:00
|
|
|
$event = new CakeEvent('another.event', $this, array('some' => 'data'));
|
|
|
|
$manager->dispatch($event);
|
2011-12-25 01:07:22 +00:00
|
|
|
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2013-10-06 00:12:57 +00:00
|
|
|
$listener = $this->getMock('CustomTestEventListener', array('listenerFunction', 'thirdListenerFunction'));
|
2011-12-25 01:07:22 +00:00
|
|
|
$manager->attach($listener);
|
|
|
|
$event = new CakeEvent('multiple.handlers');
|
|
|
|
$listener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
|
$listener->expects($this->once())->method('thirdListenerFunction')->with($event);
|
|
|
|
$manager->dispatch($event);
|
2011-12-13 05:43:25 +00:00
|
|
|
}
|
2011-12-14 03:59:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests subscribing a listener object and firing the events it subscribed to
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDetachSubscriber() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2013-10-06 00:12:57 +00:00
|
|
|
$listener = $this->getMock('CustomTestEventListener', array('secondListenerFunction'));
|
2011-12-14 03:59:36 +00:00
|
|
|
$manager->attach($listener);
|
|
|
|
$expected = array(
|
|
|
|
array('callable' => array($listener, 'secondListenerFunction'), 'passParams' => true)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
|
$expected = array(
|
|
|
|
array('callable' => array($listener, 'listenerFunction'), 'passParams' => false)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
|
$manager->detach($listener);
|
|
|
|
$this->assertEquals(array(), $manager->listeners('fake.event'));
|
|
|
|
$this->assertEquals(array(), $manager->listeners('another.event'));
|
|
|
|
}
|
2011-12-14 05:05:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that it is possible to get/set the manager singleton
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGlobalDispatcherGetter() {
|
|
|
|
$this->assertInstanceOf('CakeEventManager', CakeEventManager::instance());
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-14 05:05:11 +00:00
|
|
|
|
|
|
|
CakeEventManager::instance($manager);
|
|
|
|
$this->assertSame($manager, CakeEventManager::instance());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that the global event manager gets the event too from any other manager
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2011-12-14 05:05:11 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchWithGlobal() {
|
2013-09-13 10:41:41 +00:00
|
|
|
$generalManager = $this->getMock('CakeEventManager', array('prioritisedListeners'));
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
2011-12-14 05:05:11 +00:00
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
CakeEventManager::instance($generalManager);
|
|
|
|
|
2013-09-13 10:41:41 +00:00
|
|
|
$generalManager->expects($this->once())->method('prioritisedListeners')->with('fake.event');
|
2011-12-14 05:05:11 +00:00
|
|
|
$manager->dispatch($event);
|
2013-11-11 03:16:09 +00:00
|
|
|
CakeEventManager::instance(new CakeEventManager());
|
2011-12-14 05:05:11 +00:00
|
|
|
}
|
2011-12-14 05:12:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that stopping an event will not notify the rest of the listeners
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2011-12-14 05:12:05 +00:00
|
|
|
*/
|
|
|
|
public function testStopPropagation() {
|
2013-09-16 07:38:54 +00:00
|
|
|
$generalManager = $this->getMock('CakeEventManager');
|
|
|
|
$manager = new CakeEventManager();
|
|
|
|
$listener = new CakeEventTestListener();
|
2013-09-13 10:41:41 +00:00
|
|
|
|
|
|
|
CakeEventManager::instance($generalManager);
|
|
|
|
$generalManager->expects($this->any())
|
|
|
|
->method('prioritisedListeners')
|
|
|
|
->with('fake.event')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
2011-12-14 05:12:05 +00:00
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$manager->attach(array($listener, 'stopListener'), 'fake.event', array('priority' => 8));
|
|
|
|
$manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
|
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
$manager->dispatch($event);
|
|
|
|
|
|
|
|
$expected = array('secondListenerFunction');
|
|
|
|
$this->assertEquals($expected, $listener->callStack);
|
2013-11-11 03:16:09 +00:00
|
|
|
CakeEventManager::instance(new CakeEventManager());
|
2011-12-14 05:12:05 +00:00
|
|
|
}
|
2013-09-13 10:41:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests event dispatching using priorities
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2013-09-13 10:41:41 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchPrioritizedWithGlobal() {
|
|
|
|
$generalManager = $this->getMock('CakeEventManager');
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
|
|
|
$listener = new CustomTestEventListener();
|
2013-09-13 10:41:41 +00:00
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
|
|
|
|
CakeEventManager::instance($generalManager);
|
|
|
|
$generalManager->expects($this->any())
|
|
|
|
->method('prioritisedListeners')
|
|
|
|
->with('fake.event')
|
|
|
|
->will($this->returnValue(
|
|
|
|
array(11 => array(
|
|
|
|
array('callable' => array($listener, 'secondListenerFunction'), 'passParams' => false)
|
|
|
|
))
|
|
|
|
));
|
|
|
|
|
|
|
|
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
|
$manager->attach(array($listener, 'thirdListenerFunction'), 'fake.event', array('priority' => 15));
|
|
|
|
|
|
|
|
$manager->dispatch($event);
|
|
|
|
|
|
|
|
$expected = array('listenerFunction', 'secondListenerFunction', 'thirdListenerFunction');
|
|
|
|
$this->assertEquals($expected, $listener->callStack);
|
2013-11-11 03:16:09 +00:00
|
|
|
CakeEventManager::instance(new CakeEventManager());
|
2013-09-13 10:41:41 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 21:10:47 +00:00
|
|
|
/**
|
2013-09-13 10:41:41 +00:00
|
|
|
* Tests event dispatching using priorities
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers fake.event
|
2013-09-13 10:41:41 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchGlobalBeforeLocal() {
|
|
|
|
$generalManager = $this->getMock('CakeEventManager');
|
2013-09-16 07:38:54 +00:00
|
|
|
$manager = new CakeEventManager();
|
|
|
|
$listener = new CustomTestEventListener();
|
2013-09-13 10:41:41 +00:00
|
|
|
$event = new CakeEvent('fake.event');
|
|
|
|
|
|
|
|
CakeEventManager::instance($generalManager);
|
|
|
|
$generalManager->expects($this->any())
|
|
|
|
->method('prioritisedListeners')
|
|
|
|
->with('fake.event')
|
|
|
|
->will($this->returnValue(
|
|
|
|
array(10 => array(
|
|
|
|
array('callable' => array($listener, 'listenerFunction'), 'passParams' => false)
|
|
|
|
))
|
|
|
|
));
|
|
|
|
|
|
|
|
$manager->attach(array($listener, 'secondListenerFunction'), 'fake.event');
|
|
|
|
|
|
|
|
$manager->dispatch($event);
|
|
|
|
|
|
|
|
$expected = array('listenerFunction', 'secondListenerFunction');
|
|
|
|
$this->assertEquals($expected, $listener->callStack);
|
2013-11-11 03:16:09 +00:00
|
|
|
CakeEventManager::instance(new CakeEventManager());
|
2013-09-13 10:41:41 +00:00
|
|
|
}
|
2013-09-16 07:38:54 +00:00
|
|
|
|
2014-01-31 09:43:42 +00:00
|
|
|
/**
|
|
|
|
* test callback
|
|
|
|
*/
|
|
|
|
public function onMyEvent($event) {
|
|
|
|
$event->data['callback'] = 'ok';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests events dispatched by a local manager can be handled by
|
|
|
|
* handler registered in the global event manager
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers my_event $manager
|
2014-01-31 09:43:42 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchLocalHandledByGlobal() {
|
|
|
|
$callback = array($this, 'onMyEvent');
|
|
|
|
CakeEventManager::instance()->attach($callback, 'my_event');
|
|
|
|
$manager = new CakeEventManager();
|
|
|
|
$event = new CakeEvent('my_event', $manager);
|
|
|
|
$manager->dispatch($event);
|
|
|
|
$this->assertEquals('ok', $event->data['callback']);
|
|
|
|
}
|
|
|
|
|
2014-02-27 02:06:32 +00:00
|
|
|
/**
|
2014-03-07 02:16:07 +00:00
|
|
|
* Test that events are dispatched properly when there are global and local
|
2014-02-27 02:06:32 +00:00
|
|
|
* listeners at the same priority.
|
|
|
|
*
|
|
|
|
* @return void
|
2014-11-30 21:50:11 +00:00
|
|
|
* @triggers fake.event $this
|
2014-02-27 02:06:32 +00:00
|
|
|
*/
|
|
|
|
public function testDispatchWithGlobalAndLocalEvents() {
|
|
|
|
$listener = new CustomTestEventListener();
|
|
|
|
CakeEventManager::instance()->attach($listener);
|
|
|
|
$listener2 = new CakeEventTestListener();
|
|
|
|
$manager = new CakeEventManager();
|
2014-03-07 02:16:07 +00:00
|
|
|
$manager->attach(array($listener2, 'listenerFunction'), 'fake.event');
|
2014-02-27 02:06:32 +00:00
|
|
|
|
|
|
|
$manager->dispatch(new CakeEvent('fake.event', $this));
|
|
|
|
$this->assertEquals(array('listenerFunction'), $listener->callStack);
|
|
|
|
$this->assertEquals(array('listenerFunction'), $listener2->callStack);
|
|
|
|
}
|
|
|
|
|
2012-03-13 01:22:17 +00:00
|
|
|
}
|