2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * Testcase for the validator resolver
35 class Tx_Extbase_Validation_ValidatorResolver_testcase
extends Tx_Extbase_BaseTestCase
{
40 public function resolveValidatorObjectNameReturnsFalseIfValidatorCantBeResolved() {
41 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
42 $validatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
43 $validatorResolver->_set('objectManager', $objectManager);
44 $this->assertSame(FALSE, $validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
50 public function resolveValidatorObjectNameReturnsTheGivenArgumentIfAnObjectOfThatNameIsRegistered() {
51 $validatorName = uniqid('FooValidator_') . 'Validator';
52 eval('class ' . $validatorName . ' {}');
53 $validatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
54 $this->assertSame($validatorName, $validatorResolver->_call('resolveValidatorObjectName', $validatorName));
60 public function resolveValidatorObjectNameCanResolveShortNamesOfBuiltInValidators() {
61 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
62 eval('class Tx_Extbase_Validation_Validator_FooValidator {}');
63 $validatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
64 $validatorResolver->_set('objectManager', $mockObjectManager);
65 $this->assertSame('Tx_Extbase_Validation_Validator_FooValidator', $validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
71 public function createValidatorResolvesAndReturnsAValidatorAndPassesTheGivenOptions() {
72 $className = uniqid('Test');
73 $mockValidator = $this->getMock('Tx_Extbase_Validation_Validator_ObjectValidatorInterface', array(), array(), $className);
74 $mockValidator->expects($this->once())->method('setOptions')->with(array('foo' => 'bar'));
76 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
77 $mockObjectManager->expects($this->any())->method('getObject')->with($className)->will($this->returnValue($mockValidator));
79 $validatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'),array('resolveValidatorObjectName'));
80 $validatorResolver->_set('objectManager', $mockObjectManager);
81 $validatorResolver->expects($this->once())->method('resolveValidatorObjectName')->with($className)->will($this->returnValue($className));
82 $validator = $validatorResolver->createValidator($className, array('foo' => 'bar'));
83 $this->assertSame($mockValidator, $validator);
89 public function createValidatorReturnsNullIfAValidatorCouldNotBeResolved() {
90 $validatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver',array('resolveValidatorObjectName'), array(), '', FALSE);
91 $validatorResolver->expects($this->once())->method('resolveValidatorObjectName')->with('Foo')->will($this->returnValue(FALSE));
92 $validator = $validatorResolver->createValidator('Foo', array('foo' => 'bar'));
93 $this->assertNull($validator);
99 public function getBaseValidatorCachesTheResultOfTheBuildBaseValidatorChainCalls() {
100 $mockConjunctionValidator = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
102 $validatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array('buildBaseValidatorConjunction'), array(), '', FALSE);
103 $validatorResolver->expects($this->once())->method('buildBaseValidatorConjunction')->with('Tx_Virtual_Foo')->will($this->returnValue($mockConjunctionValidator));
105 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
106 $this->assertSame($mockConjunctionValidator, $result, '#1');
108 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
109 $this->assertSame($mockConjunctionValidator, $result, '#2');
114 * @author Sebastian Kurfürst <sbastian@typo3.org>
116 public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() {
117 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
119 $methodTagsValues = array();
120 $methodParameters = array();
122 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
123 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
124 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
126 $validatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array('createValidator'));
127 $validatorResolver->injectReflectionService($mockReflectionService);
129 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
130 $this->assertSame(array(), $result);
135 * @author Robert Lemke <robert@typo3.org>
136 * @author Bastian Waidelich <bastian@typo3.org>
138 public function buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod() {
139 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
141 $methodParameters = array(
150 $methodTagsValues = array(
156 '$arg1 Foo(bar = baz), Bar',
157 '$arg2 F3_TestPackage_Quux'
161 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
162 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
163 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
165 $mockStringValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface', array(), array(), '', FALSE);
166 $mockArrayValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface', array(), array(), '', FALSE);
167 $mockFooValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface', array(), array(), '', FALSE);
168 $mockBarValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface', array(), array(), '', FALSE);
169 $mockQuuxValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface', array(), array(), '', FALSE);
171 $conjunction1 = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
172 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
173 $conjunction1->expects($this->at(1))->method('addValidator')->with($mockFooValidator);
174 $conjunction1->expects($this->at(2))->method('addValidator')->with($mockBarValidator);
176 $conjunction2 = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
177 $conjunction2->expects($this->at(0))->method('addValidator')->with($mockArrayValidator);
178 $conjunction2->expects($this->at(1))->method('addValidator')->with($mockQuuxValidator);
180 $mockObjectFactory = $this->getMock('Tx_Extbase_Object_FactoryInterface');
182 $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
183 $mockArguments->addArgument(new Tx_Extbase_MVC_Controller_Argument('arg1', 'dummyValue'));
184 $mockArguments->addArgument(new Tx_Extbase_MVC_Controller_Argument('arg2', 'dummyValue'));
186 $validatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array('createValidator'));
187 $validatorResolver->expects($this->at(0))->method('createValidator')->with('Conjunction')->will($this->returnValue($conjunction1));
188 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
189 $validatorResolver->expects($this->at(2))->method('createValidator')->with('Conjunction')->will($this->returnValue($conjunction2));
190 $validatorResolver->expects($this->at(3))->method('createValidator')->with('array')->will($this->returnValue($mockArrayValidator));
191 $validatorResolver->expects($this->at(4))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockFooValidator));
192 $validatorResolver->expects($this->at(5))->method('createValidator')->with('Bar')->will($this->returnValue($mockBarValidator));
193 $validatorResolver->expects($this->at(6))->method('createValidator')->with('F3_TestPackage_Quux')->will($this->returnValue($mockQuuxValidator));
195 $validatorResolver->injectReflectionService($mockReflectionService);
197 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
198 $this->assertEquals(array('arg1' => $conjunction1, 'arg2' => $conjunction2), $result);
203 * @author Sebastian Kurfürst <sbastian@typo3.org>
204 * @expectedException Tx_Extbase_Validation_Exception_InvalidValidationConfiguration
206 public function buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists() {
207 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
209 $methodParameters = array(
214 $methodTagsValues = array(
219 '$arg2 F3_TestPackage_Quux'
223 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
224 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
225 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
227 $mockStringValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface', array(), array(), '', FALSE);
228 $mockQuuxValidator = $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface', array(), array(), '', FALSE);
229 $conjunction1 = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
230 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
232 $validatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array('createValidator'));
233 $validatorResolver->expects($this->at(0))->method('createValidator')->with('Conjunction')->will($this->returnValue($conjunction1));
234 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
235 $validatorResolver->expects($this->at(2))->method('createValidator')->with('F3_TestPackage_Quux')->will($this->returnValue($mockQuuxValidator));
237 $validatorResolver->injectReflectionService($mockReflectionService);
239 $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
244 * @author Robert Lemke <robert@typo3.org>
245 * @author Christopher Hlubek <hlubek@networkteam.com>
247 public function buildBaseValidatorConjunctionAddsValidatorsDefinedByAnnotationsInTheClassToTheReturnedConjunction() {
248 $mockObject = $this->getMock('stdClass');
249 $className = get_class($mockObject);
251 $propertyTagsValues = array(
253 'var' => array('string'),
255 'Foo(bar = baz), Bar',
260 'var' => array('integer'),
262 'F3_TestPackage_Quux'
267 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
268 $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('foo', 'bar')));
269 $mockReflectionService->expects($this->at(1))->method('getPropertyTagsValues')->with($className, 'foo')->will($this->returnValue($propertyTagsValues['foo']));
270 $mockReflectionService->expects($this->at(2))->method('getPropertyTagsValues')->with($className, 'bar')->will($this->returnValue($propertyTagsValues['bar']));
272 $mockObjectValidator = $this->getMock('Tx_Extbase_Validation_Validator_GenericObjectValidator', array(), array(), '', FALSE);
274 $mockConjunctionValidator = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
275 $mockConjunctionValidator->expects($this->once())->method('addValidator')->with($mockObjectValidator);
277 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface', array(), array(), '', FALSE);
278 $mockObjectManager->expects($this->at(0))->method('getObject')->with('Tx_Extbase_Validation_Validator_ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
280 $validatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('resolveValidatorObjectName', 'createValidator'));
281 $validatorResolver->injectReflectionService($mockReflectionService);
282 $validatorResolver->injectObjectManager($mockObjectManager);
284 $validatorResolver->expects($this->at(0))->method('createValidator')->with('GenericObject')->will($this->returnValue($mockObjectValidator));
285 $validatorResolver->expects($this->at(1))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockObjectValidator));
286 $validatorResolver->expects($this->at(2))->method('createValidator')->with('Bar')->will($this->returnValue($mockObjectValidator));
287 $validatorResolver->expects($this->at(3))->method('createValidator')->with('Baz')->will($this->returnValue($mockObjectValidator));
288 $validatorResolver->expects($this->at(4))->method('createValidator')->with('F3_TestPackage_Quux')->will($this->returnValue($mockObjectValidator));
289 $validatorResolver->expects($this->at(5))->method('createValidator')->with($className . 'Validator')->will($this->returnValue(NULL));
291 $result = $validatorResolver->_call('buildBaseValidatorConjunction', $className);
292 $this->assertSame($mockConjunctionValidator, $result);
297 * @author Bastian Waidelich <bastian@typo3.org>
299 public function resolveValidatorObjectNameCallsUnifyDataType() {
300 $mockValidatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('unifyDataType'));
301 $mockValidatorResolver->expects($this->once())->method('unifyDataType')->with('someDataType');
302 $mockValidatorResolver->_call('resolveValidatorObjectName', 'someDataType');
307 * @author Bastian Waidelich <bastian@typo3.org>
309 public function unifyDataTypeCorrectlyRenamesPHPDataTypes() {
310 $mockValidatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
311 $this->assertEquals('Integer', $mockValidatorResolver->_call('unifyDataType', 'integer'));
312 $this->assertEquals('Integer', $mockValidatorResolver->_call('unifyDataType', 'int'));
313 $this->assertEquals('String', $mockValidatorResolver->_call('unifyDataType', 'string'));
314 $this->assertEquals('Array', $mockValidatorResolver->_call('unifyDataType', 'array'));
315 $this->assertEquals('Float', $mockValidatorResolver->_call('unifyDataType', 'float'));
316 $this->assertEquals('Float', $mockValidatorResolver->_call('unifyDataType', 'double'));
317 $this->assertEquals('Boolean', $mockValidatorResolver->_call('unifyDataType', 'boolean'));
318 $this->assertEquals('Boolean', $mockValidatorResolver->_call('unifyDataType', 'bool'));
319 $this->assertEquals('Boolean', $mockValidatorResolver->_call('unifyDataType', 'bool'));
320 $this->assertEquals('Number', $mockValidatorResolver->_call('unifyDataType', 'number'));
321 $this->assertEquals('Number', $mockValidatorResolver->_call('unifyDataType', 'numeric'));
326 * @author Bastian Waidelich <bastian@typo3.org>
328 public function unifyDataTypeRenamesMixedToRaw() {
329 $mockValidator = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
330 $this->assertEquals('Raw', $mockValidator->_call('unifyDataType', 'mixed'));
334 * dataProvider for parseValidatorAnnotationCanParseAnnotations
335 * @author Karsten Dambekalns <karsten@typo3.org>
336 * @author Christopher Hlubek <hlubek@networkteam.com>
338 public function validatorAnnotations() {
340 array('$var Bar', array('argumentName' => 'var', 'validators' => array(
341 array('validatorName' => 'Bar', 'validatorOptions' => array())))),
342 array('$var Bar, Foo', array('argumentName' => 'var', 'validators' => array(
343 array('validatorName' => 'Bar', 'validatorOptions' => array()),
344 array('validatorName' => 'Foo', 'validatorOptions' => array())
346 array('$var Baz (Foo=Bar)', array('argumentName' => 'var', 'validators' => array(
347 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'Bar'))))),
348 array('$var Buzz (Foo="B=a, r", Baz=1)', array('argumentName' => 'var', 'validators' => array(
349 array('validatorName' => 'Buzz', 'validatorOptions' => array('Foo' => 'B=a, r', 'Baz' => '1'))))),
350 array('$var Foo(Baz=1, Bar=Quux)', array('argumentName' => 'var', 'validators' => array(
351 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))))),
352 array('$var Pax, Foo(Baz = \'1\', Bar = Quux)', array('argumentName' => 'var', 'validators' => array(
353 array('validatorName' => 'Pax', 'validatorOptions' => array()),
354 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))
356 array('$var Reg (P="[at]*(h|g)"), Quux', array('argumentName' => 'var', 'validators' => array(
357 array('validatorName' => 'Reg', 'validatorOptions' => array('P' => '[at]*(h|g)')),
358 array('validatorName' => 'Quux', 'validatorOptions' => array())
360 array('$var Baz (Foo="B\"ar")', array('argumentName' => 'var', 'validators' => array(
361 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'B"ar'))))),
362 array('$var F3_TestPackage_Quux', array('argumentName' => 'var', 'validators' => array(
363 array('validatorName' => 'F3_TestPackage_Quux', 'validatorOptions' => array())))),
364 array('$var Baz(Foo="5"), Bar(Quux="123")', array('argumentName' => 'var', 'validators' => array(
365 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '5')),
366 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123'))))),
367 array('$var Baz(Foo="2"), Bar(Quux=123, Pax="a weird \"string\" with *freaky* \\stuff")', array('argumentName' => 'var', 'validators' => array(
368 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '2')),
369 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123', 'Pax' => 'a weird "string" with *freaky* \\stuff'))))),
376 * @dataProvider validatorAnnotations
377 * @author Karsten Dambekalns <karsten@typo3.org>
379 public function parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult) {
380 $mockValidatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
381 $result = $mockValidatorResolver->_call('parseValidatorAnnotation', $annotation);
383 $this->assertEquals($result, $expectedResult);