Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tests

  • Loading branch information...
commit a6601e786038261c7fb57543bb2287e68eb7b3dd 1 parent cbb2043
Thomas Gossmann authored October 08, 2011
4  .gitignore
... ...
@@ -1 +1,3 @@
1  
-/build
  1
+.project
  2
+.buildpath
  3
+.settings/
18  generator/templates/ObjectListener.php
@@ -148,12 +148,22 @@ private function notifyListener($event) {
148 148
 
149 149
 			if ($l->getOn()) {
150 150
 				$cls = $l->getOn();
  151
+				$c = new $cls();
  152
+				
151 153
 				$method = $l->getCallback();
152  
-				if ($method == '') {
153  
-					$method = 'handleEvent';
  154
+				if (!method_exists($c, $method)) {
  155
+					$method = sprintf('on%s%s', 
  156
+						strtoupper(substr($e['event'], 0, 1)),
  157
+						substr($e['event'], 1));
  158
+					
  159
+					if (!method_exists($c, $method)) {
  160
+						$method = 'handleEvent';
  161
+					}
  162
+				}
  163
+				
  164
+				if (method_exists($c, $method)) {
  165
+					$c->$method($e);
154 166
 				}
155  
-				$c = new $cls();
156  
-				$c->$method($e);
157 167
 			} else {
158 168
 				$cb = $l->getCallback();
159 169
 				$cb($e);
21  runtime/RecordListener.php
@@ -9,14 +9,15 @@ class RecordListener implements ListenerConfigInterface
9 9
 	public function __construct($cfg = array()) 
10 10
 	{
11 11
 		$this->cfg = array(
12  
-			'on' => 'RecordListener',
13  
-			'params' => $cfg,
  12
+			'on' => 'RecordListener'
14 13
 		);
15 14
 		
16 15
 		if (array_key_exists('event', $cfg)) {
17 16
 			$this->cfg['event'] = $cfg['event'];
18 17
 			unset($cfg['event']);
19 18
 		}
  19
+		
  20
+		$this->cfg['params'] = $cfg;
20 21
 	}
21 22
 
22 23
 	public function getListenerConfig() 
@@ -29,19 +30,27 @@ public function handleEvent($e)
29 30
 		$target = array_key_exists('target', $e['params']) ? $e['params']['target'] : null;
30 31
 		$find = array_key_exists('find', $e['params']) ? $e['params']['find'] : null;
31 32
 		$param = array_key_exists('param', $e['params']) ? $e['params']['param'] : null;
  33
+		$method = '';
32 34
 		
33 35
 		if (array_key_exists('method', $e['params'])) {
34 36
 			$method = $e['params']['method'];
35  
-		} else {
36  
-			$event = $e['event'];
37  
-			$event[0] = strtoupper($event);
38  
-			$method = 'on'.$event;
39 37
 		}
40 38
 		
41 39
 		if ($target && $find) {
42 40
 			$class = $target.'Query';
43 41
 			$o = $class::create()->$find($param);
44 42
 			
  43
+			// some testing for tests
  44
+// 			$o = $class::create();
  45
+// 			echo $o->getModelName();
  46
+// 			$o = $o->$find($param);
  47
+
  48
+			if (!method_exists($o, $method)) {
  49
+				$event = $e['event'];
  50
+				$event[0] = strtoupper($event);
  51
+				$method = 'on'.$event;
  52
+			}
  53
+
45 54
 			if (method_exists($o, $method)) {
46 55
 				$o->$method($e);
47 56
 			} else if (method_exists($o, 'handleEvent')) {
121  test/listener-behavior/ClassListenerTest.php
... ...
@@ -0,0 +1,121 @@
  1
+<?php
  2
+require_once 'helpers/listenerTests.php';
  3
+
  4
+class ClassListenerTest extends ListenerTests
  5
+{
  6
+	// with listeners added during runtime
  7
+	public function testHandleEvent()
  8
+	{
  9
+		Table::addGlobalListener(array('on' => 'MyBlankListener'));
  10
+		
  11
+		$this->asserts = array(
  12
+			array('event' => 'preSave', 'method' => 'handleEvent'),
  13
+			array('event' => 'preInsert', 'method' => 'handleEvent'),
  14
+			array('event' => 'postInsert', 'method' => 'handleEvent'),
  15
+			array('event' => 'postSave', 'method' => 'handleEvent'),
  16
+		);
  17
+
  18
+		$t = new Table();
  19
+		$t->setName('test1');
  20
+		$t->save();
  21
+		
  22
+		$this->asserts = array(
  23
+			array('event' => 'preSave', 'method' => 'handleEvent'),
  24
+			array('event' => 'preUpdate', 'method' => 'handleEvent'),
  25
+			array('event' => 'postUpdate', 'method' => 'handleEvent'),
  26
+			array('event' => 'postSave', 'method' => 'handleEvent'),
  27
+		);
  28
+		
  29
+		$t->setName('test2');
  30
+		$t->save();
  31
+		
  32
+		$this->asserts = array(
  33
+			array('event' => 'preDelete', 'method' => 'handleEvent'),
  34
+			array('event' => 'postDelete', 'method' => 'handleEvent'),
  35
+		);
  36
+		
  37
+		$t->delete();
  38
+		
  39
+		Table::removeGlobalListener(array('on' => 'MyBlankListener'));
  40
+	}
  41
+	
  42
+	public function testEventHandle()
  43
+	{
  44
+		Table::addGlobalListener(array('on' => 'MyListener'));
  45
+	
  46
+		$this->asserts = array(
  47
+		array('event' => 'preSave', 'method' => 'handleEvent'),
  48
+		array('event' => 'preInsert', 'method' => 'handleEvent'),
  49
+		array('event' => 'postInsert', 'method' => 'handleEvent'),
  50
+		array('event' => 'postSave', 'method' => 'onPostSave'),
  51
+		);
  52
+	
  53
+		$t = new Table();
  54
+		$t->setName('test1');
  55
+		$t->save();
  56
+	
  57
+		$this->asserts = array(
  58
+		array('event' => 'preSave', 'method' => 'handleEvent'),
  59
+		array('event' => 'preUpdate', 'method' => 'handleEvent'),
  60
+		array('event' => 'postUpdate', 'method' => 'handleEvent'),
  61
+		array('event' => 'postSave', 'method' => 'onPostSave'),
  62
+		);
  63
+	
  64
+		$t->setName('test2');
  65
+		$t->save();
  66
+	
  67
+		$this->asserts = array(
  68
+		array('event' => 'preDelete', 'method' => 'handleEvent'),
  69
+		array('event' => 'postDelete', 'method' => 'handleEvent'),
  70
+		);
  71
+	
  72
+		$t->delete();
  73
+	
  74
+		Table::removeGlobalListener(array('on' => 'MyListener'));
  75
+	}
  76
+	
  77
+	public function testFallbacks()
  78
+	{
  79
+		// fall back to handle event
  80
+		Table::addGlobalListener(array('on' => 'MyListener', 'event' => 'preSave', 'callback' => 'notPresent'));
  81
+		
  82
+		$this->asserts = array(
  83
+		array('event' => 'preSave', 'method' => 'handleEvent'),
  84
+		);
  85
+		
  86
+		$t = new Table();
  87
+		$t->setName('test1');
  88
+		$t->save();
  89
+		
  90
+		Table::removeGlobalListener(array('on' => 'MyListener', 'event' => 'preSave', 'callback' => 'notPresent'));
  91
+		
  92
+		
  93
+		// fall back to onEvent
  94
+		Table::addGlobalListener(array('on' => 'MyListener', 'event' => 'postSave', 'callback' => 'notPresent'));
  95
+		
  96
+		$this->asserts = array(
  97
+		array('event' => 'postSave', 'method' => 'onPostSave'),
  98
+		);
  99
+		
  100
+		$t = new Table();
  101
+		$t->setName('test1');
  102
+		$t->save();
  103
+		
  104
+		Table::removeGlobalListener(array('on' => 'MyListener', 'event' => 'postSave', 'callback' => 'notPresent'));
  105
+		
  106
+		
  107
+		// no fallback
  108
+		Table::addGlobalListener(array('on' => 'MyListener', 'event' => 'postSave', 'callback' => 'listener'));
  109
+		
  110
+		$this->asserts = array(
  111
+		array('event' => 'postSave', 'method' => 'listener'),
  112
+		);
  113
+		
  114
+		$t = new Table();
  115
+		$t->setName('test1');
  116
+		$t->save();
  117
+		
  118
+		Table::removeGlobalListener(array('on' => 'MyListener', 'event' => 'postSave', 'callback' => 'listener'));
  119
+	}
  120
+}
  121
+?>
193  test/listener-behavior/FunctionListenerTest.php
... ...
@@ -0,0 +1,193 @@
  1
+<?php
  2
+require_once 'helpers/listenerTests.php';
  3
+
  4
+class FunctionListenerTest extends ListenerTests
  5
+{	
  6
+	// with listeners added during runtime
  7
+	public function testGlobalAllEvents()
  8
+	{
  9
+		Table::addGlobalListener('listener');
  10
+		
  11
+		$this->asserts = array(
  12
+			array('event' => 'preSave'),
  13
+			array('event' => 'preInsert'),
  14
+			array('event' => 'postInsert'),
  15
+			array('event' => 'postSave'),
  16
+		);
  17
+
  18
+		$t = new Table();
  19
+		$t->setName('test1');
  20
+		$t->save();
  21
+		
  22
+		$this->asserts = array(
  23
+			array('event' => 'preSave'),
  24
+			array('event' => 'preUpdate'),
  25
+			array('event' => 'postUpdate'),
  26
+			array('event' => 'postSave'),
  27
+		);
  28
+		
  29
+		$t->setName('test2');
  30
+		$t->save();
  31
+		
  32
+		$this->asserts = array(
  33
+			array('event' => 'preDelete'),
  34
+			array('event' => 'postDelete'),
  35
+		);
  36
+		
  37
+		$t->delete();
  38
+		
  39
+		Table::removeGlobalListener('listener');
  40
+	}
  41
+	
  42
+	public function testGlobalNoEvents()
  43
+	{
  44
+		Table::addGlobalListener('listener');
  45
+		Table::removeGlobalListener('listener');
  46
+		
  47
+		$this->calls = 0;
  48
+		$t = new Table();
  49
+		$t->setName('test1');
  50
+		$t->save();
  51
+		
  52
+		$this->assertEquals(0, $this->calls);
  53
+		
  54
+		$this->calls = 0;
  55
+		$t->setName('test2');
  56
+		$t->save();
  57
+		
  58
+		$this->assertEquals(0, $this->calls);
  59
+		
  60
+		$this->calls = 0;
  61
+		$t->delete();
  62
+		$this->assertEquals(0, $this->calls);
  63
+	}
  64
+	
  65
+	public function testSingleEvents()
  66
+	{
  67
+		// preSave - preDelete
  68
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'preSave'));
  69
+		
  70
+		$this->asserts = array(
  71
+		array('event' => 'preSave'),
  72
+		);
  73
+		
  74
+		$t = new Table();
  75
+		$t->setName('test1');
  76
+		$t->save();
  77
+		
  78
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'preSave'));
  79
+		
  80
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'preDelete'));
  81
+		
  82
+		$this->asserts = array(
  83
+		array('event' => 'preDelete'),
  84
+		);
  85
+		$t->delete();
  86
+		
  87
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'preDelete'));
  88
+		
  89
+		// postSave - postDelete
  90
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'postSave'));
  91
+		
  92
+		$this->asserts = array(
  93
+		array('event' => 'postSave'),
  94
+		);
  95
+		
  96
+		$t = new Table();
  97
+		$t->setName('test1');
  98
+		$t->save();
  99
+		
  100
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'postSave'));
  101
+		
  102
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'postDelete'));
  103
+		
  104
+		$this->asserts = array(
  105
+		array('event' => 'postDelete'),
  106
+		);
  107
+		$t->delete();
  108
+		
  109
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'postDelete'));
  110
+		
  111
+		
  112
+		// preInsert - preUpdate
  113
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'preInsert'));
  114
+		
  115
+		$this->asserts = array(
  116
+		array('event' => 'preInsert'),
  117
+		);
  118
+		
  119
+		$t = new Table();
  120
+		$t->setName('test1');
  121
+		$t->save();
  122
+		
  123
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'preInsert'));
  124
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'preUpdate'));
  125
+		
  126
+		$this->asserts = array(
  127
+		array('event' => 'preUpdate'),
  128
+		);
  129
+		$t->delete();
  130
+		
  131
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'preUpdate'));
  132
+		
  133
+		// postInsert - postUpdate
  134
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'postInsert'));
  135
+		
  136
+		$this->asserts = array(
  137
+		array('event' => 'postInsert'),
  138
+		);
  139
+		
  140
+		$t = new Table();
  141
+		$t->setName('test1');
  142
+		$t->save();
  143
+		
  144
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'postInsert'));
  145
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'postUpdate'));
  146
+		
  147
+		$this->asserts = array(
  148
+		array('event' => 'postUpdate'),
  149
+		);
  150
+		$t->delete();
  151
+		
  152
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'postUpdate'));
  153
+	}
  154
+	
  155
+	public function testParams()
  156
+	{
  157
+		Table::addGlobalListener(array('callback' => 'listener', 'event' => 'preSave', 'params' => array(
  158
+			'foo' => 'bar',
  159
+			'baz' => 0
  160
+		)));
  161
+		
  162
+		$this->asserts = array(
  163
+		array('event' => 'preSave', 'params' => array(
  164
+			'foo' => 'bar',
  165
+			'baz' => 0
  166
+		)));
  167
+		
  168
+		$t = new Table();
  169
+		$t->setName('test1');
  170
+		$t->save();
  171
+		
  172
+		Table::removeGlobalListener(array('callback' => 'listener', 'event' => 'preSave', 'params' => array(
  173
+			'foo' => 'bar',
  174
+			'baz' => 0
  175
+		)));
  176
+	}
  177
+	
  178
+	public function testLocalListeners()
  179
+	{
  180
+		// preSave - preDelete
  181
+		$this->asserts = array(
  182
+		array('event' => 'preSave'),
  183
+		);
  184
+		
  185
+		$t = new Table();
  186
+		$t->setName('test1');
  187
+		$t->addListener(array('callback' => 'listener', 'event' => 'preSave'));
  188
+		$t->save();
  189
+		
  190
+		$t->removeListener(array('callback' => 'listener', 'event' => 'preSave'));
  191
+	}
  192
+}
  193
+?>
4  test/listener-behavior/ListenerInstallationTests.php → test/listener-behavior/ListenerInstallationTest.php
@@ -160,8 +160,8 @@ public function testLocalListener()
160 160
 		$this->assertEquals($t->getId(), $l->getRefId());
161 161
 
162 162
 		// remove
163  
-// 		$t->removeListener('listener');
164  
-// 		$this->assertEquals(0, ListenerQuery::create()->count());
  163
+		$t->removeListener('listener');
  164
+		$this->assertEquals(0, ListenerQuery::create()->count());
165 165
 	}
166 166
 }
167 167
 ?>
41  test/listener-behavior/RecordListenerTest.php
... ...
@@ -0,0 +1,41 @@
  1
+<?php
  2
+require_once 'helpers/listenerTests.php';
  3
+require_once '../../runtime/RecordListener.php';
  4
+
  5
+class RecordListenerTest extends ListenerTests
  6
+{
  7
+	// with listeners added during runtime
  8
+	public function testHandleEvent()
  9
+	{
  10
+		$t = new Table();
  11
+		$t->setName('Unchanged Record');
  12
+		$t->save();
  13
+		
  14
+		$o = new OtherTable();
  15
+		$o->setOther($t->getId());
  16
+		$o->save();
  17
+		
  18
+		$t->addListener(new RecordListener(array(
  19
+			'event' => 'postUpdate',
  20
+			'target' => 'OtherTableListener',
  21
+			'find' => 'findOneByOther',
  22
+			'param' => $t->getId()
  23
+		)));
  24
+
  25
+		// this won't assert right now
  26
+		$this->asserts = array(
  27
+			array('event' => 'postUpdate', 'method' => 'handleEvent')
  28
+		);
  29
+		
  30
+		$t->setName('Changed Record');
  31
+		$t->save();
  32
+		
  33
+		$t->removeListener(new RecordListener(array(
  34
+			'event' => 'postUpdate',
  35
+			'target' => 'OtherTableListener',
  36
+			'find' => 'findOneByOther',
  37
+			'param' => $t->getId()
  38
+		)));
  39
+	}
  40
+}
  41
+?>
34  test/listener-behavior/helpers/ListenerTests.php
... ...
@@ -0,0 +1,34 @@
  1
+<?php
  2
+require_once 'ListenerBehaviorTestBase.php';
  3
+require_once 'listeners.php';
  4
+
  5
+class ListenerTests extends ListenerBehaviorTestBase
  6
+{
  7
+	protected $asserts = array();
  8
+	protected $calls = 0;
  9
+	protected $boink = null;
  10
+	
  11
+	public function setUp()
  12
+	{
  13
+		global $cb; // woudln't work anywhere else
  14
+		$cb = $this;
  15
+
  16
+		$this->asserts = array();
  17
+		ListenerQuery::create()->deleteAll();
  18
+		TableQuery::create()->deleteAll();
  19
+	}
  20
+	
  21
+	// helpers
  22
+	public function receiveCallback($e)
  23
+	{
  24
+		$asserts = array_shift($this->asserts); 
  25
+		
  26
+		$this->assertNotNull($asserts);
  27
+		
  28
+		foreach ($asserts as $k => $v) {
  29
+			$this->assertEquals($v, $e[$k]);
  30
+		}
  31
+		$this->calls++;
  32
+	}
  33
+}
  34
+?>
155  test/listener-behavior/helpers/listeners.php
... ...
@@ -0,0 +1,155 @@
  1
+<?php
  2
+$cb = null;
  3
+
  4
+function listener($e) 
  5
+{
  6
+	global $cb;
  7
+	$cb->receiveCallback($e);
  8
+}
  9
+
  10
+class TestListener
  11
+{
  12
+	protected function callback($e, $method)
  13
+	{
  14
+		global $cb;
  15
+		$e['method'] = $method;
  16
+		$cb->receiveCallback($e);
  17
+	}
  18
+}
  19
+
  20
+class MyBlankListener extends TestListener
  21
+{
  22
+	public function handleEvent($e)
  23
+	{
  24
+		$this->callback($e, 'handleEvent');
  25
+	}
  26
+}
  27
+
  28
+class MyListener extends TestListener 
  29
+{
  30
+	public function handleEvent($e) 
  31
+	{
  32
+		$this->callback($e, 'handleEvent');
  33
+	}
  34
+	
  35
+	public function onPostSave($e) 
  36
+	{
  37
+		$this->callback($e, 'onPostSave');
  38
+	}
  39
+	
  40
+	public function listener($e) 
  41
+	{
  42
+		$this->callback($e, 'listener');
  43
+	}
  44
+}
  45
+
  46
+/* Workaround to test RecordListeners */
  47
+class OtherTableListenerPeer extends OtherTablePeer
  48
+{
  49
+	const CLASS_DEFAULT = 'listener-behavior.OtherTableListener';
  50
+	
  51
+	/** the related Propel class for this table */
  52
+	const OM_CLASS = 'OtherTableListener';
  53
+	
  54
+	public static function getOMClass($withPrefix = true)
  55
+	{
  56
+		return $withPrefix ? OtherTableListenerPeer::CLASS_DEFAULT : OtherTableListenerPeer::OM_CLASS;
  57
+	}
  58
+	
  59
+	public static function populateObjects(PDOStatement $stmt)
  60
+	{
  61
+		$results = array();
  62
+	
  63
+		// set the class once to avoid overhead in the loop
  64
+		$cls = OtherTableListenerPeer::getOMClass(false);
  65
+		// populate the object(s)
  66
+		while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
  67
+			$key = OtherTablePeer::getPrimaryKeyHashFromRow($row, 0);
  68
+			if (null !== ($obj = OtherTablePeer::getInstanceFromPool($key))) {
  69
+				// We no longer rehydrate the object, since this can cause data loss.
  70
+				// See http://www.propelorm.org/ticket/509
  71
+				// $obj->hydrate($row, 0, true); // rehydrate
  72
+				$results[] = $obj;
  73
+			} else {
  74
+				$obj = new $cls();
  75
+				$obj->hydrate($row);
  76
+				$results[] = $obj;
  77
+				OtherTablePeer::addInstanceToPool($obj, $key);
  78
+			} // if key exists
  79
+		}
  80
+		$stmt->closeCursor();
  81
+		return $results;
  82
+	}
  83
+	
  84
+	public static function populateObject($row, $startcol = 0)
  85
+	{
  86
+		$key = OtherTablePeer::getPrimaryKeyHashFromRow($row, $startcol);
  87
+		if (null !== ($obj = OtherTablePeer::getInstanceFromPool($key))) {
  88
+			// We no longer rehydrate the object, since this can cause data loss.
  89
+			// See http://www.propelorm.org/ticket/509
  90
+			// $obj->hydrate($row, $startcol, true); // rehydrate
  91
+			$col = $startcol + OtherTablePeer::NUM_HYDRATE_COLUMNS;
  92
+		} else {
  93
+			$cls = OtherTablePeerListener::OM_CLASS;
  94
+			$obj = new $cls();
  95
+			$col = $obj->hydrate($row, $startcol);
  96
+			OtherTablePeer::addInstanceToPool($obj, $key);
  97
+		}
  98
+		return array($obj, $col);
  99
+	}
  100
+}
  101
+
  102
+class OtherTableListenerQuery extends OtherTableQuery
  103
+{
  104
+	public function __construct($dbName = null, $modelName = null, $modelAlias = null)
  105
+	{
  106
+		parent::__construct();
  107
+		$this->modelName = 'OtherTableListener';
  108
+		$this->modelPeerName = constant($this->modelName . '::PEER');
  109
+	}
  110
+	
  111
+	public static function create($modelAlias = null, $criteria = null)
  112
+    {
  113
+        if ($criteria instanceof OtherTableQuery) {
  114
+            return $criteria;
  115
+        }
  116
+        $query = new OtherTableListenerQuery();
  117
+        if (null !== $modelAlias) {
  118
+            $query->setModelAlias($modelAlias);
  119
+        }
  120
+        if ($criteria instanceof Criteria) {
  121
+            $query->mergeWith($criteria);
  122
+        }
  123
+        return $query;
  124
+    }
  125
+}
  126
+
  127
+/* Workaround end */
  128
+
  129
+class OtherTableListener extends OtherTable
  130
+{
  131
+	const PEER = 'OtherTableListenerPeer';
  132
+	
  133
+	protected function callback($e, $method)
  134
+	{
  135
+		global $cb;
  136
+		$e['method'] = $method;
  137
+		$cb->receiveCallback($e);
  138
+	}
  139
+	
  140
+	public function handleEvent($e)
  141
+	{
  142
+		$this->callback($e, 'handleEvent');
  143
+	}
  144
+	
  145
+	public function onPostSave($e)
  146
+	{
  147
+		$this->callback($e, 'onPostSave');
  148
+	}
  149
+	
  150
+	public function listener($e)
  151
+	{
  152
+		$this->callback($e, 'listener');
  153
+	}
  154
+}
  155
+?>

0 notes on commit a6601e7

Please sign in to comment.
Something went wrong with that request. Please try again.