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