‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪GenericObjectValidatorTest extends UnitTestCase
29 {
30  #[Test]
32  {
33  self::assertTrue((new ‪GenericObjectValidator())->validate('foo')->hasErrors());
34  }
35 
36  #[Test]
38  {
39  self::assertFalse((new ‪GenericObjectValidator())->validate(null)->hasErrors());
40  }
41 
42  public static function ‪dataProviderForValidator(): array
43  {
44  $error1 = new ‪Error('error1', 1);
45  $error2 = new ‪Error('error2', 2);
46  $emptyResult1 = new ‪Result();
47  $emptyResult2 = new ‪Result();
48  $resultWithError1 = new ‪Result();
49  $resultWithError1->addError($error1);
50  $resultWithError2 = new ‪Result();
51  $resultWithError2->addError($error2);
52  $objectWithPrivateProperties = new class () {
53  protected $foo = 'foovalue';
54  protected $bar = 'barvalue';
55 
56  public function getFoo(): string
57  {
58  return $this->foo;
59  }
60 
61  public function getBar(): string
62  {
63  return $this->bar;
64  }
65  };
66 
67  return [
68  // If no errors happened, this is shown
69  [$objectWithPrivateProperties, $emptyResult1, $emptyResult2, []],
70  // If errors on two properties happened, they are merged together.
71  [$objectWithPrivateProperties, $resultWithError1, $resultWithError2, ['foo' => [$error1], 'bar' => [$error2]]],
72  ];
73  }
74 
81  #[DataProvider('dataProviderForValidator')]
82  #[Test]
83  public function ‪validateChecksAllPropertiesForWhichAPropertyValidatorExists($objectToBeValidated, $validationResultForFoo, $validationResultForBar, ‪$errors): void
84  {
86 
87  $validatorForFoo = $this->getMockBuilder(ValidatorInterface::class)
88  ->onlyMethods(['validate', 'getOptions', 'setOptions'])
89  ->getMock();
90  $validatorForFoo->expects(self::once())->method('validate')->with('foovalue')->willReturn($validationResultForFoo);
91 
92  $validatorForBar = $this->getMockBuilder(ValidatorInterface::class)
93  ->onlyMethods(['validate', 'getOptions', 'setOptions'])
94  ->getMock();
95  $validatorForBar->expects(self::once())->method('validate')->with('barvalue')->willReturn($validationResultForBar);
96 
97  ‪$validator->addPropertyValidator('foo', $validatorForFoo);
98  ‪$validator->addPropertyValidator('bar', $validatorForBar);
99 
100  self::assertEquals(‪$errors, ‪$validator->validate($objectToBeValidated)->getFlattenedErrors());
101  }
102 
103  #[Test]
105  {
106  $A = new class () {
107  public $b;
108  };
109 
110  $B = new class () {
111  public $a;
112  };
113 
114  $A->b = $B;
115  $B->a = $A;
116 
117  $aValidator = new ‪GenericObjectValidator();
118  $bValidator = new ‪GenericObjectValidator();
119 
120  $aValidator->addPropertyValidator('b', $bValidator);
121  $bValidator->addPropertyValidator('a', $aValidator);
122 
123  self::assertFalse($aValidator->validate($A)->hasErrors());
124  }
125 
126  #[Test]
128  {
129  $A = new class () {
130  public $b;
131  };
132 
133  $B = new class () {
134  public $a;
135  public $uuid = 0xF;
136  };
137 
138  $A->b = $B;
139  $B->a = $A;
140  $aValidator = new ‪GenericObjectValidator();
141  $bValidator = new ‪GenericObjectValidator();
142 
143  $aValidator->addPropertyValidator('b', $bValidator);
144  $bValidator->addPropertyValidator('a', $aValidator);
145  $error = new ‪Error('error1', 123);
146  $result = new ‪Result();
147  $result->addError($error);
148 
149  $mockUuidValidator = $this->getMockBuilder(ValidatorInterface::class)
150  ->onlyMethods(['validate', 'getOptions', 'setOptions'])
151  ->getMock();
152  $mockUuidValidator->method('validate')->with(15)->willReturn($result);
153  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
154 
155  self::assertSame(['b.uuid' => [$error]], $aValidator->validate($A)->getFlattenedErrors());
156  }
157 
158  #[Test]
160  {
161  $A = new class () {
162  public $b;
163  public $uuid = 0xF;
164  };
165 
166  $B = new class () {
167  public $a;
168  public $uuid = 0xF;
169  };
170 
171  $A->b = $B;
172  $B->a = $A;
173  $aValidator = new ‪GenericObjectValidator();
174  $bValidator = new ‪GenericObjectValidator();
175 
176  $aValidator->addPropertyValidator('b', $bValidator);
177  $bValidator->addPropertyValidator('a', $aValidator);
178  $error1 = new ‪Error('error1', 123);
179  $result1 = new ‪Result();
180  $result1->addError($error1);
181 
182  $mockUuidValidator = $this->getMockBuilder(ValidatorInterface::class)
183  ->onlyMethods(['validate', 'getOptions', 'setOptions'])
184  ->getMock();
185  $mockUuidValidator->method('validate')->with(15)->willReturn($result1);
186  $aValidator->addPropertyValidator('uuid', $mockUuidValidator);
187  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
188 
189  self::assertSame(['b.uuid' => [$error1], 'uuid' => [$error1]], $aValidator->validate($A)->getFlattenedErrors());
190  }
191 
192  #[Test]
194  {
195  // Create to test-entities. Use the same uuid to make the same validator trigger on both objects
196  $A = new class () {
197  public $b;
198  public $uuid = 0xF;
199  };
200 
201  $B = new class () {
202  public $a;
203  public $uuid = 0xF;
204  };
205 
206  $A->b = $B;
207  $B->a = $A;
208  $aValidator = new ‪GenericObjectValidator();
209  $bValidator = new ‪GenericObjectValidator();
210 
211  $error1 = new ‪Error('error1', 123);
212  $result1 = new ‪Result();
213  $result1->addError($error1);
214 
215  $mockValidatorUuidNot0xF = $this->getMockBuilder(ValidatorInterface::class)
216  ->onlyMethods(['validate', 'getOptions', 'setOptions'])
217  ->getMock();
218  $mockValidatorUuidNot0xF
219  ->method('validate')->with(0xF)->willReturn($result1);
220 
221  $aValidator->addPropertyValidator('uuid', $mockValidatorUuidNot0xF);
222  $bValidator->addPropertyValidator('uuid', $mockValidatorUuidNot0xF);
223  $aValidator->addPropertyValidator('b', $bValidator);
224  $bValidator->addPropertyValidator('a', $aValidator);
225 
226  // assert that the validation error is being reported for both objects
227  self::assertSame(
228  ['uuid' => [$error1], 'b.uuid' => [$error1], 'b.a.uuid' => [$error1]],
229  $aValidator->validate($A)->getFlattenedErrors()
230  );
231  }
232 }
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateDetectsFailuresInRecursiveTargetsI
‪validateDetectsFailuresInRecursiveTargetsI()
Definition: GenericObjectValidatorTest.php:127
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest
Definition: GenericObjectValidatorTest.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateDetectsFailuresInRecursiveTargetsIII
‪validateDetectsFailuresInRecursiveTargetsIII()
Definition: GenericObjectValidatorTest.php:193
‪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:83
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:262
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:24
‪$errors
‪$errors
Definition: annotationChecker.php:116
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\dataProviderForValidator
‪static dataProviderForValidator()
Definition: GenericObjectValidatorTest.php:42
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull
‪validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull()
Definition: GenericObjectValidatorTest.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateDetectsFailuresInRecursiveTargetsII
‪validateDetectsFailuresInRecursiveTargetsII()
Definition: GenericObjectValidatorTest.php:159
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validatorShouldReturnNoErrorsIfTheValueIsNull
‪validatorShouldReturnNoErrorsIfTheValueIsNull()
Definition: GenericObjectValidatorTest.php:37
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\GenericObjectValidatorTest\validateCanHandleRecursiveTargetsWithoutEndlessLooping
‪validateCanHandleRecursiveTargetsWithoutEndlessLooping()
Definition: GenericObjectValidatorTest.php:104