[BUGFIX] Correctly apply Extbase validator options and add tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / ValidatorResolverTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation;
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 text file 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
31 /**
32 * Test case
33 */
34 class ValidatorResolverTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver | \PHPUnit_Framework_MockObject_MockObject | \TYPO3\CMS\Core\Tests\AccessibleObjectInterface
38 */
39 protected $validatorResolver;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Object\ObjectManager | \PHPUnit_Framework_MockObject_MockObject
43 */
44 protected $mockObjectManager;
45
46 public function setUp() {
47 $this->validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
48 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
49 $this->validatorResolver->_set('objectManager', $this->mockObjectManager);
50 }
51
52 /****************/
53
54 /**
55 * @test
56 */
57 public function resolveValidatorObjectNameWithShortHandNotationReturnsValidatorNameIfClassExists() {
58 $extensionName = 'tx_foo';
59 $className = uniqid('Foo');
60 $realClassName = 'Tx_' . $extensionName . '_Validation_Validator_' . $className . 'Validator';
61 $validatorName = $extensionName . ':' . $className;
62 eval('class ' . $realClassName . ' implements TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface {}');
63 $this->assertEquals($realClassName, $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName));
64 }
65
66 /**
67 * @test
68 */
69 public function resolveValidatorObjectNameWithShortHandNotationThrowsExceptionIfClassNotExists() {
70 $className = uniqid('Foo');
71 $validatorName = 'tx_foo:' . $className;
72 $this->setExpectedException('TYPO3\\CMS\\Extbase\\Validation\\Exception\\NoSuchValidatorException', '', 1365799920);
73 $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName);
74 }
75
76 /**
77 * @test
78 */
79 public function resolveValidatorObjectNameWithShortHandNotationReturnsValidatorNameIfClassExistsButDoesNotImplementValidatorInterface() {
80 $extensionName = 'tx_foo';
81 $className = uniqid('Foo');
82 $realClassName = 'Tx_' . $extensionName . '_Validation_Validator_' . $className . 'Validator';
83 $validatorName = $extensionName . ':' . $className;
84 eval('class ' . $realClassName . '{}');
85 $this->setExpectedException('TYPO3\\CMS\\Extbase\\Validation\\Exception\\NoSuchValidatorException', '', 1365776838);
86 $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName);
87 }
88
89 /****************/
90
91 /**
92 * @test
93 */
94 public function resolveValidatorObjectNameReturnsValidatorNameIfClassExists() {
95 $className = uniqid('Foo_');
96 $expectedValidatorName = $className . 'Validator';
97 eval('class ' . $expectedValidatorName . ' implements TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface {}');
98 $this->assertEquals(
99 $expectedValidatorName,
100 $this->validatorResolver->_call('resolveValidatorObjectName', $className)
101 );
102 }
103
104 /**
105 * @test
106 */
107 public function resolveValidatorObjectNameThrowsNoSuchValidatorExceptionIfClassNotExists() {
108 $className = uniqid('Foo');
109 $this->setExpectedException('TYPO3\\CMS\\Extbase\\Validation\\Exception\\NoSuchValidatorException', '', 1365799920);
110 $this->validatorResolver->_call('resolveValidatorObjectName', $className);
111 }
112
113 /**
114 * @test
115 */
116 public function resolveValidatorObjectNameThrowsNoSuchValidatorExceptionIfClassExistsButDoesNotImplementValidatorInterface() {
117 $className = uniqid('Foo_');
118 $expectedValidatorName = $className . 'Validator';
119 eval('class ' . $expectedValidatorName . '{}');
120 $this->setExpectedException('TYPO3\\CMS\\Extbase\\Validation\\Exception\\NoSuchValidatorException', '', 1365776838);
121 $this->validatorResolver->_call('resolveValidatorObjectName', $className);
122 }
123
124 /****************/
125
126 /**
127 * @return array
128 */
129 public function namespacedShorthandValidatornames() {
130 return array(
131 array('TYPO3\\CMS\\Mypkg\\Validation\\Validator', 'MySecondValidator', 'TYPO3.CMS.Mypkg:MySecond'),
132 array('Acme\\Mypkg\\Validation\\Validator', 'MyThirdValidator', 'Acme.Mypkg:MyThird')
133 );
134 }
135
136 /**
137 * @param string $namespace
138 * @param string $className
139 * @param string $shorthandValidatorname
140 *
141 * @test
142 * @dataProvider namespacedShorthandValidatornames
143 */
144 public function resolveValidatorObjectNameCanResolveNamespacedShorthandValidatornames($namespace, $className, $shorthandValidatorname) {
145 eval('namespace ' . $namespace . '; class ' . $className . ' implements \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface {}');
146 $this->assertSame($namespace . '\\' . $className, $this->validatorResolver->_call('resolveValidatorObjectName', $shorthandValidatorname));
147 }
148
149 /**
150 * @test
151 */
152 public function resolveValidatorObjectNameCanResolveShortNamesOfBuiltInValidators() {
153 eval('namespace TYPO3\\CMS\\Extbase\\Validation\\Validator;' . LF . 'class FooValidator implements \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface {}');
154 $this->assertSame('TYPO3\\CMS\\Extbase\\Validation\\Validator\\FooValidator', $this->validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
155 }
156
157 /**
158 * @test
159 */
160 public function createValidatorResolvesAndReturnsAValidatorAndPassesTheGivenOptions() {
161 $className = uniqid('Test');
162 $validatorOptions = array('requiredOption' => 'foo', 'demoOption' => 'bar');
163 $mockValidator = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Tests\\Unit\\Validation\\Validator\\Fixture\\AbstractValidatorClass', array('dummy'), array($validatorOptions), $className);
164 $this->mockObjectManager->expects($this->any())->method('get')->with($className, $validatorOptions)->will($this->returnValue($mockValidator));
165 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName'));
166 $validatorResolver->_set('objectManager', $this->mockObjectManager);
167 $validatorResolver->expects($this->once())->method('resolveValidatorObjectName')->with($className)->will($this->returnValue($className));
168 $validator = $validatorResolver->createValidator($className, $validatorOptions);
169 $this->assertEquals($validatorOptions, $validator->_get('options'));
170 $this->assertSame($mockValidator, $validator);
171 }
172
173 /**
174 * @test
175 */
176 public function createValidatorThrowsNoSuchValidatorExceptionIfAValidatorCouldNotBeResolved() {
177 $this->markTestSkipped('');
178 $className = uniqid('Test');
179 $this->setExpectedException('\\TYPO3\\CMS\\Extbase\\Validation\\Exception\NoSuchValidatorException', '', 1365799920);
180 $this->validatorResolver->createValidator($className);
181 }
182
183 /**
184 * @test
185 */
186 public function getBaseValidatorCachesTheResultOfTheBuildBaseValidatorChainCalls() {
187 $this->markTestSkipped('Functionality is different now.');
188 $mockConjunctionValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
189 $validatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('buildBaseValidatorConjunction'), array(), '', FALSE);
190 $validatorResolver->expects($this->once())->method('buildBaseValidatorConjunction')->with('Tx_Virtual_Foo')->will($this->returnValue($mockConjunctionValidator));
191 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
192 $this->assertSame($mockConjunctionValidator, $result, '#1');
193 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
194 $this->assertSame($mockConjunctionValidator, $result, '#2');
195 }
196
197 /**
198 * @test
199 * @author Sebastian Kurfürst <sbastian@typo3.org>
200 */
201 public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() {
202 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
203 $methodParameters = array();
204 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
205 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
206 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
207 $validatorResolver->_set('reflectionService', $mockReflectionService);
208 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
209 $this->assertSame(array(), $result);
210 }
211
212 /**
213 * @test
214 * @author Robert Lemke <robert@typo3.org>
215 * @author Bastian Waidelich <bastian@typo3.org>
216 */
217 public function buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod() {
218 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
219 $methodParameters = array(
220 'arg1' => array(
221 'type' => 'string'
222 ),
223 'arg2' => array(
224 'type' => 'array'
225 )
226 );
227 $methodTagsValues = array(
228 'param' => array(
229 'string $arg1',
230 'array $arg2'
231 ),
232 'validate' => array(
233 '$arg1 Foo(bar = baz), Bar',
234 '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
235 )
236 );
237 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
238 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
239 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
240 $mockStringValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
241 $mockArrayValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
242 $mockFooValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
243 $mockBarValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
244 $mockQuuxValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
245 $conjunction1 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
246 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
247 $conjunction1->expects($this->at(1))->method('addValidator')->with($mockFooValidator);
248 $conjunction1->expects($this->at(2))->method('addValidator')->with($mockBarValidator);
249 $conjunction2 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
250 $conjunction2->expects($this->at(0))->method('addValidator')->with($mockArrayValidator);
251 $conjunction2->expects($this->at(1))->method('addValidator')->with($mockQuuxValidator);
252 $mockArguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
253 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg1', 'dummyValue'));
254 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg2', 'dummyValue'));
255 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
256 $validatorResolver->expects($this->at(0))->method('createValidator')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($conjunction1));
257 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
258 $validatorResolver->expects($this->at(2))->method('createValidator')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($conjunction2));
259 $validatorResolver->expects($this->at(3))->method('createValidator')->with('array')->will($this->returnValue($mockArrayValidator));
260 $validatorResolver->expects($this->at(4))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockFooValidator));
261 $validatorResolver->expects($this->at(5))->method('createValidator')->with('Bar')->will($this->returnValue($mockBarValidator));
262 $validatorResolver->expects($this->at(6))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
263 $validatorResolver->_set('reflectionService', $mockReflectionService);
264 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
265 $this->assertEquals(array('arg1' => $conjunction1, 'arg2' => $conjunction2), $result);
266 }
267
268 /**
269 * @test
270 * @author Sebastian Kurfürst <sbastian@typo3.org>
271 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException
272 */
273 public function buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists() {
274 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
275 $methodParameters = array(
276 'arg1' => array(
277 'type' => 'string'
278 )
279 );
280 $methodTagsValues = array(
281 'param' => array(
282 'string $arg1'
283 ),
284 'validate' => array(
285 '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
286 )
287 );
288 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
289 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
290 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
291 $mockStringValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
292 $mockQuuxValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
293 $conjunction1 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
294 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
295 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
296 $validatorResolver->expects($this->at(0))->method('createValidator')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($conjunction1));
297 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
298 $validatorResolver->expects($this->at(2))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
299 $validatorResolver->_set('reflectionService', $mockReflectionService);
300 $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
301 }
302
303 /**
304 * @test
305 * @author Robert Lemke <robert@typo3.org>
306 * @author Christopher Hlubek <hlubek@networkteam.com>
307 */
308 public function buildBaseValidatorConjunctionAddsValidatorsDefinedByAnnotationsInTheClassToTheReturnedConjunction() {
309 $mockObject = $this->getMock('stdClass');
310 $className = get_class($mockObject);
311 $propertyTagsValues = array(
312 'foo' => array(
313 'var' => array('string'),
314 'validate' => array(
315 'Foo(bar= baz), Bar',
316 'Baz'
317 )
318 ),
319 'bar' => array(
320 'var' => array('integer'),
321 'validate' => array(
322 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator'
323 )
324 )
325 );
326
327 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
328 $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('foo', 'bar')));
329 $mockReflectionService->expects($this->at(1))->method('getPropertyTagsValues')->with($className, 'foo')->will($this->returnValue($propertyTagsValues['foo']));
330 $mockReflectionService->expects($this->at(2))->method('getPropertyTagsValues')->with($className, 'bar')->will($this->returnValue($propertyTagsValues['bar']));
331 $mockObjectValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator', array('dummy'), array(), '', FALSE);
332 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
333 $mockObjectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator')->will($this->returnValue($mockObjectValidator));
334 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
335 $validatorResolver->_set('reflectionService', $mockReflectionService);
336 $validatorResolver->_set('objectManager', $mockObjectManager);
337 $validatorResolver->expects($this->at(0))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockObjectValidator));
338 $validatorResolver->expects($this->at(1))->method('createValidator')->with('Bar')->will($this->returnValue($mockObjectValidator));
339 $validatorResolver->expects($this->at(2))->method('createValidator')->with('Baz')->will($this->returnValue($mockObjectValidator));
340 $validatorResolver->expects($this->at(3))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator')->will($this->returnValue($mockObjectValidator));
341 $validatorResolver->_call('buildBaseValidatorConjunction', $className, $className);
342 }
343
344 /**
345 * dataProvider for buildBaseValidatorConjunctionAddsValidatorFromConventionToTheReturnedConjunction
346 *
347 * @return array
348 */
349 public function modelNamesProvider() {
350 return array(
351 'no replace' => array('VENDOR\\ModelCollection\\Domain\\Model\\Model', 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator'),
352 'replace in not namespaced class' => array('Tx_ModelCollection_Domain_Model_Model', 'Tx_ModelCollection_Domain_Validator_ModelValidator'),
353 'replace in namespaced class' => array('VENDOR\\ModelCollection\\Domain\\Model\\Model', 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator')
354 );
355 }
356
357 /**
358 * @param string $modelClassName
359 * @param string $validatorClassName
360 *
361 * @test
362 * @dataProvider modelNamesProvider
363 */
364 public function buildBaseValidatorConjunctionCreatesValidatorFromClassName($modelClassName, $validatorClassName) {
365 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
366 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
367 $validatorResolver->_set('objectManager', $mockObjectManager);
368 $validatorResolver->expects($this->once())->method('createValidator')->with($validatorClassName)->will($this->returnValue(NULL));
369 $validatorResolver->_call('buildBaseValidatorConjunction', $modelClassName, $modelClassName);
370 }
371
372 /**
373 * @test
374 * @author Bastian Waidelich <bastian@typo3.org>
375 */
376 public function resolveValidatorObjectNameCallsGetValidatorType() {
377 $validatorName = uniqid('FooValidator');
378 eval('namespace TYPO3\CMS\Extbase\Validation\Validator;' . LF . 'class ' . $validatorName . 'Validator implements \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface {}');
379 $mockValidatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('getValidatorType'));
380 $mockValidatorResolver->expects($this->once())->method('getValidatorType')->with($validatorName)->will($this->returnValue($validatorName));
381
382 $mockValidatorResolver->_call('resolveValidatorObjectName', $validatorName);
383 }
384
385 /**
386 * @test
387 * @author Bastian Waidelich <bastian@typo3.org>
388 */
389 public function getValidatorTypeCorrectlyRenamesPhpDataTypes() {
390 $this->assertEquals('Integer', $this->validatorResolver->_call('getValidatorType', 'integer'));
391 $this->assertEquals('Integer', $this->validatorResolver->_call('getValidatorType', 'int'));
392 $this->assertEquals('String', $this->validatorResolver->_call('getValidatorType', 'string'));
393 $this->assertEquals('Array', $this->validatorResolver->_call('getValidatorType', 'array'));
394 $this->assertEquals('Float', $this->validatorResolver->_call('getValidatorType', 'float'));
395 $this->assertEquals('Float', $this->validatorResolver->_call('getValidatorType', 'double'));
396 $this->assertEquals('Boolean', $this->validatorResolver->_call('getValidatorType', 'boolean'));
397 $this->assertEquals('Boolean', $this->validatorResolver->_call('getValidatorType', 'bool'));
398 $this->assertEquals('Boolean', $this->validatorResolver->_call('getValidatorType', 'bool'));
399 $this->assertEquals('Number', $this->validatorResolver->_call('getValidatorType', 'number'));
400 $this->assertEquals('Number', $this->validatorResolver->_call('getValidatorType', 'numeric'));
401 }
402
403 /**
404 * @test
405 * @author Bastian Waidelich <bastian@typo3.org>
406 */
407 public function getValidatorTypeRenamesMixedToRaw() {
408 $this->assertEquals('Raw', $this->validatorResolver->_call('getValidatorType', 'mixed'));
409 }
410
411 /**
412 * dataProvider for parseValidatorAnnotationCanParseAnnotations
413 *
414 * @author Karsten Dambekalns <karsten@typo3.org>
415 * @author Christopher Hlubek <hlubek@networkteam.com>
416 * @return array
417 */
418 public function validatorAnnotations() {
419 return array(
420 array(
421 '$var Bar',
422 array(
423 'argumentName' => 'var',
424 'validators' => array(
425 array('validatorName' => 'Bar', 'validatorOptions' => array())
426 )
427 )
428 ),
429 array(
430 '$var Bar, Foo',
431 array(
432 'argumentName' => 'var',
433 'validators' => array(
434 array('validatorName' => 'Bar', 'validatorOptions' => array()),
435 array('validatorName' => 'Foo', 'validatorOptions' => array())
436 )
437 )
438 ),
439 array(
440 '$var Baz (Foo=Bar)',
441 array(
442 'argumentName' => 'var',
443 'validators' => array(
444 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'Bar'))
445 )
446 )
447 ),
448 array(
449 '$var Buzz (Foo="B=a, r", Baz=1)',
450 array(
451 'argumentName' => 'var',
452 'validators' => array(
453 array('validatorName' => 'Buzz', 'validatorOptions' => array('Foo' => 'B=a, r', 'Baz' => '1'))
454 )
455 )
456 ),
457 array(
458 '$var Foo(Baz=1, Bar=Quux)',
459 array(
460 'argumentName' => 'var',
461 'validators' => array(
462 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))
463 )
464 )
465 ),
466 array(
467 '$var Pax, Foo(Baz = \'1\', Bar = Quux)',
468 array(
469 'argumentName' => 'var',
470 'validators' => array(
471 array('validatorName' => 'Pax', 'validatorOptions' => array()),
472 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))
473 )
474 )
475 ),
476 array(
477 '$var Reg (P="[at]*(h|g)"), Quux',
478 array(
479 'argumentName' => 'var',
480 'validators' => array(
481 array('validatorName' => 'Reg', 'validatorOptions' => array('P' => '[at]*(h|g)')),
482 array('validatorName' => 'Quux', 'validatorOptions' => array())
483 )
484 )
485 ),
486 array(
487 '$var Baz (Foo="B\\"ar")',
488 array(
489 'argumentName' => 'var',
490 'validators' => array(
491 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'B"ar'))
492 )
493 )
494 ),
495 array(
496 '$var F3_TestPackage_Quux',
497 array(
498 'argumentName' => 'var',
499 'validators' => array(
500 array('validatorName' => 'F3_TestPackage_Quux', 'validatorOptions' => array())
501 )
502 )
503 ),
504 array(
505 '$var Baz(Foo="5"), Bar(Quux="123")',
506 array(
507 'argumentName' => 'var',
508 'validators' => array(
509 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '5')),
510 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123'))
511 )
512 )
513 ),
514 array(
515 '$var Baz(Foo="2"), Bar(Quux=123, Pax="a weird \\"string\\" with *freaky* \\stuff")',
516 array(
517 'argumentName' => 'var',
518 'validators' => array(
519 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '2')),
520 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123', 'Pax' => 'a weird "string" with *freaky* \\stuff'))
521 )
522 )
523 ),
524 'namespaced validator class name' => array(
525 'annotation' => '$var F3\TestPackage\Quux',
526 'expected' => array(
527 'argumentName' => 'var',
528 'validators' => array(
529 array('validatorName' => 'F3\TestPackage\Quux', 'validatorOptions' => array())
530 )
531 )
532 ),
533 'shorthand notation for system validator' => array(
534 'annotation' => '$var TYPO3.CMS.Mypkg:MySecond',
535 'expected' => array(
536 'argumentName' => 'var',
537 'validators' => array(
538 array('validatorName' => 'TYPO3.CMS.Mypkg:MySecond', 'validatorOptions' => array())
539 )
540 )
541 ),
542 'shorthand notation for custom validator with parameter' => array(
543 'annotation' => '$var Acme.Mypkg:MyThird(Foo="2")',
544 'expected' => array(
545 'argumentName' => 'var',
546 'validators' => array(
547 array('validatorName' => 'Acme.Mypkg:MyThird', 'validatorOptions' => array('Foo' => '2'))
548 )
549 )
550 ),
551 );
552 }
553
554 /**
555 * @test
556 * @dataProvider validatorAnnotations
557 * @author Karsten Dambekalns <karsten@typo3.org>
558 * @param mixed $annotation
559 * @param mixed $expectedResult
560 */
561 public function parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult) {
562 $result = $this->validatorResolver->_call('parseValidatorAnnotation', $annotation);
563 $this->assertEquals($result, $expectedResult);
564 }
565 }