‪TYPO3CMS  10.4
GenericObjectValidatorTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪GenericObjectValidatorTest extends UnitTestCase
30 {
35  {
36  self::assertTrue((new ‪GenericObjectValidator())->validate('foo')->hasErrors());
37  }
38 
43  {
44  self::assertFalse((new ‪GenericObjectValidator())->validate(null)->hasErrors());
45  }
46 
50  public function ‪dataProviderForValidator(): array
51  {
52  $error1 = new ‪Error('error1', 1);
53  $error2 = new ‪Error('error2', 2);
54  $emptyResult1 = new ‪Result();
55  $emptyResult2 = new ‪Result();
56  $resultWithError1 = new ‪Result();
57  $resultWithError1->addError($error1);
58  $resultWithError2 = new ‪Result();
59  $resultWithError2->addError($error2);
60  $objectWithPrivateProperties = new class() {
61  protected $foo = 'foovalue';
62  protected $bar = 'barvalue';
63 
64  public function getFoo()
65  {
66  return $this->foo;
67  }
68 
69  public function getBar()
70  {
71  return $this->bar;
72  }
73  };
74 
75  return [
76  // If no errors happened, this is shown
77  [$objectWithPrivateProperties, $emptyResult1, $emptyResult2, []],
78  // If errors on two properties happened, they are merged together.
79  [$objectWithPrivateProperties, $resultWithError1, $resultWithError2, ['foo' => [$error1], 'bar' => [$error2]]]
80  ];
81  }
82 
92  public function ‪validateChecksAllPropertiesForWhichAPropertyValidatorExists($objectToBeValidated, $validationResultForFoo, $validationResultForBar, ‪$errors)
93  {
95 
97  $validatorForFoo = $this->getMockBuilder(ValidatorInterface::class)
98  ->setMethods(['validate', 'getOptions'])
99  ->getMock();
100  $validatorForFoo->expects(self::once())->method('validate')->with('foovalue')->willReturn($validationResultForFoo);
101 
103  $validatorForBar = $this->getMockBuilder(ValidatorInterface::class)
104  ->setMethods(['validate', 'getOptions'])
105  ->getMock();
106  $validatorForBar->expects(self::once())->method('validate')->with('barvalue')->willReturn($validationResultForBar);
107 
108  ‪$validator->addPropertyValidator('foo', $validatorForFoo);
109  ‪$validator->addPropertyValidator('bar', $validatorForBar);
110 
111  self::assertEquals(‪$errors, ‪$validator->validate($objectToBeValidated)->getFlattenedErrors());
112  }
113 
118  {
119  $A = new class() {
120  public $b;
121  };
122 
123  $B = new class() {
124  public $a;
125  };
126 
127  $A->b = $B;
128  $B->a = $A;
129 
130  $aValidator = new ‪GenericObjectValidator();
131  $bValidator = new ‪GenericObjectValidator();
132 
133  $aValidator->addPropertyValidator('b', $bValidator);
134  $bValidator->addPropertyValidator('a', $aValidator);
135 
136  self::assertFalse($aValidator->validate($A)->hasErrors());
137  }
138 
143  {
144  $A = new class() {
145  public $b;
146  };
147 
148  $B = new class() {
149  public $a;
150  public $uuid = 0xF;
151  };
152 
153  $A->b = $B;
154  $B->a = $A;
155  $aValidator = new ‪GenericObjectValidator();
156  $bValidator = new ‪GenericObjectValidator();
157 
158  $aValidator->addPropertyValidator('b', $bValidator);
159  $bValidator->addPropertyValidator('a', $aValidator);
160  $error = new ‪Error('error1', 123);
161  $result = new ‪Result();
162  $result->addError($error);
163 
165  $mockUuidValidator = $this->getMockBuilder(ValidatorInterface::class)
166  ->setMethods(['validate', 'getOptions'])
167  ->getMock();
168  $mockUuidValidator->expects(self::any())->method('validate')->with(15)->willReturn($result);
169  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
170 
171  self::assertSame(['b.uuid' => [$error]], $aValidator->validate($A)->getFlattenedErrors());
172  }
173 
178  {
179  $A = new class() {
180  public $b;
181  public $uuid = 0xF;
182  };
183 
184  $B = new class() {
185  public $a;
186  public $uuid = 0xF;
187  };
188 
189  $A->b = $B;
190  $B->a = $A;
191  $aValidator = new ‪GenericObjectValidator();
192  $bValidator = new ‪GenericObjectValidator();
193 
194  $aValidator->addPropertyValidator('b', $bValidator);
195  $bValidator->addPropertyValidator('a', $aValidator);
196  $error1 = new ‪Error('error1', 123);
197  $result1 = new ‪Result();
198  $result1->addError($error1);
199 
201  $mockUuidValidator = $this->getMockBuilder(ValidatorInterface::class)
202  ->setMethods(['validate', 'getOptions'])
203  ->getMock();
204  $mockUuidValidator->expects(self::any())->method('validate')->with(15)->willReturn($result1);
205  $aValidator->addPropertyValidator('uuid', $mockUuidValidator);
206  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
207 
208  self::assertSame(['b.uuid' => [$error1], 'uuid' => [$error1]], $aValidator->validate($A)->getFlattenedErrors());
209  }
210 
215  {
216  // Create to test-entities. Use the same uuid to make the same validator trigger on both objects
217  $A = new class() {
218  public $b;
219  public $uuid = 0xF;
220  };
221 
222  $B = new class() {
223  public $a;
224  public $uuid = 0xF;
225  };
226 
227  $A->b = $B;
228  $B->a = $A;
229  $aValidator = new ‪GenericObjectValidator();
230  $bValidator = new ‪GenericObjectValidator();
231 
232  $error1 = new ‪Error('error1', 123);
233  $result1 = new ‪Result();
234  $result1->addError($error1);
235 
237  $mockValidatorUuidNot0xF = $this->getMockBuilder(ValidatorInterface::class)
238  ->setMethods(['validate', 'getOptions'])
239  ->getMock();
240  $mockValidatorUuidNot0xF->expects(self::any())
241  ->method('validate')->with(0xF)->willReturn($result1);
242 
243  $aValidator->addPropertyValidator('uuid', $mockValidatorUuidNot0xF);
244  $bValidator->addPropertyValidator('uuid', $mockValidatorUuidNot0xF);
245  $aValidator->addPropertyValidator('b', $bValidator);
246  $bValidator->addPropertyValidator('a', $aValidator);
247 
248  // assert that the validation error is being reported for both objects
249  self::assertSame(
250  ['uuid' => [$error1], 'b.uuid' => [$error1], 'b.a.uuid' => [$error1]],
251  $aValidator->validate($A)->getFlattenedErrors()
252  );
253  }
254 }
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateDetectsFailuresInRecursiveTargetsI
‪validateDetectsFailuresInRecursiveTargetsI()
Definition: GenericObjectValidatorTest.php:142
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest
Definition: GenericObjectValidatorTest.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateDetectsFailuresInRecursiveTargetsIII
‪validateDetectsFailuresInRecursiveTargetsIII()
Definition: GenericObjectValidatorTest.php:214
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateChecksAllPropertiesForWhichAPropertyValidatorExists
‪validateChecksAllPropertiesForWhichAPropertyValidatorExists($objectToBeValidated, $validationResultForFoo, $validationResultForBar, $errors)
Definition: GenericObjectValidatorTest.php:92
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:25
‪$errors
‪$errors
Definition: annotationChecker.php:121
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\dataProviderForValidator
‪array dataProviderForValidator()
Definition: GenericObjectValidatorTest.php:50
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull
‪validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull()
Definition: GenericObjectValidatorTest.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateDetectsFailuresInRecursiveTargetsII
‪validateDetectsFailuresInRecursiveTargetsII()
Definition: GenericObjectValidatorTest.php:177
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:16
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validatorShouldReturnNoErrorsIfTheValueIsNull
‪validatorShouldReturnNoErrorsIfTheValueIsNull()
Definition: GenericObjectValidatorTest.php:42
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateCanHandleRecursiveTargetsWithoutEndlessLooping
‪validateCanHandleRecursiveTargetsWithoutEndlessLooping()
Definition: GenericObjectValidatorTest.php:117