‪TYPO3CMS  10.4
DispatcherTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Prophecy\Prophecy\ObjectProphecy;
21 use Psr\Log\LoggerInterface;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪DispatcherTest extends UnitTestCase
35 {
39  protected ‪$resetSingletonInstances = true;
40 
44  protected ‪$objectManagerProphecy;
45 
49  protected ‪$signalSlotDispatcher;
50 
51  protected function ‪setUp(): void
52  {
53  parent::setUp();
54  $this->objectManagerProphecy = $this->prophesize(ObjectManagerInterface::class);
55 
56  $this->signalSlotDispatcher = new ‪Dispatcher(
57  $this->objectManagerProphecy->reveal(),
58  $this->prophesize(LoggerInterface::class)->reveal()
59  );
60  }
61 
66  {
67  $mockSignal = $this->getMockBuilder('ClassA')
68  ->setMethods(['emitSomeSignal'])
69  ->getMock();
70  $mockSlot = $this->getMockBuilder('ClassB')
71  ->setMethods(['someSlotMethod'])
72  ->getMock();
73  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', get_class($mockSlot), 'someSlotMethod', true);
74  $expectedSlots = [
75  ['class' => get_class($mockSlot), 'method' => 'someSlotMethod', 'object' => null, 'passSignalInformation' => true]
76  ];
77  self::assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
78  }
79 
84  {
85  $mockSignal = $this->getMockBuilder('ClassA')
86  ->setMethods(['emitSomeSignal'])
87  ->getMock();
88  $mockSlot = $this->getMockBuilder('ClassB')
89  ->setMethods(['someSlotMethod'])
90  ->getMock();
91  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'someSlotMethod', true);
92  $expectedSlots = [
93  ['class' => null, 'method' => 'someSlotMethod', 'object' => $mockSlot, 'passSignalInformation' => true]
94  ];
95  self::assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
96  }
97 
102  {
103  $mockSignal = $this->getMockBuilder('ClassA')
104  ->setMethods(['emitSomeSignal'])
105  ->getMock();
106  $mockSlot = function () {
107  };
108  $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'foo', true);
109  $expectedSlots = [
110  ['class' => null, 'method' => '__invoke', 'object' => $mockSlot, 'passSignalInformation' => true]
111  ];
112  self::assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
113  }
114 
119  {
120  $arguments = [];
121  $mockSlot = function () use (&$arguments) {
122  $arguments = func_get_args();
123  };
124  $this->signalSlotDispatcher->connect('Foo', 'bar', $mockSlot, '', false);
125  $this->signalSlotDispatcher->dispatch('Foo', 'bar', ['bar', 'quux']);
126  self::assertSame(['bar', 'quux'], $arguments);
127  }
128 
133  {
134  $slotClassName = OnlyClassNameSpecifiedFixture::class;
135  $mockSlot = new ‪OnlyClassNameSpecifiedFixture();
136  $this->objectManagerProphecy->get($slotClassName)->willReturn($mockSlot);
137  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'slot', false);
138  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
139  self::assertSame($mockSlot->arguments, ['bar', 'quux']);
140  }
141 
146  {
147  $firstMockSlot = $this->createMock(SlotFixture::class);
148  $firstMockSlot->expects(self::once())
149  ->method('slot')
150  ->willReturnCallback(
151  function ($foo, $baz) {
152  return ['modified_' . $foo, 'modified_' . $baz];
153  }
154  );
155 
156  $secondMockSlot = $this->createMock(SlotFixture::class);
157  $secondMockSlot->expects(self::once())
158  ->method('slot')
159  ->with('modified_bar', 'modified_quux');
160 
161  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot', false);
162  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot', false);
163 
164  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
165  }
166 
171  {
172  $firstMockSlot = $this->createMock(SlotFixture::class);
173  $firstMockSlot->expects(self::once())
174  ->method('slot')
175  ->willReturnCallback(
176  function ($foo, $baz) {
177  return ['modified_' . $foo, 'modified_' . $baz];
178  }
179  );
180 
181  $secondMockSlot = $this->createMock(SlotFixture::class);
182  $secondMockSlot->expects(self::once())
183  ->method('slot')
184  ->with('modified_bar', 'modified_quux');
185 
186  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
187  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
188 
189  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
190  }
191 
196  {
197  $firstMockSlot = $this->createMock(SlotFixture::class);
198  $firstMockSlot->expects(self::once())
199  ->method('slot')
200  ->willReturnCallback(
201  function ($foo, $baz) {
202  return ['modified_' . $foo, 'modified_' . $baz];
203  }
204  );
205 
206  $secondMockSlot = $this->createMock(SlotFixture::class);
207  $secondMockSlot->expects(self::once())
208  ->method('slot');
209 
210  $thirdMockSlot = $this->createMock(SlotFixture::class);
211  $thirdMockSlot->expects(self::once())
212  ->method('slot')
213  ->with('modified_bar', 'modified_quux');
214 
215  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $firstMockSlot, 'slot');
216  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $secondMockSlot, 'slot');
217  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $thirdMockSlot, 'slot');
218 
219  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
220  }
221 
226  {
227  $this->expectException(InvalidSlotReturnException::class);
228  $this->expectExceptionCode(1376683067);
229 
230  $mockSlot = $this->createMock(SlotFixture::class);
231  $mockSlot->expects(self::once())
232  ->method('slot')
233  ->willReturnCallback(
234  function () {
235  return 'string';
236  }
237  );
238 
239  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', false);
240  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
241  }
242 
247  {
248  $this->expectException(InvalidSlotReturnException::class);
249  $this->expectExceptionCode(1376683066);
250 
251  $mockSlot = $this->createMock(SlotFixture::class);
252  $mockSlot->expects(self::once())
253  ->method('slot')
254  ->willReturnCallback(
255  function () {
256  return [1, 2, 3];
257  }
258  );
259 
260  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $mockSlot, 'slot', false);
261  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
262  }
263 
268  {
269  $this->expectException(InvalidSlotException::class);
270  $this->expectExceptionCode(1245673367);
271  $this->signalSlotDispatcher->connect('Foo', 'emitBar', 'NonExistingClassName', 'slot', true);
272  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', []);
273  }
274 
279  {
280  $this->expectException(InvalidSlotException::class);
281  $this->expectExceptionCode(1245673368);
282  $slotClassName = SlotMethodDoesNotExistFixture::class;
283  $mockSlot = new ‪SlotMethodDoesNotExistFixture();
284  $this->objectManagerProphecy->get($slotClassName)->willReturn($mockSlot);
285  $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'unknownMethodName', true);
286  $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', ['bar', 'quux']);
287  self::assertSame($mockSlot->arguments, ['bar', 'quux']);
288  }
289 
294  {
295  $arguments = [];
296  $mockSlot = function () use (&$arguments) {
297  $arguments = func_get_args();
298  };
299  $this->signalSlotDispatcher->connect('SignalClassName', 'methodName', $mockSlot, '', true);
300  $this->signalSlotDispatcher->dispatch('SignalClassName', 'methodName', ['bar', 'quux']);
301  self::assertSame(['bar', 'quux', 'SignalClassName::methodName'], $arguments);
302  }
303 
308  {
309  $this->expectException(\InvalidArgumentException::class);
310  $this->expectExceptionCode(1229531659);
311  $this->signalSlotDispatcher->connect('ClassA', 'emitSomeSignal', 'ClassB', '');
312  }
313 
318  {
319  self::assertSame([], $this->signalSlotDispatcher->dispatch('ClassA', 'someNotRegisteredSignalName'));
320  }
321 
326  {
327  self::assertSame([], $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal'));
328  }
329 
334  {
335  $arguments = [
336  42,
337  'a string',
338  new \stdClass()
339  ];
340  self::assertSame($arguments, $this->signalSlotDispatcher->dispatch('ClassA', 'emitSomeSignal', $arguments));
341  }
342 }
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchRetrievesSlotInstanceFromTheObjectManagerIfOnlyAClassNameWasSpecified
‪dispatchRetrievesSlotInstanceFromTheObjectManagerIfOnlyAClassNameWasSpecified()
Definition: DispatcherTest.php:129
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSpecifiedSlotMethodDoesNotExist
‪dispatchThrowsAnExceptionIfTheSpecifiedSlotMethodDoesNotExist()
Definition: DispatcherTest.php:275
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchHandsOverFormerArgumentsIfPreviousSlotDoesNotReturnAnything
‪dispatchHandsOverFormerArgumentsIfPreviousSlotDoesNotReturnAnything()
Definition: DispatcherTest.php:192
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot
Definition: DispatcherTest.php:18
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchHandsOverArgumentsReturnedByAFormerSlotWithoutInterferingWithSignalSlotInformation
‪dispatchHandsOverArgumentsReturnedByAFormerSlotWithoutInterferingWithSignalSlotInformation()
Definition: DispatcherTest.php:167
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\connectAlsoAcceptsClosuresActingAsASlot
‪connectAlsoAcceptsClosuresActingAsASlot()
Definition: DispatcherTest.php:98
‪TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
Definition: InvalidSlotReturnException.php:26
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSpecifiedClassOfASlotIsUnknown
‪dispatchThrowsAnExceptionIfTheSpecifiedClassOfASlotIsUnknown()
Definition: DispatcherTest.php:264
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchHandsOverArgumentsReturnedByAFormerSlot
‪dispatchHandsOverArgumentsReturnedByAFormerSlot()
Definition: DispatcherTest.php:142
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest
Definition: DispatcherTest.php:35
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchPassesTheSignalArgumentsToTheSlotMethod
‪dispatchPassesTheSignalArgumentsToTheSlotMethod()
Definition: DispatcherTest.php:115
‪TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
Definition: InvalidSlotException.php:26
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\$objectManagerProphecy
‪ObjectManagerInterface ObjectProphecy $objectManagerProphecy
Definition: DispatcherTest.php:42
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\connectAlsoAcceptsObjectsInPlaceOfTheClassName
‪connectAlsoAcceptsObjectsInPlaceOfTheClassName()
Definition: DispatcherTest.php:80
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\Fixtures\OnlyClassNameSpecifiedFixture
Definition: OnlyClassNameSpecifiedFixture.php:22
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\connectThrowsInvalidArgumentExceptionIfSlotMethodNameIsEmptyAndSlotClassNameIsNoClosure
‪connectThrowsInvalidArgumentExceptionIfSlotMethodNameIsEmptyAndSlotClassNameIsNoClosure()
Definition: DispatcherTest.php:304
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchPassesFirstArgumentContainingSlotInformationIfTheConnectionStatesSo
‪dispatchPassesFirstArgumentContainingSlotInformationIfTheConnectionStatesSo()
Definition: DispatcherTest.php:290
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSlotReturnsDifferentNumberOfItems
‪dispatchThrowsAnExceptionIfTheSlotReturnsDifferentNumberOfItems()
Definition: DispatcherTest.php:243
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchReturnsEmptyArrayIfSignalNameAndOrSignalClassNameIsNotRegistered
‪dispatchReturnsEmptyArrayIfSignalNameAndOrSignalClassNameIsNotRegistered()
Definition: DispatcherTest.php:314
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\$signalSlotDispatcher
‪TYPO3 CMS Extbase SignalSlot Dispatcher PHPUnit Framework MockObject MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $signalSlotDispatcher
Definition: DispatcherTest.php:46
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\Fixtures\SlotMethodDoesNotExistFixture
Definition: SlotMethodDoesNotExistFixture.php:22
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchReturnsArgumentsArrayAsIsIfSignalIsNotRegistered
‪dispatchReturnsArgumentsArrayAsIsIfSignalIsNotRegistered()
Definition: DispatcherTest.php:330
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchThrowsAnExceptionIfTheSlotReturnsNonArray
‪dispatchThrowsAnExceptionIfTheSlotReturnsNonArray()
Definition: DispatcherTest.php:222
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\dispatchReturnsEmptyArrayIfSignalDoesNotProvideAnyArguments
‪dispatchReturnsEmptyArrayIfSignalDoesNotProvideAnyArguments()
Definition: DispatcherTest.php:322
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\setUp
‪setUp()
Definition: DispatcherTest.php:48
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: DispatcherTest.php:38
‪TYPO3\CMS\Extbase\Tests\Fixture\SlotFixture
Definition: SlotFixture.php:22
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\SignalSlot\DispatcherTest\connectAllowsForConnectingASlotWithASignal
‪connectAllowsForConnectingASlotWithASignal()
Definition: DispatcherTest.php:62
‪TYPO3\CMS\Extbase\SignalSlot\Dispatcher
Definition: Dispatcher.php:126