Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 254 lines (223 sloc) 7.795 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
<?php
/**
* ControllerTestCaseTest file
*
* Test Case for ControllerTestCase class
*
* PHP version 5
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
* @link http://cakephp.org CakePHP Project
* @package Cake.Test.Case.Event
* @since CakePHP v 2.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('CakeEvent', 'Event');
App::uses('CakeEventManager', 'Event');
App::uses('CakeEventListener', 'Event');

/**
* 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__;
}
}

/**
* Mock used for testing the subscriber objects
*
* @package Cake.Test.Case.Event
*/
class CustomTestEventListerner extends CakeEventTestListener implements CakeEventListener {

public function implementedEvents() {
return array(
'fake.event' => 'listenerFunction',
'another.event' => array('callable' => 'secondListenerFunction', 'passParams' => true)
);
}

/**
* Test function to be used in event dispatching
*
* @return void
*/
public function thirdListenerFunction() {
$this->callStack[] = __FUNCTION__;
}
}

/**
* 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() {
$manager = new CakeEventManager;
$manager->attach('fakeFunction', 'fake.event');
$expected = array(
array('callable' => 'fakeFunction', 'passParams' => false)
);
$this->assertEquals($expected, $manager->listeners('fake.event'));

$manager->attach('fakeFunction2', 'fake.event');
$expected[] = array('callable' => 'fakeFunction2', 'passParams' => false);
$this->assertEquals($expected, $manager->listeners('fake.event'));

$manager->attach('inQ5', 'fake.event', array('priority' => 5));
$manager->attach('inQ1', 'fake.event', array('priority' => 1));
$manager->attach('otherInQ5', 'fake.event', array('priority' => 5));

$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() {
$manager = new CakeEventManager;
$manager->attach('fakeFunction', 'fake.event');
$manager->attach('fakeFunction2', 'another.event');
$manager->attach('fakeFunction3', 'another.event', array('priority' => 1, 'passParams' => true));
$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() {
$manager = new CakeEventManager;
$manager->attach(array('AClass', 'aMethod'), 'fake.event');
$manager->attach(array('AClass', 'anotherMethod'), 'another.event');
$manager->attach('fakeFunction', 'another.event', array('priority' => 1));

$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() {
$manager = new CakeEventManager;
$manager->attach(array('AClass', 'aMethod'), 'fake.event');
$manager->attach(array('AClass', 'aMethod'), 'another.event');
$manager->attach('fakeFunction', 'another.event', array('priority' => 1));

$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
*/
public function testDispatch() {
$manager = new CakeEventManager;
$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->once())->method('listenerFunction')->with($event);
$anotherListener->expects($this->once())->method('listenerFunction')->with($event);
$manager->dispatch($event);
}

/**
* Tests event dispatching using priorities
*
* @return void
*/
public function testDispatchPrioritized() {
$manager = new CakeEventManager;
$listener = new CakeEventTestListener;
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
$manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
$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
*/
public function testDispatchPassingParams() {
$manager = new CakeEventManager;
$listener = $this->getMock('CakeEventTestListener');
$anotherListener = $this->getMock('CakeEventTestListener');
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
$manager->attach(array($anotherListener, 'secondListenerFunction'), 'fake.event', array('passParams' => true));
$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);
}

/**
* Tests subscribing a listener object and firing the events it subscribed to
*
* @return void
*/
public function testAttachSubscriber() {
$manager = new CakeEventManager;
$listener = $this->getMock('CustomTestEventListerner', array('secondListenerFunction'));
$manager->attach($listener);
$event = new CakeEvent('fake.event');

$manager->dispatch($event);

$expected = array('listenerFunction');
$this->assertEquals($expected, $listener->callStack);

$listener->expects($this->once())->method('secondListenerFunction')->with('data');
$event = new CakeEvent('another.event', $this, array('some' => 'data'));
$manager->dispatch($event);
}
}
Something went wrong with that request. Please try again.