Changeset 1035
- Timestamp:
- 11/12/07 23:30:26 (9 months ago)
- Files:
-
- trunk/config/xml/events.xml (added)
- trunk/lib/xjconf.star (modified) (previous)
- trunk/src/main/php/net/stubbles/events/stubCallbackListener.php (modified) (7 diffs)
- trunk/src/main/php/net/stubbles/events/stubEventDispatcher.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/events/stubEventsInitializer.php (added)
- trunk/src/main/php/net/stubbles/events/stubEventsXJConfInitializer.php (added)
- trunk/src/main/php/net/stubbles/events/stubLazyEventListener.php (modified) (2 diffs)
- trunk/src/main/resources/xjconf/events.xml (added)
- trunk/src/test/php/net/stubbles/events/stubCallbackListenerTestCase.php (modified) (5 diffs)
- trunk/src/test/php/net/stubbles/events/stubEventDispatcherTestCase.php (modified) (21 diffs)
- trunk/src/test/php/net/stubbles/integration/EventsTestCase.php (added)
- trunk/src/test/runIntegration.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/events/stubCallbackListener.php
r1024 r1035 9 9 stubClassLoader::load('net.stubbles.events.stubCallbackException', 10 10 'net.stubbles.events.stubEvent', 11 'net.stubbles.events.stubEventListener' 11 'net.stubbles.events.stubEventListener', 12 'net.stubbles.lang.exceptions.stubIllegalArgumentException' 12 13 ); 13 14 /** … … 21 22 * @subpackage events 22 23 */ 23 class stubCallbackListener extends stub BaseObject implements stubEventListener24 class stubCallbackListener extends stubSerializableObject implements stubEventListener 24 25 { 25 26 /** … … 66 67 * @param string $method method of class to callback when listener is notified 67 68 * @param bool $autoRemove optional whether listener should be removed if it has handled one event or not 68 * @throws stubCallbackException69 69 */ 70 70 public function __construct($instance, $method, $autoRemove = false) 71 71 { 72 if (is_string($instance) == false && is_object($instance)== false) {73 throw new stub CallbackException('Argument $instance must be a string or an instance of an object.');74 } 75 76 if (is_string($method) == false) {77 throw new stub CallbackException('Argument $method must be a string.');78 } 79 80 if (is_object($instance) == true) {72 if (is_string($instance) === false && is_object($instance) === false) { 73 throw new stubIllegalArgumentException('Argument $instance must be a string or an instance of an object.'); 74 } 75 76 if (is_string($method) === false) { 77 throw new stubIllegalArgumentException('Argument $method must be a string.'); 78 } 79 80 if (is_object($instance) === true) { 81 81 $this->instance = $instance; 82 82 $this->class = get_class($instance); 83 83 } else { 84 if (class_exists($instance, false) === false) {85 throw new stubCallbackException('Class ' . $instance . ' does not exist.');86 }87 88 84 $this->class = $instance; 89 85 } … … 186 182 187 183 /** 184 * returns name of class to call back 185 * 186 * @return string 187 */ 188 public function getCallbackClassName() 189 { 190 return $this->class; 191 } 192 193 /** 194 * returns name of the method to call back 195 * 196 * @return string 197 */ 198 public function getCallbackMethodName() 199 { 200 return $this->method; 201 } 202 203 /** 188 204 * return whether the callback was notified or not 189 205 * … … 192 208 public function isNotified() 193 209 { 194 return ((null == $this->event) ? (false) : (true));210 return ((null === $this->event) ? (false) : (true)); 195 211 } 196 212 … … 211 227 } 212 228 213 $refClass = new ReflectionClass($this->class); 229 if (class_exists(stubClassLoader::getNonQualifiedClassName($this->class), false) === false) { 230 stubClassLoader::load($this->class); 231 } 232 233 $refClass = new ReflectionClass(stubClassLoader::getNonQualifiedClassName($this->class)); 214 234 215 235 try { … … 246 266 } 247 267 } 268 269 /** 270 * template method to hook into __sleep() 271 * 272 * @return array<string> list of property names that should not be serialized 273 */ 274 protected function __doSleep() 275 { 276 return array('event', 'instance', 'refMethod'); 277 } 278 248 279 } 249 280 ?> trunk/src/main/php/net/stubbles/events/stubEventDispatcher.php
r984 r1035 83 83 return self::$instances[$name]; 84 84 } 85 85 86 86 /** 87 87 * destroy an event dispatcher … … 103 103 } 104 104 } 105 105 106 /** 107 * returns a list of all instance names 108 * 109 * @return array<string> 110 */ 111 public static function getInstanceNames() 112 { 113 return array_keys(self::$instances); 114 } 115 106 116 /** 107 117 * nests this as a dispatcher of the parent dispatcher … … 113 123 $this->parentDispatcher = $eventDispatcher; 114 124 } 115 125 116 126 /** 117 127 * removes the parent dispatcher … … 121 131 $this->parentDispatcher = null; 122 132 } 123 133 124 134 /** 125 135 * checks whether this dispatcher has a parent dispatcher … … 131 141 return (null !== $this->parentDispatcher); 132 142 } 133 143 134 144 /** 135 145 * returns the parent dispatcher … … 202 212 203 213 return isset($this->listeners[$eventName][get_class($eventListener)]); 214 } 215 216 /** 217 * returns the list of event listeners registered for given event 218 * 219 * @param string $eventName 220 * @return array<stubEventListener> 221 */ 222 public function getListenerForEvent($eventName) 223 { 224 if (isset($this->listeners[$eventName]) === true) { 225 return $this->listeners[$eventName]; 226 } 227 228 return array(); 229 } 230 231 /** 232 * returns a list of all event names known to the dispatcher 233 * 234 * @return array<string> 235 */ 236 public function getEventNames() 237 { 238 return array_keys($this->listeners); 204 239 } 205 240 trunk/src/main/php/net/stubbles/events/stubLazyEventListener.php
r887 r1035 14 14 * @subpackage events 15 15 */ 16 class stubLazyEventListener extends stub BaseObject implements stubEventListener16 class stubLazyEventListener extends stubSerializableObject implements stubEventListener 17 17 { 18 18 /** … … 31 31 { 32 32 $this->fqClassName = $fqClassName; 33 } 34 35 /** 36 * returns the class name of the lazy loaded class 37 * 38 * @return string 39 */ 40 public function getLazyClassName() 41 { 42 return $this->fqClassName; 33 43 } 34 44 trunk/src/test/php/net/stubbles/events/stubCallbackListenerTestCase.php
r98 r1035 34 34 $this->assertEqual($instance->getInvocations(), 1); 35 35 $this->assertTrue($callbacklistener->isNotified()); 36 37 $this->expectException('stubCallbackException');38 $callbacklistener = new stubCallbackListener('NonExistingClass', 'nonExistingMethod');39 36 } 40 37 … … 105 102 public function testNonExistingCallbackClass() 106 103 { 107 $this->expectException('stubCallbackException');108 104 $callbacklistener = new stubCallbackListener('NotExisting', 'notExisting'); 105 $event = new stubEvent('eventName'); 106 $this->expectException('stubClassNotFoundException'); 107 $callbacklistener->handleEvent($event); 109 108 } 110 109 … … 112 111 * assure that a failed instantiation of the callback class throws a stubCallbackException 113 112 */ 114 public function testFailingIn stantiation()113 public function testFailingInvocationWrongMethod() 115 114 { 116 115 $callbacklistener = new stubCallbackListener('TestStubCallback2', 'notExisting'); … … 136 135 public function testIllegalConstructorArgumentInstance() 137 136 { 138 $this->expectException('stub CallbackException');137 $this->expectException('stubIllegalArgumentException'); 139 138 $callbacklistener = new stubCallbackListener(6100, 'pimp'); 140 139 } … … 145 144 public function testIllegalConstructorArgumentMethod() 146 145 { 147 $this->expectException('stub CallbackException');146 $this->expectException('stubIllegalArgumentException'); 148 147 $callbacklistener = new stubCallbackListener('Binford', 6100); 149 148 } trunk/src/test/php/net/stubbles/events/stubEventDispatcherTestCase.php
r176 r1035 36 36 */ 37 37 private $eventDispatcher; 38 38 39 39 /** 40 40 * set up the test environment … … 48 48 $this->eventDispatcher->register($this->onAfter, 'onAfterTestEvent'); 49 49 } 50 50 51 51 /** 52 52 * restore environment … … 56 56 stubEventDispatcher::destroyInstance('__test'); 57 57 } 58 58 59 /** 60 * test that all instance names are returned 61 */ 62 public function testInstanceNames() 63 { 64 $this->assertEqual(stubEventDispatcher::getInstanceNames(), array('__test')); 65 } 66 67 /** 68 * test that all event names are returned 69 */ 70 public function testRegisteredEventNames() 71 { 72 $this->assertEqual($this->eventDispatcher->getEventNames(), array('onBeforeTestEvent', 73 'onAfterTestEvent' 74 ) 75 ); 76 } 59 77 /** 60 78 * assure that registered listeners are recognized … … 68 86 $this->assertFalse($this->eventDispatcher->has($this->onAfter, 'onEvent')); 69 87 } 70 88 71 89 /** 72 90 * assure that event and data are in correct places … … 83 101 $this->assertNull($this->onAfter->getEvent()); 84 102 } 85 103 86 104 /** 87 105 * assure that event and data are in correct places … … 115 133 $this->assertReference($testEvent, $event2); 116 134 } 117 135 118 136 /** 119 137 * assure that event injected from outside leads to same results … … 128 146 $this->assertEqual($event2->getInfo(), array(1)); 129 147 } 130 148 131 149 /** 132 150 * assure that a canceled event is not propagated to the following listeners … … 143 161 $this->assertReference($event2, $testEvent); 144 162 } 145 163 146 164 /** 147 165 * assure that removed event listeners are recognized by has() and … … 157 175 $this->eventDispatcher->trigger('onRemoveTest'); 158 176 } 159 177 160 178 /** 161 179 * assure that event listeners registered with autoremove are only notified once … … 170 188 $this->eventDispatcher->trigger('onAutoRemoveTest'); 171 189 } 172 190 173 191 /** 174 192 * assure that queue methods work correct … … 186 204 $this->assertEqual($this->eventDispatcher->countQueuedEvents(), 0); 187 205 } 188 206 189 207 /** 190 208 * assert that a queued event will be propagated to an event listener … … 201 219 $this->assertEqual($this->eventDispatcher->countQueuedEvents(), 1); 202 220 } 203 221 204 222 /** 205 223 * assert that the queued event will not be propagated to … … 213 231 $this->assertNull($listener->getEvent()); 214 232 } 215 233 216 234 /** 217 235 * we register a listener with autoRemove which means … … 229 247 $this->assertFalse($this->eventDispatcher->has($executeOnceListener, 'onQueuedEvent')); 230 248 } 231 249 232 250 /** 233 251 * assert that a cancelled event will be removed from the queue … … 247 265 $this->assertNull($anotherListener->getEvent()); 248 266 } 249 267 250 268 /** 251 269 * assert that a canceled event will not be added to the queue … … 261 279 $this->assertEqual($this->eventDispatcher->countQueuedEvents(), 0); 262 280 } 263 281 264 282 /** 265 283 * assert that bubbling works as expected … … 268 286 { 269 287 $nestedDispatcher = stubEventDispatcher::getInstance('__nested'); 288 $this->assertEqual(stubEventDispatcher::getInstanceNames(), array('__test', '__nested')); 270 289 $this->assertFalse($nestedDispatcher->hasParentDispatcher()); 271 290 $nestedDispatcher->setParentDispatcher($this->eventDispatcher); … … 279 298 stubEventDispatcher::destroyInstance('__nested'); 280 299 } 281 300 282 301 /** 283 302 * assert that bubbling works as expected … … 294 313 stubEventDispatcher::destroyInstance('__nested'); 295 314 } 296 315 297 316 /** 298 317 * assert that bubbling works as expected … … 310 329 stubEventDispatcher::destroyInstance('__nested'); 311 330 } 312 331 313 332 /** 314 333 * assert that bubbling works as expected … … 325 344 stubEventDispatcher::destroyInstance('__nested'); 326 345 } 327 346 328 347 public function testBubblingWithDestroyedInstance() 329 348 { trunk/src/test/runIntegration.php
r960 r1035 35 35 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/DatabaseTestCase.php'); 36 36 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/EncoderXJConfTestCase.php'); 37 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/EventsTestCase.php'); 37 38 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/InterceptorTestCase.php'); 38 39 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/LoggerTestCase.php');
