[TASK] Remove unused use statements
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / ArgumentsTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
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
17 /**
18 * Test case
19 */
20 class ArgumentsTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @test
24 */
25 public function argumentsObjectIsOfScopePrototype() {
26 $arguments1 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
27 $arguments2 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
28 $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
29 }
30
31 /**
32 * @test
33 */
34 public function addingAnArgumentManuallyWorks() {
35 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
36 $newArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('argumentName1234', 'dummyValue');
37 $arguments->addArgument($newArgument);
38 $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
39 }
40
41 /**
42 * @test
43 */
44 public function addingAnArgumentReplacesArgumentWithSameName() {
45 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
46 $mockFirstArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
47 $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
48 $arguments->addArgument($mockFirstArgument);
49 $mockSecondArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
50 $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
51 $arguments->addArgument($mockSecondArgument);
52 $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
53 }
54
55 /**
56 * @test
57 */
58 public function addNewArgumentProvidesFluentInterface() {
59 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array(), array(), '', FALSE);
60 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
61 $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument')->will($this->returnValue($mockArgument));
62 $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
63 $arguments->_set('objectManager', $mockObjectManager);
64 $newArgument = $arguments->addNewArgument('someArgument');
65 $this->assertSame($newArgument, $mockArgument);
66 }
67
68 /**
69 * @test
70 */
71 public function addingArgumentThroughArrayAccessWorks() {
72 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
73 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
74 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
75 $arguments[] = $mockArgument;
76 $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
77 $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
78 }
79
80 /**
81 * @test
82 */
83 public function retrievingArgumentThroughArrayAccessWorks() {
84 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
85 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
86 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
87 $arguments[] = $mockArgument;
88 $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
89 }
90
91 /**
92 * @test
93 */
94 public function getArgumentWithNonExistingArgumentNameThrowsException() {
95 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
96 try {
97 $arguments->getArgument('someArgument');
98 $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
99 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException $exception) {
100 }
101 }
102
103 /**
104 * @test
105 */
106 public function issetReturnsCorrectResult() {
107 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
108 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
109 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
110 $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
111 $arguments[] = $mockArgument;
112 $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
113 }
114
115 /**
116 * @test
117 */
118 public function getArgumentNamesReturnsNamesOfAddedArguments() {
119 $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
120 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
121 $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
122 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
123 $mockArgument3 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
124 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
125 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
126 $arguments[] = $mockArgument1;
127 $arguments[] = $mockArgument2;
128 $arguments[] = $mockArgument3;
129 $expectedArgumentNames = array('argumentName1', 'argumentName2', 'argumentName3');
130 $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
131 }
132
133 /**
134 * @test
135 */
136 public function getArgumentShortNamesReturnsShortNamesOfAddedArguments() {
137 $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
138 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
139 $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
140 $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
141 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
142 $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
143 $mockArgument3 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
144 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
145 $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
146 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
147 $arguments[] = $mockArgument1;
148 $arguments[] = $mockArgument2;
149 $arguments[] = $mockArgument3;
150 $expectedShortNames = array('a', 'b', 'c');
151 $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
152 }
153
154 /**
155 * @test
156 */
157 public function addNewArgumentCreatesAndAddsNewArgument() {
158 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
159 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
160 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
161 $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument')->will($this->returnValue($mockArgument));
162 $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
163 $arguments->_set('objectManager', $mockObjectManager);
164 $addedArgument = $arguments->addNewArgument('dummyName');
165 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
166 $retrievedArgument = $arguments['dummyName'];
167 $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
168 }
169
170 /**
171 * @test
172 */
173 public function addNewArgumentAssumesTextDataTypeByDefault() {
174 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
175 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
176 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
177 $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
178 $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
179 $arguments->_set('objectManager', $mockObjectManager);
180 $arguments->addNewArgument('dummyName');
181 }
182
183 /**
184 * @test
185 */
186 public function addNewArgumentCanAddArgumentsMarkedAsRequired() {
187 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'setRequired'), array(), '', FALSE);
188 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
189 $mockArgument->expects($this->once())->method('setRequired')->with(TRUE);
190 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
191 $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
192 $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
193 $arguments->_set('objectManager', $mockObjectManager);
194 $arguments->addNewArgument('dummyName', 'Text', TRUE);
195 }
196
197 /**
198 * @test
199 */
200 public function addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues() {
201 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'setRequired', 'setDefaultValue'), array(), '', FALSE);
202 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
203 $mockArgument->expects($this->once())->method('setRequired')->with(FALSE);
204 $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
205 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
206 $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
207 $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
208 $arguments->_set('objectManager', $mockObjectManager);
209 $arguments->addNewArgument('dummyName', 'Text', FALSE, 'someDefaultValue');
210 }
211
212 /**
213 * @test
214 * @expectedException \LogicException
215 * @author Bastian Waidelich <bastian@typo3.org>
216 */
217 public function callingInvalidMethodThrowsException() {
218 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
219 $arguments->nonExistingMethod();
220 }
221
222 /**
223 * @test
224 * @author Christopher Hlubek <hlubek@networkteam.com>
225 */
226 public function removeAllClearsAllArguments() {
227 $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
228 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
229 $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
230 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
231 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
232 $arguments[] = $mockArgument1;
233 $arguments[] = $mockArgument2;
234 $this->assertTrue($arguments->hasArgument('argumentName2'));
235 $arguments->removeAll();
236 $this->assertFalse($arguments->hasArgument('argumentName2'));
237 }
238 }