TYPO3 CMS  TYPO3_7-6
DispatcherTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
29 
30  protected function setUp()
31  {
32  $accessibleClassName = $this->getAccessibleMock(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class, ['dummy']);
33  $this->signalSlotDispatcher = new $accessibleClassName();
34  }
35 
40  {
41  $mockSignal = $this->getMock('ClassA', ['emitSomeSignal']);
42  $mockSlot = $this->getMock('ClassB', ['someSlotMethod']);
43  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', get_class($mockSlot), 'someSlotMethod', true);
44  $expectedSlots = [
45  ['class' => get_class($mockSlot), 'method' => 'someSlotMethod', 'object' => null, 'passSignalInformation' => true]
46  ];
47  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
48  }
49 
54  {
55  $mockSignal = $this->getMock('ClassA', ['emitSomeSignal']);
56  $mockSlot = $this->getMock('ClassB', ['someSlotMethod']);
57  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'someSlotMethod', true);
58  $expectedSlots = [
59  ['class' => null, 'method' => 'someSlotMethod', 'object' => $mockSlot, 'passSignalInformation' => true]
60  ];
61  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
62  }
63 
68  {
69  $mockSignal = $this->getMock('ClassA', ['emitSomeSignal']);
70  $mockSlot = function () {
71  };
72  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'foo', true);
73  $expectedSlots = [
74  ['class' => null, 'method' => '__invoke', 'object' => $mockSlot, 'passSignalInformation' => true]
75  ];
76  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
77  }
78 
83  {
84  $arguments = [];
85  $mockSlot = function () use (&$arguments) {
86  ($arguments = func_get_args());
87  };
88  $this->signalSlotDispatcher->connect('Foo', 'bar', $mockSlot, null, false);
89  $this->signalSlotDispatcher->dispatch('Foo', 'bar', ['bar', 'quux']);
90  $this->assertSame(['bar', 'quux'], $arguments);
91  }
92 
97  {
98  $slotClassName = OnlyClassNameSpecifiedFixture::class;
99  $mockSlot = new OnlyClassNameSpecifiedFixture();
100  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
101  $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(true));
102  $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
103  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
104  $this->signalSlotDispatcher->_set('isInitialized', true);
105  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'slot', false);
106  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
107  $this->assertSame($mockSlot->arguments, ['bar', 'quux']);
108  }
109 
114  {
115  $this->signalSlotDispatcher->_set('isInitialized', true);
116 
117  $firstMockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
118  $firstMockSlot->expects($this->once())
119  ->method('slot')
120  ->will($this->returnCallback(
121  function ($foo, $baz) {
122  return ['modified_' . $foo, 'modified_' . $baz];
123  }
124  ));
125 
126  $secondMockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
127  $secondMockSlot->expects($this->once())
128  ->method('slot')
129  ->with('modified_bar', 'modified_quux');
130 
131  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot', false);
132  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot', false);
133 
134  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
135  }
136 
141  {
142  $this->signalSlotDispatcher->_set('isInitialized', true);
143 
144  $firstMockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
145  $firstMockSlot->expects($this->once())
146  ->method('slot')
147  ->will($this->returnCallback(
148  function ($foo, $baz) {
149  return ['modified_' . $foo, 'modified_' . $baz];
150  }
151  ));
152 
153  $secondMockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
154  $secondMockSlot->expects($this->once())
155  ->method('slot')
156  ->with('modified_bar', 'modified_quux');
157 
158  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
159  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
160 
161  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
162  }
163 
168  {
169  $this->signalSlotDispatcher->_set('isInitialized', true);
170 
171  $firstMockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
172  $firstMockSlot->expects($this->once())
173  ->method('slot')
174  ->will($this->returnCallback(
175  function ($foo, $baz) {
176  return ['modified_' . $foo, 'modified_' . $baz];
177  }
178  ));
179 
180  $secondMockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
181  $secondMockSlot->expects($this->once())
182  ->method('slot');
183 
184  $thirdMockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
185  $thirdMockSlot->expects($this->once())
186  ->method('slot')
187  ->with('modified_bar', 'modified_quux');
188 
189  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
190  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
191  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $thirdMockSlot, 'slot');
192 
193  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
194  }
195 
201  {
202  $this->signalSlotDispatcher->_set('isInitialized', true);
203 
204  $mockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
205  $mockSlot->expects($this->once())
206  ->method('slot')
207  ->will($this->returnCallback(
208  function () {
209  return 'string';
210  }
211  ));
212 
213  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', false);
214  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
215  }
216 
222  {
223  $this->signalSlotDispatcher->_set('isInitialized', true);
224 
225  $mockSlot = $this->getMock(\TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture::class);
226  $mockSlot->expects($this->once())
227  ->method('slot')
228  ->will($this->returnCallback(
229  function () {
230  return [1, 2, 3];
231  }
232  ));
233 
234  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', false);
235  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
236  }
237 
243  {
244  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
245  $mockObjectManager->expects($this->once())->method('isRegistered')->with('NonExistingClassName')->will($this->returnValue(false));
246  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
247  $this->signalSlotDispatcher->_set('isInitialized', true);
248  $this->signalSlotDispatcher->connect('Foo', 'emitBar', 'NonExistingClassName', 'slot', true);
249  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', []);
250  }
251 
257  {
258  $slotClassName = SlotMethodDoesNotExistFixture::class;
259  $mockSlot = new SlotMethodDoesNotExistFixture();
260  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
261  $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(true));
262  $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
263  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
264  $this->signalSlotDispatcher->_set('isInitialized', true);
265  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'unknownMethodName', true);
266  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
267  $this->assertSame($mockSlot->arguments, ['bar', 'quux']);
268  }
269 
274  {
275  $arguments = [];
276  $mockSlot = function () use (&$arguments) {
277  ($arguments = func_get_args());
278  };
279  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
280  $this->signalSlotDispatcher->connect('SignalClassName', 'methodName', $mockSlot, null, true);
281  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
282  $this->signalSlotDispatcher->_set('isInitialized', true);
283  $this->signalSlotDispatcher->dispatch('SignalClassName', 'methodName', ['bar', 'quux']);
284  $this->assertSame(['bar', 'quux', 'SignalClassName::methodName'], $arguments);
285  }
286 
292  {
293  $this->signalSlotDispatcher->connect('ClassA', 'emitSomeSignal', 'ClassB', '');
294  }
295 
300  {
301  $this->assertSame([], $this->signalSlotDispatcher->dispatch('ClassA', 'someNotRegisteredSignalName'));
302  }
303 
308  {
309  $this->assertSame([], $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal'));
310  }
311 
316  {
317  $arguments = [
318  42,
319  'a string',
320  new \stdClass()
321  ];
322  $this->assertSame($arguments, $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal', $arguments));
323  }
324 
330  {
331  $this->signalSlotDispatcher->_set('isInitialized', true);
332  $this->signalSlotDispatcher->_set('objectManager', null);
333  $this->signalSlotDispatcher->_set('slots', ['ClassA' => ['emitSomeSignal' => [[]]]]);
334 
335  $this->assertSame(null, $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal'));
336  }
337 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)