[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / GenericObjectValidatorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
4
5 /* *
6 * This script belongs to the Extbase framework. *
7 * *
8 * It is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU Lesser General Public License as published by the *
10 * Free Software Foundation, either version 3 of the License, or (at your *
11 * option) any later version. *
12 * *
13 * This script is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
15 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
16 * General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with the script. *
20 * If not, see http://www.gnu.org/licenses/lgpl.html *
21 * *
22 * The TYPO3 project - inspiring people to share! *
23 * */
24
25 use TYPO3\CMS\Extbase\Error\Error;
26 use TYPO3\CMS\Extbase\Error\Result;
27 use TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator;
28 use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30
31 /**
32 * Testcase
33 */
34 class GenericObjectValidatorTest extends UnitTestCase
35 {
36 /**
37 * @test
38 */
39 public function validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull()
40 {
41 $this->assertTrue((new GenericObjectValidator())->validate('foo')->hasErrors());
42 }
43
44 /**
45 * @test
46 */
47 public function validatorShouldReturnNoErrorsIfTheValueIsNull()
48 {
49 $this->assertFalse((new GenericObjectValidator())->validate(null)->hasErrors());
50 }
51
52 /**
53 * @return array
54 */
55 public function dataProviderForValidator(): array
56 {
57 $error1 = new Error('error1', 1);
58 $error2 = new Error('error2', 2);
59 $emptyResult1 = new Result();
60 $emptyResult2 = new Result();
61 $resultWithError1 = new Result();
62 $resultWithError1->addError($error1);
63 $resultWithError2 = new Result();
64 $resultWithError2->addError($error2);
65 $objectWithPrivateProperties = new class() {
66 protected $foo = 'foovalue';
67 protected $bar = 'barvalue';
68 };
69
70 return [
71 // If no errors happened, this is shown
72 [$objectWithPrivateProperties, $emptyResult1, $emptyResult2, []],
73 // If errors on two properties happened, they are merged together.
74 [$objectWithPrivateProperties, $resultWithError1, $resultWithError2, ['foo' => [$error1], 'bar' => [$error2]]]
75 ];
76 }
77
78 /**
79 * @test
80 * @dataProvider dataProviderForValidator
81 *
82 * @param mixed $objectToBeValidated
83 * @param mixed $validationResultForFoo
84 * @param mixed $validationResultForBar
85 * @param mixed $errors
86 */
87 public function validateChecksAllPropertiesForWhichAPropertyValidatorExists($objectToBeValidated, $validationResultForFoo, $validationResultForBar, $errors)
88 {
89 $validator = new GenericObjectValidator();
90
91 /** @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject $validatorForFoo */
92 $validatorForFoo = $this->getMockBuilder(ValidatorInterface::class)
93 ->setMethods(['validate', 'getOptions'])
94 ->getMock();
95 $validatorForFoo->expects($this->once())->method('validate')->with('foovalue')->will($this->returnValue($validationResultForFoo));
96
97 /** @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject $validatorForBar */
98 $validatorForBar = $this->getMockBuilder(ValidatorInterface::class)
99 ->setMethods(['validate', 'getOptions'])
100 ->getMock();
101 $validatorForBar->expects($this->once())->method('validate')->with('barvalue')->will($this->returnValue($validationResultForBar));
102
103 $validator->addPropertyValidator('foo', $validatorForFoo);
104 $validator->addPropertyValidator('bar', $validatorForBar);
105
106 $this->assertEquals($errors, $validator->validate($objectToBeValidated)->getFlattenedErrors());
107 }
108
109 /**
110 * @test
111 */
112 public function validateCanHandleRecursiveTargetsWithoutEndlessLooping()
113 {
114 $A = new class() {
115 public $b;
116 };
117
118 $B = new class() {
119 public $a;
120 };
121
122 $A->b = $B;
123 $B->a = $A;
124
125 $aValidator = new GenericObjectValidator();
126 $bValidator = new GenericObjectValidator();
127
128 $aValidator->addPropertyValidator('b', $bValidator);
129 $bValidator->addPropertyValidator('a', $aValidator);
130
131 $this->assertFalse($aValidator->validate($A)->hasErrors());
132 }
133
134 /**
135 * @test
136 */
137 public function validateDetectsFailuresInRecursiveTargetsI()
138 {
139 $A = new class() {
140 public $b;
141 };
142
143 $B = new class() {
144 public $a;
145 public $uuid = 0xF;
146 };
147
148 $A->b = $B;
149 $B->a = $A;
150 $aValidator = new GenericObjectValidator();
151 $bValidator = new GenericObjectValidator();
152
153 $aValidator->addPropertyValidator('b', $bValidator);
154 $bValidator->addPropertyValidator('a', $aValidator);
155 $error = new Error('error1', 123);
156 $result = new Result();
157 $result->addError($error);
158
159 /** @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject $mockUuidValidator */
160 $mockUuidValidator = $this->getMockBuilder(ValidatorInterface::class)
161 ->setMethods(['validate', 'getOptions'])
162 ->getMock();
163 $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result));
164 $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
165
166 $this->assertSame(['b.uuid' => [$error]], $aValidator->validate($A)->getFlattenedErrors());
167 }
168
169 /**
170 * @test
171 */
172 public function validateDetectsFailuresInRecursiveTargetsII()
173 {
174 $A = new class() {
175 public $b;
176 public $uuid = 0xF;
177 };
178
179 $B = new class() {
180 public $a;
181 public $uuid = 0xF;
182 };
183
184 $A->b = $B;
185 $B->a = $A;
186 $aValidator = new GenericObjectValidator();
187 $bValidator = new GenericObjectValidator();
188
189 $aValidator->addPropertyValidator('b', $bValidator);
190 $bValidator->addPropertyValidator('a', $aValidator);
191 $error1 = new Error('error1', 123);
192 $result1 = new Result();
193 $result1->addError($error1);
194
195 /** @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject $mockUuidValidator */
196 $mockUuidValidator = $this->getMockBuilder(ValidatorInterface::class)
197 ->setMethods(['validate', 'getOptions'])
198 ->getMock();
199 $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result1));
200 $aValidator->addPropertyValidator('uuid', $mockUuidValidator);
201 $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
202
203 $this->assertSame(['b.uuid' => [$error1], 'uuid' => [$error1]], $aValidator->validate($A)->getFlattenedErrors());
204 }
205 }