[TASK] Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / SignalSlot / DispatcherTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\SignalSlot;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Felix Oertel <f@oer.tel>
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of FLOW3.
11 * All credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Testcase for the Signal Dispatcher Class
31 *
32 * @package Extbase
33 * @subpackage Tests
34 */
35 class DispatcherTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
36
37 /**
38 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
39 */
40 protected $signalSlotDispatcher;
41
42 public function setUp() {
43 $accessibleClassName = $this->buildAccessibleProxy('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
44 $this->signalSlotDispatcher = new $accessibleClassName();
45 }
46
47 /**
48 * @test
49 */
50 public function connectAllowsForConnectingASlotWithASignal() {
51 $mockSignal = $this->getMock('ClassA', array('emitSomeSignal'));
52 $mockSlot = $this->getMock('ClassB', array('someSlotMethod'));
53 $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', get_class($mockSlot), 'someSlotMethod', TRUE);
54 $expectedSlots = array(
55 array('class' => get_class($mockSlot), 'method' => 'someSlotMethod', 'object' => NULL, 'passSignalInformation' => TRUE)
56 );
57 $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
58 }
59
60 /**
61 * @test
62 */
63 public function connectAlsoAcceptsObjectsInPlaceOfTheClassName() {
64 $mockSignal = $this->getMock('ClassA', array('emitSomeSignal'));
65 $mockSlot = $this->getMock('ClassB', array('someSlotMethod'));
66 $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'someSlotMethod', TRUE);
67 $expectedSlots = array(
68 array('class' => NULL, 'method' => 'someSlotMethod', 'object' => $mockSlot, 'passSignalInformation' => TRUE)
69 );
70 $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
71 }
72
73 /**
74 * @test
75 */
76 public function connectAlsoAcceptsClosuresActingAsASlot() {
77 $mockSignal = $this->getMock('ClassA', array('emitSomeSignal'));
78 $mockSlot = function () {
79
80 };
81 $this->signalSlotDispatcher->connect(get_class($mockSignal), 'emitSomeSignal', $mockSlot, 'foo', TRUE);
82 $expectedSlots = array(
83 array('class' => NULL, 'method' => '__invoke', 'object' => $mockSlot, 'passSignalInformation' => TRUE)
84 );
85 $this->assertSame($expectedSlots, $this->signalSlotDispatcher->getSlots(get_class($mockSignal), 'emitSomeSignal'));
86 }
87
88 /**
89 * @test
90 */
91 public function dispatchPassesTheSignalArgumentsToTheSlotMethod() {
92 $arguments = array();
93 $mockSlot = function () use(&$arguments) {
94 ($arguments = func_get_args());
95 };
96 $this->signalSlotDispatcher->connect('Foo', 'bar', $mockSlot, NULL, FALSE);
97 $this->signalSlotDispatcher->dispatch('Foo', 'bar', array('foo' => 'bar', 'baz' => 'quux'));
98 $this->assertSame(array('bar', 'quux'), $arguments);
99 }
100
101 /**
102 * @test
103 */
104 public function dispatchRetrievesSlotInstanceFromTheObjectManagerIfOnlyAClassNameWasSpecified() {
105 $slotClassName = uniqid('Mock_');
106 eval(('class ' . $slotClassName) . ' { function slot($foo, $baz) { $this->arguments = array($foo, $baz); } }');
107 $mockSlot = new $slotClassName();
108 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
109 $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(TRUE));
110 $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
111 $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
112 $this->signalSlotDispatcher->_set('isInitialized', TRUE);
113 $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'slot', FALSE);
114 $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('foo' => 'bar', 'baz' => 'quux'));
115 $this->assertSame($mockSlot->arguments, array('bar', 'quux'));
116 }
117
118 /**
119 * @test
120 * @expectedException \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
121 */
122 public function dispatchThrowsAnExceptionIfTheSpecifiedClassOfASlotIsUnknown() {
123 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
124 $mockObjectManager->expects($this->once())->method('isRegistered')->with('NonExistingClassName')->will($this->returnValue(FALSE));
125 $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
126 $this->signalSlotDispatcher->_set('isInitialized', TRUE);
127 $this->signalSlotDispatcher->connect('Foo', 'emitBar', 'NonExistingClassName', 'slot', TRUE);
128 $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array());
129 }
130
131 /**
132 * @test
133 * @expectedException \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
134 */
135 public function dispatchThrowsAnExceptionIfTheSpecifiedSlotMethodDoesNotExist() {
136 $slotClassName = uniqid('Mock_');
137 eval(('class ' . $slotClassName) . ' { function slot($foo, $baz) { $this->arguments = array($foo, $baz); } }');
138 $mockSlot = new $slotClassName();
139 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
140 $mockObjectManager->expects($this->once())->method('isRegistered')->with($slotClassName)->will($this->returnValue(TRUE));
141 $mockObjectManager->expects($this->once())->method('get')->with($slotClassName)->will($this->returnValue($mockSlot));
142 $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
143 $this->signalSlotDispatcher->_set('isInitialized', TRUE);
144 $this->signalSlotDispatcher->connect('Foo', 'emitBar', $slotClassName, 'unknownMethodName', TRUE);
145 $this->signalSlotDispatcher->dispatch('Foo', 'emitBar', array('foo' => 'bar', 'baz' => 'quux'));
146 $this->assertSame($mockSlot->arguments, array('bar', 'quux'));
147 }
148
149 /**
150 * @test
151 */
152 public function dispatchPassesFirstArgumentContainingSlotInformationIfTheConnectionStatesSo() {
153 $arguments = array();
154 $mockSlot = function () use(&$arguments) {
155 ($arguments = func_get_args());
156 };
157 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
158 $this->signalSlotDispatcher->connect('SignalClassName', 'methodName', $mockSlot, NULL, TRUE);
159 $this->signalSlotDispatcher->_set('objectManager', $mockObjectManager);
160 $this->signalSlotDispatcher->_set('isInitialized', TRUE);
161 $this->signalSlotDispatcher->dispatch('SignalClassName', 'methodName', array('foo' => 'bar', 'baz' => 'quux'));
162 $this->assertSame(array('bar', 'quux', 'SignalClassName::methodName'), $arguments);
163 }
164
165 }
166
167
168 ?>