[TASK] Replace inject methods with @inject
[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 = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Argument', array('dummy'), array('someName', 'string'));
52 $this->objectArgument = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Argument', array('dummy'), array('someName', 'DateTime'));
53 $this->mockPropertyMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMapper');
54 $this->simpleValueArgument->_set('propertyMapper', $this->mockPropertyMapper);
55 $this->objectArgument->_set('propertyMapper', $this->mockPropertyMapper);
56 $this->mockConfiguration = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
57 $propertyMappingConfiguranion = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
58 $this->simpleValueArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguranion);
59 $this->objectArgument->_set('propertyMappingConfiguration', $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->simpleValueArgument = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Argument', array('dummy'), array('dummy', 'string'));
177 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
178 $this->simpleValueArgument->setValue(NULL);
179 $this->assertNull($this->simpleValueArgument->getValue());
180 }
181
182 /**
183 * @return array
184 */
185 public function validClassNameDataTypes() {
186 return array(
187 array('Tx_Foo_Bar'),
188 array('ExtbaseTeam\\BlogExample\\Foo\\Bar'),
189 );
190 }
191
192 /**
193 * @test
194 * @dataProvider validClassNameDataTypes
195 */
196 public function classSchemaIsBuiltForClassNameDataTypesWhenReflectionServiceIsInjected($dataType) {
197 /** @var $argument \TYPO3\CMS\Extbase\Mvc\Controller\Argument|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
198 $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('dummy'), array(), '', FALSE);
199 $argument->_set('dataType', $dataType);
200 $reflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
201 $reflectionService->expects($this->once())->method('getClassSchema')->with($dataType);
202 $argument->injectReflectionService($reflectionService);
203 }
204
205 /**
206 * @return array
207 */
208 public function primitiveOrBuiltInObjectsDataTypes() {
209 return array(
210 array('integer'),
211 array('array'),
212 array('DateTime'),
213 );
214 }
215
216 /**
217 * @test
218 * @dataProvider primitiveOrBuiltInObjectsDataTypes
219 */
220 public function classSchemaIsNotBuiltForPrimitiveDataTypesWhenReflectionServiceIsInjected($dataType) {
221 /** @var $argument \TYPO3\CMS\Extbase\Mvc\Controller\Argument|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
222 $argument = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('dummy'), array(), '', FALSE);
223 $argument->_set('dataType', $dataType);
224 $reflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
225 $reflectionService->expects($this->never())->method('getClassSchema');
226 $argument->injectReflectionService($reflectionService);
227 }
228
229 /**
230 * @test
231 * @author Karsten Dambekalns <karsten@typo3.org>
232 */
233 public function setValueUsesMatchingInstanceAsIs() {
234 $this->enableRewrittenPropertyMapperInArgument($this->objectArgument);
235 $this->mockPropertyMapper->expects($this->never())->method('convert');
236 $this->objectArgument->setValue(new \DateTime());
237 }
238
239 /**
240 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
241 */
242 protected function setupPropertyMapperAndSetValue() {
243 $this->mockPropertyMapper->expects($this->once())->method('convert')->with('someRawValue', 'string', $this->mockConfiguration)->will($this->returnValue('convertedValue'));
244 $this->mockPropertyMapper->expects($this->once())->method('getMessages')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
245 return $this->simpleValueArgument->setValue('someRawValue');
246 }
247
248 /**
249 * @test
250 * @author Sebastian Kurfürst <sebastian@typo3.org>
251 */
252 public function setValueShouldCallPropertyMapperCorrectlyAndStoreResultInValue() {
253 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
254 $this->setupPropertyMapperAndSetValue();
255 $this->assertSame('convertedValue', $this->simpleValueArgument->getValue());
256 $this->assertTrue($this->simpleValueArgument->isValid());
257 }
258
259 /**
260 * @test
261 * @author Sebastian Kurfürst <sebastian@typo3.org>
262 */
263 public function setValueShouldBeFluentInterface() {
264 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
265 $this->assertSame($this->simpleValueArgument, $this->setupPropertyMapperAndSetValue());
266 }
267
268 /**
269 * @test
270 * @author Sebastian Kurfürst <sebastian@typo3.org>
271 */
272 public function setValueShouldSetValidationErrorsIfValidatorIsSetAndValidationFailed() {
273 $this->enableRewrittenPropertyMapperInArgument($this->simpleValueArgument);
274 $error = new \TYPO3\CMS\Extbase\Error\Error('Some Error', 1234);
275 $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
276 $validationMessages = new \TYPO3\CMS\Extbase\Error\Result();
277 $validationMessages->addError($error);
278 $mockValidator->expects($this->once())->method('validate')->with('convertedValue')->will($this->returnValue($validationMessages));
279 $this->simpleValueArgument->setValidator($mockValidator);
280 $this->setupPropertyMapperAndSetValue();
281 $this->assertFalse($this->simpleValueArgument->isValid());
282 $this->assertEquals(array($error), $this->simpleValueArgument->getValidationResults()->getErrors());
283 }
284
285 /**
286 * @test
287 * @author Sebastian Kurfürst <sebastian@typo3.org>
288 */
289 public function defaultPropertyMappingConfigurationDoesNotAllowCreationOrModificationOfObjects() {
290 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
291 $this->assertNull($this->simpleValueArgument->getPropertyMappingConfiguration()->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
292 }
293
294 /**
295 * Helper which enables the new property mapper in the Argument class.
296 *
297 * @param \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument
298 */
299 protected function enableRewrittenPropertyMapperInArgument(\TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument) {
300 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
301 $mockConfigurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(TRUE));
302 $argument->_set('configurationManager', $mockConfigurationManager);
303 }
304 }
305
306 ?>