7ca4c207f2987a39b8aa8be4020b3537d55e817c
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / ArgumentTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
3
4 /* *
5 * This script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * Testcase for the MVC Controller Argument
25 *
26 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
27 * @covers \TYPO3\CMS\Extbase\Mvc\Controller\Argument
28 */
29 class ArgumentTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
33 */
34 protected $simpleValueArgument;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument
38 */
39 protected $objectArgument;
40
41 protected $mockPropertyMapper;
42
43 protected $mockConfigurationBuilder;
44
45 protected $mockConfiguration;
46
47 /**
48 * @author Sebastian Kurfürst <sebastian@typo3.org>
49 */
50 public function setUp() {
51 $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('someName', 'string');
52 $this->objectArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('someName', 'DateTime');
53 $this->mockPropertyMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMapper');
54 $this->simpleValueArgument->injectPropertyMapper($this->mockPropertyMapper);
55 $this->objectArgument->injectPropertyMapper($this->mockPropertyMapper);
56 $this->mockConfiguration = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
57 $propertyMappingConfiguranion = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
58 $this->simpleValueArgument->injectPropertyMappingConfiguration($propertyMappingConfiguranion);
59 $this->objectArgument->injectPropertyMappingConfiguration($propertyMappingConfiguranion);
60 }
61
62 /**
63 * @test
64 * @author Robert Lemke <robert@typo3.org>
65 * @expectedException \InvalidArgumentException
66 */
67 public function constructingArgumentWithoutNameThrowsException() {
68 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('', 'Text');
69 }
70
71 /**
72 * @test
73 * @expectedException \InvalidArgumentException
74 * @author Robert Lemke <robert@typo3.org>
75 */
76 public function constructingArgumentWithInvalidNameThrowsException() {
77 new \TYPO3\CMS\Extbase\Mvc\Controller\Argument(new \ArrayObject(), 'Text');
78 }
79
80 /**
81 * @test
82 * @author Robert Lemke <robert@typo3.org>
83 */
84 public function passingDataTypeToConstructorReallySetsTheDataType() {
85 $this->assertEquals('string', $this->simpleValueArgument->getDataType(), 'The specified data type has not been set correctly.');
86 $this->assertEquals('someName', $this->simpleValueArgument->getName(), 'The specified name has not been set correctly.');
87 }
88
89 /**
90 * @test
91 * @author Robert Lemke <robert@typo3.org>
92 */
93 public function setShortNameProvidesFluentInterface() {
94 $returnedArgument = $this->simpleValueArgument->setShortName('x');
95 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
96 }
97
98 /**
99 * @return array
100 */
101 public function invalidShortNames() {
102 return array(
103 array(''),
104 array('as'),
105 array(5)
106 );
107 }
108
109 /**
110 * @test
111 * @dataProvider invalidShortNames
112 * @expectedException \InvalidArgumentException
113 * @author Sebastian Kurfürst <sebastian@typo3.org>
114 * @param string $invalidShortName
115 */
116 public function shortNameShouldThrowExceptionIfInvalid($invalidShortName) {
117 $this->simpleValueArgument->setShortName($invalidShortName);
118 }
119
120 /**
121 * @test
122 * @author Sebastian Kurfürst <sebastian@typo3.org>
123 */
124 public function shortNameCanBeRetrievedAgain() {
125 $this->simpleValueArgument->setShortName('x');
126 $this->assertEquals('x', $this->simpleValueArgument->getShortName());
127 }
128
129 /**
130 * @test
131 * @author Sebastian Kurfürst <sebastian@typo3.org>
132 */
133 public function setRequiredShouldProvideFluentInterfaceAndReallySetRequiredState() {
134 $returnedArgument = $this->simpleValueArgument->setRequired(TRUE);
135 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
136 $this->assertTrue($this->simpleValueArgument->isRequired());
137 }
138
139 /**
140 * @test
141 * @author Sebastian Kurfürst <sebastian@typo3.org>
142 */
143 public function setDefaultValueShouldProvideFluentInterfaceAndReallySetDefaultValue() {
144 $returnedArgument = $this->simpleValueArgument->setDefaultValue('default');
145 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
146 $this->assertSame('default', $this->simpleValueArgument->getDefaultValue());
147 }
148
149 /**
150 * @test
151 * @author Sebastian Kurfürst <sebastian@typo3.org>
152 */
153 public function setValidatorShouldProvideFluentInterfaceAndReallySetValidator() {
154 $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface');
155 $returnedArgument = $this->simpleValueArgument->setValidator($mockValidator);
156 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
157 $this->assertSame($mockValidator, $this->simpleValueArgument->getValidator());
158 }
159
160 /**
161 * @test
162 * @author Robert Lemke <robert@typo3.org>
163 */
164 public function setValueProvidesFluentInterface() {
165 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
166 $returnedArgument = $this->simpleValueArgument->setValue(NULL);
167 $this->assertSame($this->simpleValueArgument, $returnedArgument, 'The returned argument is not the original argument.');
168 }
169
170 /**
171 * @test
172 * @author Karsten Dambekalns <karsten@typo3.org>
173 */
174 public function setValueUsesNullAsIs() {
175 $this->simpleValueArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('dummy', 'string');
176 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
177 $this->simpleValueArgument->setValue(NULL);
178 $this->assertNull($this->simpleValueArgument->getValue());
179 }
180
181 /**
182 * @return array
183 */
184 public function validClassNameDataTypes() {
185 return array(
186 array('Tx_Foo_Bar'),
187 array('ExtbaseTeam\\BlogExample\\Foo\\Bar'),
188 );
189 }
190
191 /**
192 * @test
193 * @dataProvider validClassNameDataTypes
194 */
195 public function classSchemaIsBuiltForClassNameDataTypesWhenReflectionServiceIsInjected($dataType) {
196 /** @var $argument \TYPO3\CMS\Extbase\Mvc\Controller\Argument|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
197 $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('dummy'), array(), '', FALSE);
198 $argument->_set('dataType', $dataType);
199 $reflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
200 $reflectionService->expects($this->once())->method('getClassSchema')->with($dataType);
201 $argument->injectReflectionService($reflectionService);
202 }
203
204 /**
205 * @return array
206 */
207 public function primitiveOrBuiltInObjectsDataTypes() {
208 return array(
209 array('integer'),
210 array('array'),
211 array('DateTime'),
212 );
213 }
214
215 /**
216 * @test
217 * @dataProvider primitiveOrBuiltInObjectsDataTypes
218 */
219 public function classSchemaIsNotBuiltForPrimitiveDataTypesWhenReflectionServiceIsInjected($dataType) {
220 /** @var $argument \TYPO3\CMS\Extbase\Mvc\Controller\Argument|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
221 $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('dummy'), array(), '', FALSE);
222 $argument->_set('dataType', $dataType);
223 $reflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
224 $reflectionService->expects($this->never())->method('getClassSchema');
225 $argument->injectReflectionService($reflectionService);
226 }
227
228 /**
229 * @test
230 * @author Karsten Dambekalns <karsten@typo3.org>
231 */
232 public function setValueUsesMatchingInstanceAsIs() {
233 $this->enableRewrittenPropertyMapperInArgument($this->objectArgument);
234 $this->mockPropertyMapper->expects($this->never())->method('convert');
235 $this->objectArgument->setValue(new \DateTime());
236 }
237
238 /**
239 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
240 */
241 protected function setupPropertyMapperAndSetValue() {
242 $this->mockPropertyMapper->expects($this->once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->will($this->returnValue('convertedValue'));
243 $this->mockPropertyMapper->expects($this->once())->method('getMessages')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
244 return $this->simpleValueArgument->setValue('someRawValue');
245 }
246
247 /**
248 * @test
249 * @author Sebastian Kurfürst <sebastian@typo3.org>
250 */
251 public function setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue() {
252 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
253 $this->setupPropertyMapperAndSetValue();
254 $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
255 $this->assertTrue($this->simpleValueArgument->isValid());
256 }
257
258 /**
259 * @test
260 * @author Sebastian Kurfürst <sebastian@typo3.org>
261 */
262 public function setValueShouldBeFluentInterface() {
263 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
264 $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
265 }
266
267 /**
268 * @test
269 * @author Sebastian Kurfürst <sebastian@typo3.org>
270 */
271 public function setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed() {
272 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
273 $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
274 $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
275 $validationMessages = new \TYPO3\CMS\Extbase\Error\Result();
276 $validationMessages->addError($error);
277 $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages));
278 $this->simpleValueArgument->setValidator($mockValidator);
279 $this->setupPropertyMapperAndSetValue();
280 $this->assertFalse($this->simpleValueArgument->isValid());
281 $this->assertEquals(array($error), $this->simpleValueArgument->getValidationResults()->getErrors());
282 }
283
284 /**
285 * @test
286 * @author Sebastian Kurfürst <sebastian@typo3.org>
287 */
288 public function defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects() {
289 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
290 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
291 }
292
293 /**
294 * Helper which enables the new property mapper in the Argument class.
295 *
296 * @param \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument
297 */
298 protected function enableRewrittenPropertyMapperInArgument(\TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument) {
299 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
300 $mockConfigurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(TRUE));
301 $argument->injectConfigurationManager($mockConfigurationManager);
302 }
303 }
304
305 ?>