11c0d0007d4d2f80a5447592fc1898a7f4f15b25
[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 $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ObjectValidatorInterface', array('setOptions', 'canValidate', 'isPropertyValid', 'setValidatedInstancesContainer'), array(), $className);
163 $mockValidator->expects($this->once())->method('setOptions')->with(array('foo' => 'bar'));
164 $this->mockObjectManager->expects($this->any())->method('get')->with($className)->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, array('foo' => 'bar'));
169 $this->assertSame($mockValidator, $validator);
170 }
171
172 /**
173 * @test
174 */
175 public function createValidatorThrowsNoSuchValidatorExceptionIfAValidatorCouldNotBeResolved() {
176 $this->markTestSkipped('');
177 $className = uniqid('Test');
178 $this->setExpectedException('\\TYPO3\\CMS\\Extbase\\Validation\\Exception\NoSuchValidatorException', '', 1365799920);
179 $this->validatorResolver->createValidator($className);
180 }
181
182 /**
183 * @test
184 */
185 public function getBaseValidatorCachesTheResultOfTheBuildBaseValidatorChainCalls() {
186 $this->markTestSkipped('Functionality is different now.');
187 $mockConjunctionValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
188 $validatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('buildBaseValidatorConjunction'), array(), '', FALSE);
189 $validatorResolver->expects($this->once())->method('buildBaseValidatorConjunction')->with('Tx_Virtual_Foo')->will($this->returnValue($mockConjunctionValidator));
190 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
191 $this->assertSame($mockConjunctionValidator, $result, '#1');
192 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
193 $this->assertSame($mockConjunctionValidator, $result, '#2');
194 }
195
196 /**
197 * @test
198 * @author Sebastian Kurfürst <sbastian@typo3.org>
199 */
200 public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() {
201 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
202 $methodParameters = array();
203 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
204 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
205 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
206 $validatorResolver->_set('reflectionService', $mockReflectionService);
207 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
208 $this->assertSame(array(), $result);
209 }
210
211 /**
212 * @test
213 * @author Robert Lemke <robert@typo3.org>
214 * @author Bastian Waidelich <bastian@typo3.org>
215 */
216 public function buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod() {
217 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
218 $methodParameters = array(
219 'arg1' => array(
220 'type' => 'string'
221 ),
222 'arg2' => array(
223 'type' => 'array'
224 )
225 );
226 $methodTagsValues = array(
227 'param' => array(
228 'string $arg1',
229 'array $arg2'
230 ),
231 'validate' => array(
232 '$arg1 Foo(bar = baz), Bar',
233 '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
234 )
235 );
236 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
237 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
238 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
239 $mockStringValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
240 $mockArrayValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
241 $mockFooValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
242 $mockBarValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
243 $mockQuuxValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
244 $conjunction1 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
245 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
246 $conjunction1->expects($this->at(1))->method('addValidator')->with($mockFooValidator);
247 $conjunction1->expects($this->at(2))->method('addValidator')->with($mockBarValidator);
248 $conjunction2 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
249 $conjunction2->expects($this->at(0))->method('addValidator')->with($mockArrayValidator);
250 $conjunction2->expects($this->at(1))->method('addValidator')->with($mockQuuxValidator);
251 $mockArguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
252 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg1', 'dummyValue'));
253 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg2', 'dummyValue'));
254 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
255 $validatorResolver->expects($this->at(0))->method('createValidator')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($conjunction1));
256 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
257 $validatorResolver->expects($this->at(2))->method('createValidator')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($conjunction2));
258 $validatorResolver->expects($this->at(3))->method('createValidator')->with('array')->will($this->returnValue($mockArrayValidator));
259 $validatorResolver->expects($this->at(4))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockFooValidator));
260 $validatorResolver->expects($this->at(5))->method('createValidator')->with('Bar')->will($this->returnValue($mockBarValidator));
261 $validatorResolver->expects($this->at(6))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
262 $validatorResolver->_set('reflectionService', $mockReflectionService);
263 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
264 $this->assertEquals(array('arg1' => $conjunction1, 'arg2' => $conjunction2), $result);
265 }
266
267 /**
268 * @test
269 * @author Sebastian Kurfürst <sbastian@typo3.org>
270 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException
271 */
272 public function buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists() {
273 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
274 $methodParameters = array(
275 'arg1' => array(
276 'type' => 'string'
277 )
278 );
279 $methodTagsValues = array(
280 'param' => array(
281 'string $arg1'
282 ),
283 'validate' => array(
284 '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
285 )
286 );
287 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
288 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
289 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
290 $mockStringValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
291 $mockQuuxValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
292 $conjunction1 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
293 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
294 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
295 $validatorResolver->expects($this->at(0))->method('createValidator')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($conjunction1));
296 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
297 $validatorResolver->expects($this->at(2))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
298 $validatorResolver->_set('reflectionService', $mockReflectionService);
299 $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
300 }
301
302 /**
303 * @test
304 * @author Robert Lemke <robert@typo3.org>
305 * @author Christopher Hlubek <hlubek@networkteam.com>
306 */
307 public function buildBaseValidatorConjunctionAddsValidatorsDefinedByAnnotationsInTheClassToTheReturnedConjunction() {
308 $mockObject = $this->getMock('stdClass');
309 $className = get_class($mockObject);
310 $propertyTagsValues = array(
311 'foo' => array(
312 'var' => array('string'),
313 'validate' => array(
314 'Foo(bar= baz), Bar',
315 'Baz'
316 )
317 ),
318 'bar' => array(
319 'var' => array('integer'),
320 'validate' => array(
321 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator'
322 )
323 )
324 );
325
326 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
327 $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('foo', 'bar')));
328 $mockReflectionService->expects($this->at(1))->method('getPropertyTagsValues')->with($className, 'foo')->will($this->returnValue($propertyTagsValues['foo']));
329 $mockReflectionService->expects($this->at(2))->method('getPropertyTagsValues')->with($className, 'bar')->will($this->returnValue($propertyTagsValues['bar']));
330 $mockObjectValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator', array('dummy'), array(), '', FALSE);
331 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
332 $mockObjectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator')->will($this->returnValue($mockObjectValidator));
333 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
334 $validatorResolver->_set('reflectionService', $mockReflectionService);
335 $validatorResolver->_set('objectManager', $mockObjectManager);
336 $validatorResolver->expects($this->at(0))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockObjectValidator));
337 $validatorResolver->expects($this->at(1))->method('createValidator')->with('Bar')->will($this->returnValue($mockObjectValidator));
338 $validatorResolver->expects($this->at(2))->method('createValidator')->with('Baz')->will($this->returnValue($mockObjectValidator));
339 $validatorResolver->expects($this->at(3))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator')->will($this->returnValue($mockObjectValidator));
340 $validatorResolver->_call('buildBaseValidatorConjunction', $className, $className);
341 }
342
343 /**
344 * dataProvider for buildBaseValidatorConjunctionAddsValidatorFromConventionToTheReturnedConjunction
345 *
346 * @return array
347 */
348 public function modelNamesProvider() {
349 return array(
350 'no replace' => array('VENDOR\\ModelCollection\\Domain\\Model\\Model', 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator'),
351 'replace in not namespaced class' => array('Tx_ModelCollection_Domain_Model_Model', 'Tx_ModelCollection_Domain_Validator_ModelValidator'),
352 'replace in namespaced class' => array('VENDOR\\ModelCollection\\Domain\\Model\\Model', 'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator')
353 );
354 }
355
356 /**
357 * @param string $modelClassName
358 * @param string $validatorClassName
359 *
360 * @test
361 * @dataProvider modelNamesProvider
362 */
363 public function buildBaseValidatorConjunctionCreatesValidatorFromClassName($modelClassName, $validatorClassName) {
364 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
365 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
366 $validatorResolver->_set('objectManager', $mockObjectManager);
367 $validatorResolver->expects($this->once())->method('createValidator')->with($validatorClassName)->will($this->returnValue(NULL));
368 $validatorResolver->_call('buildBaseValidatorConjunction', $modelClassName, $modelClassName);
369 }
370
371 /**
372 * @test
373 * @author Bastian Waidelich <bastian@typo3.org>
374 */
375 public function resolveValidatorObjectNameCallsGetValidatorType() {
376 $validatorName = uniqid('FooValidator');
377 eval('namespace TYPO3\CMS\Extbase\Validation\Validator;' . LF . 'class ' . $validatorName . 'Validator implements \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface {}');
378 $mockValidatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('getValidatorType'));
379 $mockValidatorResolver->expects($this->once())->method('getValidatorType')->with($validatorName)->will($this->returnValue($validatorName));
380
381 $mockValidatorResolver->_call('resolveValidatorObjectName', $validatorName);
382 }
383
384 /**
385 * @test
386 * @author Bastian Waidelich <bastian@typo3.org>
387 */
388 public function getValidatorTypeCorrectlyRenamesPhpDataTypes() {
389 $this->assertEquals('Integer', $this->validatorResolver->_call('getValidatorType', 'integer'));
390 $this->assertEquals('Integer', $this->validatorResolver->_call('getValidatorType', 'int'));
391 $this->assertEquals('String', $this->validatorResolver->_call('getValidatorType', 'string'));
392 $this->assertEquals('Array', $this->validatorResolver->_call('getValidatorType', 'array'));
393 $this->assertEquals('Float', $this->validatorResolver->_call('getValidatorType', 'float'));
394 $this->assertEquals('Float', $this->validatorResolver->_call('getValidatorType', 'double'));
395 $this->assertEquals('Boolean', $this->validatorResolver->_call('getValidatorType', 'boolean'));
396 $this->assertEquals('Boolean', $this->validatorResolver->_call('getValidatorType', 'bool'));
397 $this->assertEquals('Boolean', $this->validatorResolver->_call('getValidatorType', 'bool'));
398 $this->assertEquals('Number', $this->validatorResolver->_call('getValidatorType', 'number'));
399 $this->assertEquals('Number', $this->validatorResolver->_call('getValidatorType', 'numeric'));
400 }
401
402 /**
403 * @test
404 * @author Bastian Waidelich <bastian@typo3.org>
405 */
406 public function getValidatorTypeRenamesMixedToRaw() {
407 $this->assertEquals('Raw', $this->validatorResolver->_call('getValidatorType', 'mixed'));
408 }
409
410 /**
411 * dataProvider for parseValidatorAnnotationCanParseAnnotations
412 *
413 * @author Karsten Dambekalns <karsten@typo3.org>
414 * @author Christopher Hlubek <hlubek@networkteam.com>
415 * @return array
416 */
417 public function validatorAnnotations() {
418 return array(
419 array(
420 '$var Bar',
421 array(
422 'argumentName' => 'var',
423 'validators' => array(
424 array('validatorName' => 'Bar', 'validatorOptions' => array())
425 )
426 )
427 ),
428 array(
429 '$var Bar, Foo',
430 array(
431 'argumentName' => 'var',
432 'validators' => array(
433 array('validatorName' => 'Bar', 'validatorOptions' => array()),
434 array('validatorName' => 'Foo', 'validatorOptions' => array())
435 )
436 )
437 ),
438 array(
439 '$var Baz (Foo=Bar)',
440 array(
441 'argumentName' => 'var',
442 'validators' => array(
443 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'Bar'))
444 )
445 )
446 ),
447 array(
448 '$var Buzz (Foo="B=a, r", Baz=1)',
449 array(
450 'argumentName' => 'var',
451 'validators' => array(
452 array('validatorName' => 'Buzz', 'validatorOptions' => array('Foo' => 'B=a, r', 'Baz' => '1'))
453 )
454 )
455 ),
456 array(
457 '$var Foo(Baz=1, Bar=Quux)',
458 array(
459 'argumentName' => 'var',
460 'validators' => array(
461 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))
462 )
463 )
464 ),
465 array(
466 '$var Pax, Foo(Baz = \'1\', Bar = Quux)',
467 array(
468 'argumentName' => 'var',
469 'validators' => array(
470 array('validatorName' => 'Pax', 'validatorOptions' => array()),
471 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))
472 )
473 )
474 ),
475 array(
476 '$var Reg (P="[at]*(h|g)"), Quux',
477 array(
478 'argumentName' => 'var',
479 'validators' => array(
480 array('validatorName' => 'Reg', 'validatorOptions' => array('P' => '[at]*(h|g)')),
481 array('validatorName' => 'Quux', 'validatorOptions' => array())
482 )
483 )
484 ),
485 array(
486 '$var Baz (Foo="B\\"ar")',
487 array(
488 'argumentName' => 'var',
489 'validators' => array(
490 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'B"ar'))
491 )
492 )
493 ),
494 array(
495 '$var F3_TestPackage_Quux',
496 array(
497 'argumentName' => 'var',
498 'validators' => array(
499 array('validatorName' => 'F3_TestPackage_Quux', 'validatorOptions' => array())
500 )
501 )
502 ),
503 array(
504 '$var Baz(Foo="5"), Bar(Quux="123")',
505 array(
506 'argumentName' => 'var',
507 'validators' => array(
508 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '5')),
509 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123'))
510 )
511 )
512 ),
513 array(
514 '$var Baz(Foo="2"), Bar(Quux=123, Pax="a weird \\"string\\" with *freaky* \\stuff")',
515 array(
516 'argumentName' => 'var',
517 'validators' => array(
518 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '2')),
519 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123', 'Pax' => 'a weird "string" with *freaky* \\stuff'))
520 )
521 )
522 ),
523 'namespaced validator class name' => array(
524 'annotation' => '$var F3\TestPackage\Quux',
525 'expected' => array(
526 'argumentName' => 'var',
527 'validators' => array(
528 array('validatorName' => 'F3\TestPackage\Quux', 'validatorOptions' => array())
529 )
530 )
531 ),
532 'shorthand notation for system validator' => array(
533 'annotation' => '$var TYPO3.CMS.Mypkg:MySecond',
534 'expected' => array(
535 'argumentName' => 'var',
536 'validators' => array(
537 array('validatorName' => 'TYPO3.CMS.Mypkg:MySecond', 'validatorOptions' => array())
538 )
539 )
540 ),
541 'shorthand notation for custom validator with parameter' => array(
542 'annotation' => '$var Acme.Mypkg:MyThird(Foo="2")',
543 'expected' => array(
544 'argumentName' => 'var',
545 'validators' => array(
546 array('validatorName' => 'Acme.Mypkg:MyThird', 'validatorOptions' => array('Foo' => '2'))
547 )
548 )
549 ),
550 );
551 }
552
553 /**
554 * @test
555 * @dataProvider validatorAnnotations
556 * @author Karsten Dambekalns <karsten@typo3.org>
557 * @param mixed $annotation
558 * @param mixed $expectedResult
559 */
560 public function parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult) {
561 $result = $this->validatorResolver->_call('parseValidatorAnnotation', $annotation);
562 $this->assertEquals($result, $expectedResult);
563 }
564 }