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