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