Raised DBAL version from 1.1.5 to 1.1.6
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Validation / ValidatorResolver_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
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.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Testcase for the validator resolver
30 *
31 * @package Extbase
32 * @subpackage extbase
33 * @version $Id: ValidatorResolver_testcase.php 1709 2009-11-25 11:26:13Z jocrau $
34 */
35 class Tx_Extbase_Validation_ValidatorResolver_testcase extends Tx_Extbase_BaseTestCase {
36
37 /**
38 * @test
39 */
40 public function resolveValidatorObjectNameReturnsFalseIfValidatorCantBeResolved() {
41 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ManagerInterface');
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'));
45 }
46
47 /**
48 * @test
49 */
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));
55 }
56
57 /**
58 * @test
59 */
60 public function resolveValidatorObjectNameCanResolveShortNamesOfBuiltInValidators() {
61 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ManagerInterface');
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'));
66 }
67
68 /**
69 * @test
70 */
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'));
75
76 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ManagerInterface');
77 $mockObjectManager->expects($this->any())->method('getObject')->with($className)->will($this->returnValue($mockValidator));
78
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);
84 }
85
86 /**
87 * @test
88 */
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);
94 }
95
96 /**
97 * @test
98 */
99 public function getBaseValidatorCachesTheResultOfTheBuildBaseValidatorChainCalls() {
100 $mockConjunctionValidator = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
101
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));
104
105 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
106 $this->assertSame($mockConjunctionValidator, $result, '#1');
107
108 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
109 $this->assertSame($mockConjunctionValidator, $result, '#2');
110 }
111
112 /**
113 * @test
114 * @author Sebastian Kurfürst <sbastian@typo3.org>
115 */
116 public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() {
117 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
118
119 $methodTagsValues = array();
120 $methodParameters = array();
121
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));
125
126 $validatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array('createValidator'));
127 $validatorResolver->injectReflectionService($mockReflectionService);
128
129 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
130 $this->assertSame(array(), $result);
131 }
132
133 /**
134 * @test
135 * @author Robert Lemke <robert@typo3.org>
136 * @author Bastian Waidelich <bastian@typo3.org>
137 */
138 public function buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod() {
139 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
140
141 $methodParameters = array(
142 'arg1' => array(
143 'type' => 'string'
144 ),
145 'arg2' => array(
146 'type' => 'array'
147 )
148
149 );
150 $methodTagsValues = array(
151 'param' => array(
152 'string $arg1',
153 'array $arg2',
154 ),
155 'validate' => array(
156 '$arg1 Foo(bar = baz), Bar',
157 '$arg2 F3_TestPackage_Quux'
158 )
159 );
160
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));
164
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);
170
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);
175
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);
179
180 $mockObjectFactory = $this->getMock('Tx_Extbase_Object_FactoryInterface');
181
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'));
185
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));
194
195 $validatorResolver->injectReflectionService($mockReflectionService);
196
197 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
198 $this->assertEquals(array('arg1' => $conjunction1, 'arg2' => $conjunction2), $result);
199 }
200
201 /**
202 * @test
203 * @author Sebastian Kurfürst <sbastian@typo3.org>
204 * @expectedException Tx_Extbase_Validation_Exception_InvalidValidationConfiguration
205 */
206 public function buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists() {
207 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
208
209 $methodParameters = array(
210 'arg1' => array(
211 'type' => 'string'
212 )
213 );
214 $methodTagsValues = array(
215 'param' => array(
216 'string $arg1',
217 ),
218 'validate' => array(
219 '$arg2 F3_TestPackage_Quux'
220 )
221 );
222
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));
226
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);
231
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));
236
237 $validatorResolver->injectReflectionService($mockReflectionService);
238
239 $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
240 }
241
242 /**
243 * @test
244 * @author Robert Lemke <robert@typo3.org>
245 * @author Christopher Hlubek <hlubek@networkteam.com>
246 */
247 public function buildBaseValidatorConjunctionAddsValidatorsDefinedByAnnotationsInTheClassToTheReturnedConjunction() {
248 $mockObject = $this->getMock('stdClass');
249 $className = get_class($mockObject);
250
251 $propertyTagsValues = array(
252 'foo' => array(
253 'var' => array('string'),
254 'validate' => array(
255 'Foo(bar = baz), Bar',
256 'Baz'
257 )
258 ),
259 'bar' => array(
260 'var' => array('integer'),
261 'validate' => array(
262 'F3_TestPackage_Quux'
263 )
264 )
265 );
266
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']));
271
272 $mockObjectValidator = $this->getMock('Tx_Extbase_Validation_Validator_GenericObjectValidator', array(), array(), '', FALSE);
273
274 $mockConjunctionValidator = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
275 $mockConjunctionValidator->expects($this->once())->method('addValidator')->with($mockObjectValidator);
276
277 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ManagerInterface', array(), array(), '', FALSE);
278 $mockObjectManager->expects($this->at(0))->method('getObject')->with('Tx_Extbase_Validation_Validator_ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
279
280 $validatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('resolveValidatorObjectName', 'createValidator'));
281 $validatorResolver->injectReflectionService($mockReflectionService);
282 $validatorResolver->injectObjectManager($mockObjectManager);
283
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));
290
291 $result = $validatorResolver->_call('buildBaseValidatorConjunction', $className);
292 $this->assertSame($mockConjunctionValidator, $result);
293 }
294
295 /**
296 * @test
297 * @author Bastian Waidelich <bastian@typo3.org>
298 */
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');
303 }
304
305 /**
306 * @test
307 * @author Bastian Waidelich <bastian@typo3.org>
308 */
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'));
322 }
323
324 /**
325 * @test
326 * @author Bastian Waidelich <bastian@typo3.org>
327 */
328 public function unifyDataTypeRenamesMixedToRaw() {
329 $mockValidator = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
330 $this->assertEquals('Raw', $mockValidator->_call('unifyDataType', 'mixed'));
331 }
332
333 /**
334 * dataProvider for parseValidatorAnnotationCanParseAnnotations
335 * @author Karsten Dambekalns <karsten@typo3.org>
336 * @author Christopher Hlubek <hlubek@networkteam.com>
337 */
338 public function validatorAnnotations() {
339 return array(
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())
345 ))),
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'))
355 ))),
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())
359 ))),
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'))))),
370 );
371 }
372
373 /**
374 *
375 * @test
376 * @dataProvider validatorAnnotations
377 * @author Karsten Dambekalns <karsten@typo3.org>
378 */
379 public function parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult) {
380 $mockValidatorResolver = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Validation_ValidatorResolver'), array('dummy'));
381 $result = $mockValidatorResolver->_call('parseValidatorAnnotation', $annotation);
382
383 $this->assertEquals($result, $expectedResult);
384 }
385
386 }
387
388 ?>