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