[TASK] Shorten the extend class in all unit tests
[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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Test case
21 */
22 class ArgumentsTest extends UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function argumentsObjectIsOfScopePrototype()
28 {
29 $arguments1 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
30 $arguments2 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
31 $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
32 }
33
34 /**
35 * @test
36 */
37 public function addingAnArgumentManuallyWorks()
38 {
39 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
40 $newArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('argumentName1234', 'dummyValue');
41 $arguments->addArgument($newArgument);
42 $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
43 }
44
45 /**
46 * @test
47 */
48 public function addingAnArgumentReplacesArgumentWithSameName()
49 {
50 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
51 $mockFirstArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
52 ->setMethods(['getName'])
53 ->disableOriginalConstructor()
54 ->getMock();
55 $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
56 $arguments->addArgument($mockFirstArgument);
57 $mockSecondArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
58 ->setMethods(['getName'])
59 ->disableOriginalConstructor()
60 ->getMock();
61 $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
62 $arguments->addArgument($mockSecondArgument);
63 $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
64 }
65
66 /**
67 * @test
68 */
69 public function addNewArgumentProvidesFluentInterface()
70 {
71 $mockArgument = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class);
72 $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
73 $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
74 $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
75 $arguments->_set('objectManager', $mockObjectManager);
76 $newArgument = $arguments->addNewArgument('someArgument');
77 $this->assertSame($newArgument, $mockArgument);
78 }
79
80 /**
81 * @test
82 */
83 public function addingArgumentThroughArrayAccessWorks()
84 {
85 $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
86 ->setMethods(['getName'])
87 ->disableOriginalConstructor()
88 ->getMock();
89 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
90 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
91 $arguments[] = $mockArgument;
92 $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
93 $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
94 }
95
96 /**
97 * @test
98 */
99 public function retrievingArgumentThroughArrayAccessWorks()
100 {
101 $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
102 ->setMethods(['getName'])
103 ->disableOriginalConstructor()
104 ->getMock();
105 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
106 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
107 $arguments[] = $mockArgument;
108 $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
109 }
110
111 /**
112 * @test
113 */
114 public function getArgumentWithNonExistingArgumentNameThrowsException()
115 {
116 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
117 try {
118 $arguments->getArgument('someArgument');
119 $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
120 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException $exception) {
121 }
122 }
123
124 /**
125 * @test
126 */
127 public function issetReturnsCorrectResult()
128 {
129 $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
130 ->setMethods(['getName'])
131 ->disableOriginalConstructor()
132 ->getMock();
133 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
134 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
135 $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
136 $arguments[] = $mockArgument;
137 $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
138 }
139
140 /**
141 * @test
142 */
143 public function getArgumentNamesReturnsNamesOfAddedArguments()
144 {
145 $mockArgument1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
146 ->setMethods(['getName'])
147 ->disableOriginalConstructor()
148 ->getMock();
149 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
150 $mockArgument2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
151 ->setMethods(['getName'])
152 ->disableOriginalConstructor()
153 ->getMock();
154 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
155 $mockArgument3 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
156 ->setMethods(['getName'])
157 ->disableOriginalConstructor()
158 ->getMock();
159 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
160 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
161 $arguments[] = $mockArgument1;
162 $arguments[] = $mockArgument2;
163 $arguments[] = $mockArgument3;
164 $expectedArgumentNames = ['argumentName1', 'argumentName2', 'argumentName3'];
165 $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
166 }
167
168 /**
169 * @test
170 */
171 public function getArgumentShortNamesReturnsShortNamesOfAddedArguments()
172 {
173 $mockArgument1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
174 ->setMethods(['getName', 'getShortName'])
175 ->disableOriginalConstructor()
176 ->getMock();
177 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
178 $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
179 $mockArgument2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
180 ->setMethods(['getName', 'getShortName'])
181 ->disableOriginalConstructor()
182 ->getMock();
183 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
184 $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
185 $mockArgument3 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
186 ->setMethods(['getName', 'getShortName'])
187 ->disableOriginalConstructor()
188 ->getMock();
189 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
190 $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
191 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
192 $arguments[] = $mockArgument1;
193 $arguments[] = $mockArgument2;
194 $arguments[] = $mockArgument3;
195 $expectedShortNames = ['a', 'b', 'c'];
196 $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
197 }
198
199 /**
200 * @test
201 */
202 public function addNewArgumentCreatesAndAddsNewArgument()
203 {
204 $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
205 ->setMethods(['getName'])
206 ->disableOriginalConstructor()
207 ->getMock();
208 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
209 $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
210 $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
211 $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
212 $arguments->_set('objectManager', $mockObjectManager);
213 $addedArgument = $arguments->addNewArgument('dummyName');
214 $this->assertInstanceOf(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
215 $retrievedArgument = $arguments['dummyName'];
216 $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
217 }
218
219 /**
220 * @test
221 */
222 public function addNewArgumentAssumesTextDataTypeByDefault()
223 {
224 $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
225 ->setMethods(['getName'])
226 ->disableOriginalConstructor()
227 ->getMock();
228 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
229 $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
230 $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
231 $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
232 $arguments->_set('objectManager', $mockObjectManager);
233 $arguments->addNewArgument('dummyName');
234 }
235
236 /**
237 * @test
238 */
239 public function addNewArgumentCanAddArgumentsMarkedAsRequired()
240 {
241 $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
242 ->setMethods(['getName', 'setRequired'])
243 ->disableOriginalConstructor()
244 ->getMock();
245 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
246 $mockArgument->expects($this->once())->method('setRequired')->with(true);
247 $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
248 $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
249 $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
250 $arguments->_set('objectManager', $mockObjectManager);
251 $arguments->addNewArgument('dummyName', 'Text', true);
252 }
253
254 /**
255 * @test
256 */
257 public function addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues()
258 {
259 $mockArgument = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
260 ->setMethods(['getName', 'setRequired', 'setDefaultValue'])
261 ->disableOriginalConstructor()
262 ->getMock();
263 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
264 $mockArgument->expects($this->once())->method('setRequired')->with(false);
265 $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
266 $mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
267 $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, 'dummyName', 'Text')->will($this->returnValue($mockArgument));
268 $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
269 $arguments->_set('objectManager', $mockObjectManager);
270 $arguments->addNewArgument('dummyName', 'Text', false, 'someDefaultValue');
271 }
272
273 /**
274 * @test
275 */
276 public function callingInvalidMethodThrowsException()
277 {
278 $this->expectException(\LogicException::class);
279 $this->expectExceptionCode(1210858451);
280 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
281 $arguments->nonExistingMethod();
282 }
283
284 /**
285 * @test
286 */
287 public function removeAllClearsAllArguments()
288 {
289 $mockArgument1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
290 ->setMethods(['getName', 'getShortName'])
291 ->disableOriginalConstructor()
292 ->getMock();
293 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
294 $mockArgument2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)
295 ->setMethods(['getName', 'getShortName'])
296 ->disableOriginalConstructor()
297 ->getMock();
298 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
299 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
300 $arguments[] = $mockArgument1;
301 $arguments[] = $mockArgument2;
302 $this->assertTrue($arguments->hasArgument('argumentName2'));
303 $arguments->removeAll();
304 $this->assertFalse($arguments->hasArgument('argumentName2'));
305 }
306 }