TYPO3 CMS  TYPO3_6-2
DispatcherTest.php
Go to the documentation of this file.
1 <?php
3 
21 
26 
27  public function setUp() {
28  $accessibleClassName = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher', array('dummy'));
29  $this->signalSlotDispatcher = new $accessibleClassName();
30  }
31 
36  $mockSignal = $this->getMock('ClassA', array('emitSomeSignal'));
37  $mockSlot = $this->getMock('ClassB', array('someSlotMethod'));
38  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', get_class($mockSlot), 'someSlotMethod', TRUE);
39  $expectedSlots = array(
40  array('class' => get_class($mockSlot), 'method' => 'someSlotMethod', 'object' => NULL, 'passSignalInformation' => TRUE)
41  );
42  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
43  }
44 
49  $mockSignal = $this->getMock('ClassA', array('emitSomeSignal'));
50  $mockSlot = $this->getMock('ClassB', array('someSlotMethod'));
51  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'someSlotMethod', TRUE);
52  $expectedSlots = array(
53  array('class' => NULL, 'method' => 'someSlotMethod', 'object' => $mockSlot, 'passSignalInformation' => TRUE)
54  );
55  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
56  }
57 
62  $mockSignal = $this->getMock('ClassA', array('emitSomeSignal'));
63  $mockSlot = function () {
64  };
65  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'foo', TRUE);
66  $expectedSlots = array(
67  array('class' => NULL, 'method' => '__invoke', 'object' => $mockSlot, 'passSignalInformation' => TRUE)
68  );
69  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
70  }
71 
76  $arguments = array();
77  $mockSlot = function () use (&$arguments) {
78  ($arguments = func_get_args());
79  };
80  $this->signalSlotDispatcher->connect('Foo', 'bar', $mockSlot, NULL, FALSE);
81  $this->signalSlotDispatcher->dispatch('Foo', 'bar', array('bar', 'quux'));
82  $this->assertSame(array('bar', 'quux'), $arguments);
83  }
84 
89  $slotClassName = $this->getUniqueId('Mock_');
90  eval('class ' . $slotClassName . ' { function slot($foo, $baz) { $this->arguments = array($foo, $baz); } }');
91  $mockSlot = new $slotClassName();
92  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
93  $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(TRUE));
94  $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
95  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
96  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
97  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'slot', FALSE);
98  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('bar', 'quux'));
99  $this->assertSame($mockSlot->arguments, array('bar', 'quux'));
100  }
101 
106  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
107 
108  $firstMockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
109  $firstMockSlot->expects($this->once())
110  ->method('slot')
111  ->will($this->returnCallback(
112  function($foo, $baz) {
113  return array('modified_' . $foo, 'modified_' . $baz);}
114  ));
115 
116  $secondMockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
117  $secondMockSlot->expects($this->once())
118  ->method('slot')
119  ->with('modified_bar', 'modified_quux');
120 
121 
122  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot', FALSE);
123  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot', FALSE);
124 
125  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('bar', 'quux'));
126  }
127 
132  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
133 
134  $firstMockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
135  $firstMockSlot->expects($this->once())
136  ->method('slot')
137  ->will($this->returnCallback(
138  function($foo, $baz) {
139  return array('modified_' . $foo, 'modified_' . $baz);}
140  ));
141 
142  $secondMockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
143  $secondMockSlot->expects($this->once())
144  ->method('slot')
145  ->with('modified_bar', 'modified_quux');
146 
147  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
148  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
149 
150  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('bar', 'quux'));
151  }
152 
157  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
158 
159  $firstMockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
160  $firstMockSlot->expects($this->once())
161  ->method('slot')
162  ->will($this->returnCallback(
163  function($foo, $baz) {
164  return array('modified_' . $foo, 'modified_' . $baz);}
165  ));
166 
167  $secondMockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
168  $secondMockSlot->expects($this->once())
169  ->method('slot');
170 
171  $thirdMockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
172  $thirdMockSlot->expects($this->once())
173  ->method('slot')
174  ->with('modified_bar', 'modified_quux');
175 
176 
177  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
178  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
179  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $thirdMockSlot, 'slot');
180 
181  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('bar', 'quux'));
182  }
183 
189  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
190 
191  $mockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
192  $mockSlot->expects($this->once())
193  ->method('slot')
194  ->will($this->returnCallback(
195  function() {
196  return 'string';}
197  ));
198 
199  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', FALSE);
200  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('bar', 'quux'));
201  }
202 
208  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
209 
210  $mockSlot = $this->getMock('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\SlotFixture');
211  $mockSlot->expects($this->once())
212  ->method('slot')
213  ->will($this->returnCallback(
214  function() {
215  return array(1, 2, 3);}
216  ));
217 
218  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', FALSE);
219  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('bar', 'quux'));
220  }
221 
227  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
228  $mockObjectManager->expects($this->once())->method('isRegistered')->with('NonExistingClassName')->will($this->returnValue(FALSE));
229  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
230  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
231  $this->signalSlotDispatcher->connect('Foo', 'emitBar', 'NonExistingClassName', 'slot', TRUE);
232  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array());
233  }
234 
240  $slotClassName = $this->getUniqueId('Mock_');
241  eval('class ' . $slotClassName . ' { function slot($foo, $baz) { $this->arguments = array($foo, $baz); } }');
242  $mockSlot = new $slotClassName();
243  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
244  $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(TRUE));
245  $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
246  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
247  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
248  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'unknownMethodName', TRUE);
249  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('bar', 'quux'));
250  $this->assertSame($mockSlot->arguments, array('bar', 'quux'));
251  }
252 
257  $arguments = array();
258  $mockSlot = function () use (&$arguments) {
259  ($arguments = func_get_args());
260  };
261  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
262  $this->signalSlotDispatcher->connect('SignalClassName', 'methodName', $mockSlot, NULL, TRUE);
263  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
264  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
265  $this->signalSlotDispatcher->dispatch('SignalClassName', 'methodName', array('bar', 'quux'));
266  $this->assertSame(array('bar', 'quux', 'SignalClassName::methodName'), $arguments);
267  }
268 
275  $this->signalSlotDispatcher->connect('ClassA', 'emitSomeSignal', 'ClassB', '');
276  }
277 
283  $this->assertSame(array(), $this->signalSlotDispatcher->dispatch('ClassA', 'someNotRegisteredSignalName'));
284  }
285 
290  $this->assertSame(array(), $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal'));
291  }
292 
297  $arguments = array(
298  42,
299  'a string',
300  new \stdClass()
301  );
302  $this->assertSame($arguments, $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal', $arguments));
303  }
304 
311  $this->signalSlotDispatcher->_set('isInitialized', TRUE);
312  $this->signalSlotDispatcher->_set('objectManager', NULL);
313  $this->signalSlotDispatcher->_set('slots', array('ClassA' => array('emitSomeSignal' => array(array()))));
314 
315  $this->assertSame(NULL, $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal'));
316  }
317 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)