‪TYPO3CMS  9.5
DispatcherTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪DispatcherTest extends UnitTestCase
32 {
36  protected ‪$resetSingletonInstances = true;
37 
42 
43  protected function ‪setUp()
44  {
45  $accessibleClassName = $this->getAccessibleMock(Dispatcher::class, ['dummy']);
46  $this->signalSlotDispatcher = new $accessibleClassName();
47  }
48 
53  {
54  $mockSignal = $this->getMockBuilder('ClassA')
55  ->setMethods(['emitSomeSignal'])
56  ->getMock();
57  $mockSlot = $this->getMockBuilder('ClassB')
58  ->setMethods(['someSlotMethod'])
59  ->getMock();
60  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', get_class($mockSlot), 'someSlotMethod', true);
61  $expectedSlots = [
62  ['class' => get_class($mockSlot), 'method' => 'someSlotMethod', 'object' => null, 'passSignalInformation' => true]
63  ];
64  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
65  }
66 
71  {
72  $mockSignal = $this->getMockBuilder('ClassA')
73  ->setMethods(['emitSomeSignal'])
74  ->getMock();
75  $mockSlot = $this->getMockBuilder('ClassB')
76  ->setMethods(['someSlotMethod'])
77  ->getMock();
78  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'someSlotMethod', true);
79  $expectedSlots = [
80  ['class' => null, 'method' => 'someSlotMethod', 'object' => $mockSlot, 'passSignalInformation' => true]
81  ];
82  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
83  }
84 
89  {
90  $mockSignal = $this->getMockBuilder('ClassA')
91  ->setMethods(['emitSomeSignal'])
92  ->getMock();
93  $mockSlot = function () {
94  };
95  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'foo', true);
96  $expectedSlots = [
97  ['class' => null, 'method' => '__invoke', 'object' => $mockSlot, 'passSignalInformation' => true]
98  ];
99  $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
100  }
101 
106  {
107  $arguments = [];
108  $mockSlot = function () use (&$arguments) {
109  $arguments = func_get_args();
110  };
111  $this->signalSlotDispatcher->connect('Foo', 'bar', $mockSlot, null, false);
112  $this->signalSlotDispatcher->dispatch('Foo', 'bar', ['bar', 'quux']);
113  $this->assertSame(['bar', 'quux'], $arguments);
114  }
115 
120  {
121  $slotClassName = OnlyClassNameSpecifiedFixture::class;
122  $mockSlot = new OnlyClassNameSpecifiedFixture();
123  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
124  $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(true));
125  $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
126  $mockLogger = $this->createMock(Logger::class);
127  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
128  $this->signalSlotDispatcher->_set('isInitialized', true);
129  $this->signalSlotDispatcher->_set('logger', $mockLogger);
130  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'slot', false);
131  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
132  $this->assertSame($mockSlot->arguments, ['bar', 'quux']);
133  }
134 
139  {
140  $this->signalSlotDispatcher->_set('isInitialized', true);
141  $mockLogger = $this->createMock(Logger::class);
142  $this->signalSlotDispatcher->_set('logger', $mockLogger);
143 
144  $firstMockSlot = $this->createMock(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->createMock(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', false);
159  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot', false);
160 
161  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
162  }
163 
168  {
169  $this->signalSlotDispatcher->_set('isInitialized', true);
170  $mockLogger = $this->createMock(Logger::class);
171  $this->signalSlotDispatcher->_set('logger', $mockLogger);
172 
173  $firstMockSlot = $this->createMock(SlotFixture::class);
174  $firstMockSlot->expects($this->once())
175  ->method('slot')
176  ->will($this->returnCallback(
177  function ($foo, $baz) {
178  return ['modified_' . $foo, 'modified_' . $baz];
179  }
180  ));
181 
182  $secondMockSlot = $this->createMock(SlotFixture::class);
183  $secondMockSlot->expects($this->once())
184  ->method('slot')
185  ->with('modified_bar', 'modified_quux');
186 
187  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
188  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
189 
190  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
191  }
192 
197  {
198  $this->signalSlotDispatcher->_set('isInitialized', true);
199  $mockLogger = $this->createMock(Logger::class);
200  $this->signalSlotDispatcher->_set('logger', $mockLogger);
201 
202  $firstMockSlot = $this->createMock(SlotFixture::class);
203  $firstMockSlot->expects($this->once())
204  ->method('slot')
205  ->will($this->returnCallback(
206  function ($foo, $baz) {
207  return ['modified_' . $foo, 'modified_' . $baz];
208  }
209  ));
210 
211  $secondMockSlot = $this->createMock(SlotFixture::class);
212  $secondMockSlot->expects($this->once())
213  ->method('slot');
214 
215  $thirdMockSlot = $this->createMock(SlotFixture::class);
216  $thirdMockSlot->expects($this->once())
217  ->method('slot')
218  ->with('modified_bar', 'modified_quux');
219 
220  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
221  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
222  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $thirdMockSlot, 'slot');
223 
224  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
225  }
226 
231  {
232  $this->expectException(InvalidSlotReturnException::class);
233  $this->expectExceptionCode(1376683067);
234  $this->signalSlotDispatcher->_set('isInitialized', true);
235  $mockLogger = $this->createMock(Logger::class);
236  $this->signalSlotDispatcher->_set('logger', $mockLogger);
237 
238  $mockSlot = $this->createMock(SlotFixture::class);
239  $mockSlot->expects($this->once())
240  ->method('slot')
241  ->will($this->returnCallback(
242  function () {
243  return 'string';
244  }
245  ));
246 
247  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', false);
248  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
249  }
250 
255  {
256  $this->expectException(InvalidSlotReturnException::class);
257  $this->expectExceptionCode(1376683066);
258  $this->signalSlotDispatcher->_set('isInitialized', true);
259  $mockLogger = $this->createMock(Logger::class);
260  $this->signalSlotDispatcher->_set('logger', $mockLogger);
261 
262  $mockSlot = $this->createMock(SlotFixture::class);
263  $mockSlot->expects($this->once())
264  ->method('slot')
265  ->will($this->returnCallback(
266  function () {
267  return [1, 2, 3];
268  }
269  ));
270 
271  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', false);
272  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
273  }
274 
279  {
280  $this->expectException(InvalidSlotException::class);
281  $this->expectExceptionCode(1245673367);
282  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
283  $mockObjectManager->expects($this->once())->method('isRegistered')->with('NonExistingClassName')->will($this->returnValue(false));
284  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
285  $this->signalSlotDispatcher->_set('isInitialized', true);
286  $mockLogger = $this->createMock(Logger::class);
287  $this->signalSlotDispatcher->_set('logger', $mockLogger);
288  $this->signalSlotDispatcher->connect('Foo', 'emitBar', 'NonExistingClassName', 'slot', true);
289  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', []);
290  }
291 
296  {
297  $this->expectException(InvalidSlotException::class);
298  $this->expectExceptionCode(1245673368);
299  $slotClassName = SlotMethodDoesNotExistFixture::class;
300  $mockSlot = new SlotMethodDoesNotExistFixture();
301  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
302  $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(true));
303  $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
304  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
305  $this->signalSlotDispatcher->_set('isInitialized', true);
306  $mockLogger = $this->createMock(Logger::class);
307  $this->signalSlotDispatcher->_set('logger', $mockLogger);
308  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'unknownMethodName', true);
309  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
310  $this->assertSame($mockSlot->arguments, ['bar', 'quux']);
311  }
312 
317  {
318  $arguments = [];
319  $mockSlot = function () use (&$arguments) {
320  $arguments = func_get_args();
321  };
322  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
323  $this->signalSlotDispatcher->connect('SignalClassName', 'methodName', $mockSlot, null, true);
324  $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
325  $this->signalSlotDispatcher->_set('isInitialized', true);
326  $mockLogger = $this->createMock(Logger::class);
327  $this->signalSlotDispatcher->_set('logger', $mockLogger);
328  $this->signalSlotDispatcher->dispatch('SignalClassName', 'methodName', ['bar', 'quux']);
329  $this->assertSame(['bar', 'quux', 'SignalClassName::methodName'], $arguments);
330  }
331 
336  {
337  $this->expectException(\InvalidArgumentException::class);
338  $this->expectExceptionCode(1229531659);
339  $this->signalSlotDispatcher->connect('ClassA', 'emitSomeSignal', 'ClassB', '');
340  }
341 
346  {
347  $this->assertSame([], $this->signalSlotDispatcher->dispatch('ClassA', 'someNotRegisteredSignalName'));
348  }
349 
354  {
355  $this->assertSame([], $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal'));
356  }
357 
362  {
363  $arguments = [
364  42,
365  'a string',
366  new \stdClass()
367  ];
368  $this->assertSame($arguments, $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal', $arguments));
369  }
370 
375  {
376  $this->expectException(InvalidSlotException::class);
377  $this->expectExceptionCode(1298113624);
378  $this->signalSlotDispatcher->_set('isInitialized', true);
379  $mockLogger = $this->createMock(Logger::class);
380  $this->signalSlotDispatcher->_set('logger', $mockLogger);
381  $this->signalSlotDispatcher->_set('objectManager', null);
382  $this->signalSlotDispatcher->_set('slots', ['ClassA' => ['emitSomeSignal' => [[]]]]);
383 
384  $this->assertSame(null, $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal'));
385  }
386 }
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchPassesTheSignalArgumentsToTheSlotMethod
‪dispatchPassesTheSignalArgumentsToTheSlotMethod()
Definition: DispatcherTest.php:103
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\Fixtures\SlotMethodDoesNotExistFixture
Definition: SlotMethodDoesNotExistFixture.php:21
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\connectAlsoAcceptsObjectsInPlaceOfTheClassName
‪connectAlsoAcceptsObjectsInPlaceOfTheClassName()
Definition: DispatcherTest.php:68
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\connectAllowsForConnectingASlotWithASignal
‪connectAllowsForConnectingASlotWithASignal()
Definition: DispatcherTest.php:50
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\connectAlsoAcceptsClosuresActingAsASlot
‪connectAlsoAcceptsClosuresActingAsASlot()
Definition: DispatcherTest.php:86
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\Fixtures\OnlyClassNameSpecifiedFixture
Definition: OnlyClassNameSpecifiedFixture.php:21
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchThrowsInvalidSlotExceptionIfObjectManagerOfSignalSlotDispatcherIsNotSet
‪dispatchThrowsInvalidSlotExceptionIfObjectManagerOfSignalSlotDispatcherIsNotSet()
Definition: DispatcherTest.php:372
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchHandsOverFormerArgumentsIfPreviousSlotDoesNotReturnAnything
‪dispatchHandsOverFormerArgumentsIfPreviousSlotDoesNotReturnAnything()
Definition: DispatcherTest.php:194
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSpecifiedSlotMethodDoesNotExist
‪dispatchThrowsAnExceptionIfTheSpecifiedSlotMethodDoesNotExist()
Definition: DispatcherTest.php:293
‪TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
Definition: InvalidSlotReturnException.php:21
‪TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
Definition: InvalidSlotException.php:21
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\connectThrowsInvalidArgumentExceptionIfSlotMethodNameIsEmptyAndSlotClassNameIsNoClosure
‪connectThrowsInvalidArgumentExceptionIfSlotMethodNameIsEmptyAndSlotClassNameIsNoClosure()
Definition: DispatcherTest.php:333
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot
Definition: DispatcherTest.php:3
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: DispatcherTest.php:35
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchHandsOverArgumentsReturnedByAFormerSlotWithoutInterferingWithSignalSlotInformation
‪dispatchHandsOverArgumentsReturnedByAFormerSlotWithoutInterferingWithSignalSlotInformation()
Definition: DispatcherTest.php:165
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\setUp
‪setUp()
Definition: DispatcherTest.php:41
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchPassesFirstArgumentContainingSlotInformationIfTheConnectionStatesSo
‪dispatchPassesFirstArgumentContainingSlotInformationIfTheConnectionStatesSo()
Definition: DispatcherTest.php:314
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSpecifiedClassOfASlotIsUnknown
‪dispatchThrowsAnExceptionIfTheSpecifiedClassOfASlotIsUnknown()
Definition: DispatcherTest.php:276
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchHandsOverArgumentsReturnedByAFormerSlot
‪dispatchHandsOverArgumentsReturnedByAFormerSlot()
Definition: DispatcherTest.php:136
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchReturnsArgumentsArrayAsIsIfSignalIsNotRegistered
‪dispatchReturnsArgumentsArrayAsIsIfSignalIsNotRegistered()
Definition: DispatcherTest.php:359
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest
Definition: DispatcherTest.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\$signalSlotDispatcher
‪TYPO3 CMS Extbase SignalSlot Dispatcher PHPUnit_Framework_MockObject_MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $signalSlotDispatcher
Definition: DispatcherTest.php:39
‪TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture
Definition: SlotFixture.php:21
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchReturnsEmptyArrayIfSignalNameAndOrSignalClassNameIsNotRegistered
‪dispatchReturnsEmptyArrayIfSignalNameAndOrSignalClassNameIsNotRegistered()
Definition: DispatcherTest.php:343
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSlotReturnsDifferentNumberOfItems
‪dispatchThrowsAnExceptionIfTheSlotReturnsDifferentNumberOfItems()
Definition: DispatcherTest.php:252
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchReturnsEmptyArrayIfSignalDoesNotProvideAnyArguments
‪dispatchReturnsEmptyArrayIfSignalDoesNotProvideAnyArguments()
Definition: DispatcherTest.php:351
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchRetrievesSlotInstanceFromTheObjectManagerIfOnlyAClassNameWasSpecified
‪dispatchRetrievesSlotInstanceFromTheObjectManagerIfOnlyAClassNameWasSpecified()
Definition: DispatcherTest.php:117
‪TYPO3\CMS\Extbase\Tests\Unit\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSlotReturnsNonArray
‪dispatchThrowsAnExceptionIfTheSlotReturnsNonArray()
Definition: DispatcherTest.php:228
‪TYPO3\CMS\Extbase\SignalSlot\Dispatcher
Definition: Dispatcher.php:28