[CLEANUP] Adjust code to coding guidelines
[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 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 class ArgumentsTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
31
32 /**
33 * @test
34 */
35 public function argumentsObjectIsOfScopePrototype() {
36 $arguments1 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
37 $arguments2 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
38 $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
39 }
40
41 /**
42 * @test
43 */
44 public function addingAnArgumentManuallyWorks() {
45 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
46 $newArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('argumentName1234', 'dummyValue');
47 $arguments->addArgument($newArgument);
48 $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
49 }
50
51 /**
52 * @test
53 */
54 public function addingAnArgumentReplacesArgumentWithSameName() {
55 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
56 $mockFirstArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
57 $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
58 $arguments->addArgument($mockFirstArgument);
59 $mockSecondArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
60 $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
61 $arguments->addArgument($mockSecondArgument);
62 $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
63 }
64
65 /**
66 * @test
67 */
68 public function addNewArgumentProvidesFluentInterface() {
69 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array(), array(), '', FALSE);
70 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
71 $mockObjectManager->expects($this->once())->method('create')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument')->will($this->returnValue($mockArgument));
72 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
73 $arguments->injectObjectManager($mockObjectManager);
74 $newArgument = $arguments->addNewArgument('someArgument');
75 $this->assertSame($newArgument, $mockArgument);
76 }
77
78 /**
79 * @test
80 */
81 public function addingArgumentThroughArrayAccessWorks() {
82 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
83 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
84 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
85 $arguments[] = $mockArgument;
86 $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
87 $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
88 }
89
90 /**
91 * @test
92 */
93 public function retrievingArgumentThroughArrayAccessWorks() {
94 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
95 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
96 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
97 $arguments[] = $mockArgument;
98 $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
99 }
100
101 /**
102 * @test
103 */
104 public function getArgumentWithNonExistingArgumentNameThrowsException() {
105 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
106 try {
107 $arguments->getArgument('someArgument');
108 $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
109 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException $exception) {
110 }
111 }
112
113 /**
114 * @test
115 */
116 public function issetReturnsCorrectResult() {
117 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
118 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
119 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
120 $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
121 $arguments[] = $mockArgument;
122 $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
123 }
124
125 /**
126 * @test
127 */
128 public function getArgumentNamesReturnsNamesOfAddedArguments() {
129 $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
130 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
131 $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
132 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
133 $mockArgument3 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
134 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
135 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
136 $arguments[] = $mockArgument1;
137 $arguments[] = $mockArgument2;
138 $arguments[] = $mockArgument3;
139 $expectedArgumentNames = array('argumentName1', 'argumentName2', 'argumentName3');
140 $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
141 }
142
143 /**
144 * @test
145 */
146 public function getArgumentShortNamesReturnsShortNamesOfAddedArguments() {
147 $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
148 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
149 $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
150 $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
151 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
152 $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
153 $mockArgument3 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
154 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
155 $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
156 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
157 $arguments[] = $mockArgument1;
158 $arguments[] = $mockArgument2;
159 $arguments[] = $mockArgument3;
160 $expectedShortNames = array('a', 'b', 'c');
161 $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
162 }
163
164 /**
165 * @test
166 */
167 public function addNewArgumentCreatesAndAddsNewArgument() {
168 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
169 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
170 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
171 $mockObjectManager->expects($this->once())->method('create')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument')->will($this->returnValue($mockArgument));
172 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
173 $arguments->injectObjectManager($mockObjectManager);
174 $addedArgument = $arguments->addNewArgument('dummyName');
175 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
176 $retrievedArgument = $arguments['dummyName'];
177 $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
178 }
179
180 /**
181 * @test
182 */
183 public function addNewArgumentAssumesTextDataTypeByDefault() {
184 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
185 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
186 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
187 $mockObjectManager->expects($this->once())->method('create')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
188 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
189 $arguments->injectObjectManager($mockObjectManager);
190 $arguments->addNewArgument('dummyName');
191 }
192
193 /**
194 * @test
195 */
196 public function addNewArgumentCanAddArgumentsMarkedAsRequired() {
197 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'setRequired'), array(), '', FALSE);
198 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
199 $mockArgument->expects($this->once())->method('setRequired')->with(TRUE);
200 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
201 $mockObjectManager->expects($this->once())->method('create')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
202 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
203 $arguments->injectObjectManager($mockObjectManager);
204 $arguments->addNewArgument('dummyName', 'Text', TRUE);
205 }
206
207 /**
208 * @test
209 */
210 public function addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues() {
211 $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'setRequired', 'setDefaultValue'), array(), '', FALSE);
212 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
213 $mockArgument->expects($this->once())->method('setRequired')->with(FALSE);
214 $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
215 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
216 $mockObjectManager->expects($this->once())->method('create')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
217 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
218 $arguments->injectObjectManager($mockObjectManager);
219 $arguments->addNewArgument('dummyName', 'Text', FALSE, 'someDefaultValue');
220 }
221
222 /**
223 * @test
224 * @expectedException \LogicException
225 * @author Bastian Waidelich <bastian@typo3.org>
226 */
227 public function callingInvalidMethodThrowsException() {
228 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
229 $arguments->nonExistingMethod();
230 }
231
232 /**
233 * @test
234 * @author Christopher Hlubek <hlubek@networkteam.com>
235 */
236 public function removeAllClearsAllArguments() {
237 $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
238 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
239 $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
240 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
241 $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
242 $arguments[] = $mockArgument1;
243 $arguments[] = $mockArgument2;
244 $this->assertTrue($arguments->hasArgument('argumentName2'));
245 $arguments->removeAll();
246 $this->assertFalse($arguments->hasArgument('argumentName2'));
247 }
248 }
249
250 ?>