‪TYPO3CMS  9.5
ValidatorResolverTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
26 class ‪ValidatorResolverTest extends UnitTestCase
27 {
31  protected ‪$validatorResolver;
32 
33  protected function ‪setUp()
34  {
35  $this->validatorResolver = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Validation\ValidatorResolver::class, ['dummy']);
36  }
37 
42  {
43  $mockController = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\ActionController::class, ['fooAction'], [], '', false);
44  $className = get_class($mockController);
45  $methodName = 'fooAction';
46 
47  $classSchemaMock = $this->createMock(ClassSchema::class);
48  $classSchemaMock->expects($this->any())->method('getMethod')->with($methodName)->willReturn([
49  'params' => [],
50  ]);
51 
52  $mockReflectionService = $this->createMock(\‪TYPO3\CMS\‪Extbase\Reflection\ReflectionService::class);
53  $mockReflectionService->expects($this->any())->method('getClassSchema')->with($className)->willReturn($classSchemaMock);
54 
56  ‪$validatorResolver = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Validation\ValidatorResolver::class, ['createValidator']);
57  ‪$validatorResolver->_set('reflectionService', $mockReflectionService);
58 
59  $result = ‪$validatorResolver->‪buildMethodArgumentsValidatorConjunctions($className, $methodName);
60  $this->assertSame([], $result);
61  }
62 
67  {
68  $mockObject = $this->getMockBuilder('stdClass')
69  ->setMethods(['fooMethod'])
70  ->disableOriginalConstructor()
71  ->getMock();
72 
73  $className = get_class($mockObject);
74  $methodName = 'fooAction';
75 
76  $methodParameters = [
77  'arg1' => [
78  'type' => 'string'
79  ],
80  'arg2' => [
81  'type' => 'array'
82  ]
83  ];
84  $methodTagsValues = [
85  'param' => [
86  'string $arg1',
87  'array $arg2'
88  ],
89  'validate' => [
90  '$arg1 Foo(bar = baz), Bar',
91  '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
92  ]
93  ];
94 
95  $classSchemaMock = $this->createMock(ClassSchema::class);
96  $classSchemaMock->expects($this->any())->method('getMethod')->with($methodName)->willReturn([
97  'params' => $methodParameters,
98  'tags' => $methodTagsValues
99  ]);
100 
101  $mockReflectionService = $this->createMock(\‪TYPO3\CMS\‪Extbase\Reflection\ReflectionService::class);
102  $mockReflectionService->expects($this->any())->method('getClassSchema')->with($className)->willReturn($classSchemaMock);
103 
104  $mockStringValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
105  $mockArrayValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
106  $mockFooValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
107  $mockBarValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
108  $mockQuuxValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
109  $conjunction1 = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ConjunctionValidator::class);
110  $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
111  $conjunction1->expects($this->at(1))->method('addValidator')->with($mockFooValidator);
112  $conjunction1->expects($this->at(2))->method('addValidator')->with($mockBarValidator);
113  $conjunction2 = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ConjunctionValidator::class);
114  $conjunction2->expects($this->at(0))->method('addValidator')->with($mockArrayValidator);
115  $conjunction2->expects($this->at(1))->method('addValidator')->with($mockQuuxValidator);
116  $mockArguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
117  $mockArguments->addArgument(new \‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument('arg1', 'dummyValue'));
118  $mockArguments->addArgument(new \‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument('arg2', 'dummyValue'));
119 
121  ‪$validatorResolver = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Validation\ValidatorResolver::class, ['createValidator']);
122  ‪$validatorResolver->expects($this->at(0))->method('createValidator')->with(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ConjunctionValidator::class)->will($this->returnValue($conjunction1));
123  ‪$validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
124  ‪$validatorResolver->expects($this->at(2))->method('createValidator')->with(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ConjunctionValidator::class)->will($this->returnValue($conjunction2));
125  ‪$validatorResolver->expects($this->at(3))->method('createValidator')->with('array')->will($this->returnValue($mockArrayValidator));
126  ‪$validatorResolver->expects($this->at(4))->method('createValidator')->with('Foo', ['bar' => 'baz'])->will($this->returnValue($mockFooValidator));
127  ‪$validatorResolver->expects($this->at(5))->method('createValidator')->with('Bar')->will($this->returnValue($mockBarValidator));
128  ‪$validatorResolver->expects($this->at(6))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
129  ‪$validatorResolver->_set('reflectionService', $mockReflectionService);
130 
131  $result = ‪$validatorResolver->‪buildMethodArgumentsValidatorConjunctions($className, $methodName);
132  $this->assertEquals(['arg1' => $conjunction1, 'arg2' => $conjunction2], $result);
133  }
134 
139  {
140  $this->expectException(InvalidValidationConfigurationException::class);
141  $this->expectExceptionCode(1253172726);
142  $mockObject = $this->getMockBuilder('stdClass')
143  ->setMethods(['fooMethod'])
144  ->disableOriginalConstructor()
145  ->getMock();
146 
147  $className = get_class($mockObject);
148  $methodName = 'fooAction';
149 
150  $methodParameters = [
151  'arg1' => [
152  'type' => 'string'
153  ]
154  ];
155  $methodTagsValues = [
156  'param' => [
157  'string $arg1'
158  ],
159  'validate' => [
160  '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
161  ]
162  ];
163 
164  $classSchemaMock = $this->createMock(ClassSchema::class);
165  $classSchemaMock->expects($this->any())->method('getMethod')->with($methodName)->willReturn([
166  'params' => $methodParameters,
167  'tags' => $methodTagsValues
168  ]);
169 
170  $mockReflectionService = $this->createMock(\‪TYPO3\CMS\‪Extbase\Reflection\ReflectionService::class);
171  $mockReflectionService->expects($this->any())->method('getClassSchema')->with($className)->willReturn($classSchemaMock);
172 
173  $mockStringValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
174  $mockQuuxValidator = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ValidatorInterface::class);
175  $conjunction1 = $this->createMock(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ConjunctionValidator::class);
176  $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
177 
179  ‪$validatorResolver = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Validation\ValidatorResolver::class, ['createValidator']);
180  ‪$validatorResolver->expects($this->at(0))->method('createValidator')->with(\‪TYPO3\CMS\‪Extbase\Validation\Validator\ConjunctionValidator::class)->will($this->returnValue($conjunction1));
181  ‪$validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
182  ‪$validatorResolver->expects($this->at(2))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
183  ‪$validatorResolver->_set('reflectionService', $mockReflectionService);
185  }
186 
191  public function ‪validatorAnnotations(): array
192  {
193  return [
194  [
195  '$var Bar',
196  [
197  'argumentName' => 'var',
198  'validators' => [
199  ['validatorName' => 'Bar', 'validatorOptions' => []]
200  ]
201  ]
202  ],
203  [
204  '$var Bar, Foo',
205  [
206  'argumentName' => 'var',
207  'validators' => [
208  ['validatorName' => 'Bar', 'validatorOptions' => []],
209  ['validatorName' => 'Foo', 'validatorOptions' => []]
210  ]
211  ]
212  ],
213  [
214  '$var Baz (Foo=Bar)',
215  [
216  'argumentName' => 'var',
217  'validators' => [
218  ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => 'Bar']]
219  ]
220  ]
221  ],
222  [
223  '$var Buzz (Foo="B=a, r", Baz=1)',
224  [
225  'argumentName' => 'var',
226  'validators' => [
227  ['validatorName' => 'Buzz', 'validatorOptions' => ['Foo' => 'B=a, r', 'Baz' => '1']]
228  ]
229  ]
230  ],
231  [
232  '$var Foo(Baz=1, Bar=Quux)',
233  [
234  'argumentName' => 'var',
235  'validators' => [
236  ['validatorName' => 'Foo', 'validatorOptions' => ['Baz' => '1', 'Bar' => 'Quux']]
237  ]
238  ]
239  ],
240  [
241  '$var Pax, Foo(Baz = \'1\', Bar = Quux)',
242  [
243  'argumentName' => 'var',
244  'validators' => [
245  ['validatorName' => 'Pax', 'validatorOptions' => []],
246  ['validatorName' => 'Foo', 'validatorOptions' => ['Baz' => '1', 'Bar' => 'Quux']]
247  ]
248  ]
249  ],
250  [
251  '$var Reg (P="[at]*(h|g)"), Quux',
252  [
253  'argumentName' => 'var',
254  'validators' => [
255  ['validatorName' => 'Reg', 'validatorOptions' => ['P' => '[at]*(h|g)']],
256  ['validatorName' => 'Quux', 'validatorOptions' => []]
257  ]
258  ]
259  ],
260  [
261  '$var Baz (Foo="B\\"ar")',
262  [
263  'argumentName' => 'var',
264  'validators' => [
265  ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => 'B"ar']]
266  ]
267  ]
268  ],
269  [
270  '$var Baz(Foo="5"), Bar(Quux="123")',
271  [
272  'argumentName' => 'var',
273  'validators' => [
274  ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => '5']],
275  ['validatorName' => 'Bar', 'validatorOptions' => ['Quux' => '123']]
276  ]
277  ]
278  ],
279  [
280  '$var Baz(Foo="2"), Bar(Quux=123, Pax="a weird \\"string\\" with *freaky* \\stuff")',
281  [
282  'argumentName' => 'var',
283  'validators' => [
284  ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => '2']],
285  ['validatorName' => 'Bar', 'validatorOptions' => ['Quux' => '123', 'Pax' => 'a weird "string" with *freaky* \\stuff']]
286  ]
287  ]
288  ],
289  'namespaced validator class name' => [
290  'annotation' => '$var F3\TestPackage\Quux',
291  'expected' => [
292  'argumentName' => 'var',
293  'validators' => [
294  ['validatorName' => 'F3\TestPackage\Quux', 'validatorOptions' => []]
295  ]
296  ]
297  ],
298  'shorthand notation for system validator' => [
299  'annotation' => '$var TYPO3.CMS.Mypkg:MySecond',
300  'expected' => [
301  'argumentName' => 'var',
302  'validators' => [
303  ['validatorName' => 'TYPO3.CMS.Mypkg:MySecond', 'validatorOptions' => []]
304  ]
305  ]
306  ],
307  'shorthand notation for custom validator with parameter' => [
308  'annotation' => '$var Acme.Mypkg:MyThird(Foo="2")',
309  'expected' => [
310  'argumentName' => 'var',
311  'validators' => [
312  ['validatorName' => 'Acme.Mypkg:MyThird', 'validatorOptions' => ['Foo' => '2']]
313  ]
314  ]
315  ],
316  ];
317  }
318 
326  public function ‪parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult)
327  {
328  $result = $this->validatorResolver->_call('parseValidatorAnnotation', $annotation);
329  static::assertEquals($result, $expectedResult);
330  }
331 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException
Definition: InvalidValidationConfigurationException.php:21
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest\buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod
‪buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod()
Definition: ValidatorResolverTest.php:65
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest
Definition: ValidatorResolverTest.php:27
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:29
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver\buildMethodArgumentsValidatorConjunctions
‪ConjunctionValidator[] buildMethodArgumentsValidatorConjunctions($className, $methodName, array $methodParameters=null, array $methodValidateAnnotations=null)
Definition: ValidatorResolver.php:157
‪TYPO3
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest\setUp
‪setUp()
Definition: ValidatorResolverTest.php:32
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest\$validatorResolver
‪TYPO3 CMS Extbase Validation ValidatorResolver $validatorResolver
Definition: ValidatorResolverTest.php:30
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest\buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments
‪buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments()
Definition: ValidatorResolverTest.php:40
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest\buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists
‪buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists()
Definition: ValidatorResolverTest.php:137
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest\parseValidatorAnnotationCanParseAnnotations
‪parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult)
Definition: ValidatorResolverTest.php:325
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:41
‪TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation\ValidatorResolverTest\validatorAnnotations
‪array validatorAnnotations()
Definition: ValidatorResolverTest.php:190