[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / CollectionValidatorTest.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\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class CollectionValidatorTest extends UnitTestCase
31 {
32 /**
33 * @var string
34 */
35 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator::class;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
39 */
40 protected $mockValidatorResolver;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
44 */
45 protected $validator;
46
47 /**
48 * @param array $options
49 * @param array $mockedMethods
50 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
51 */
52 protected function getValidator(array $options = [], array $mockedMethods = ['translateErrorMessage'])
53 {
54 return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, [$options], '', true);
55 }
56
57 protected function setUp()
58 {
59 $this->mockValidatorResolver = $this->getAccessibleMock(
60 \TYPO3\CMS\Extbase\Validation\ValidatorResolver::class,
61 ['createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction']
62 );
63 $this->validator = $this->getValidator();
64 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
65 }
66
67 /**
68 * @test
69 */
70 public function collectionValidatorReturnsNoErrorsForANullValue()
71 {
72 $this->assertFalse($this->validator->validate(null)->hasErrors());
73 }
74
75 /**
76 * @test
77 */
78 public function collectionValidatorFailsForAValueNotBeingACollection()
79 {
80 $this->assertTrue($this->validator->validate(new \stdClass())->hasErrors());
81 }
82
83 /**
84 * @test
85 */
86 public function collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator()
87 {
88 // todo: this test is rather complex, consider making it a functional test with fixtures
89
90 $this->validator->_set('options', ['elementValidator' => 'EmailAddress']);
91 $this->mockValidatorResolver->expects($this->exactly(4))
92 ->method('createValidator')
93 ->with('EmailAddress')
94 ->will($this->returnValue(
95 $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator::class)
96 ->setMethods(['translateErrorMessage'])
97 ->getMock()
98 ));
99 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
100 $arrayOfEmailAddresses = [
101 'foo@bar.de',
102 'not a valid address',
103 'dummy@typo3.org',
104 'also not valid'
105 ];
106
107 $result = $this->validator->validate($arrayOfEmailAddresses);
108
109 $this->assertTrue($result->hasErrors());
110 $this->assertCount(2, $result->getFlattenedErrors());
111 }
112
113 /**
114 * @test
115 */
116 public function collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping()
117 {
118 // todo: this test is rather complex, consider making it a functional test with fixtures
119
120 $A = new class() {
121 public $b = [];
122 public $integer = 5;
123 };
124
125 $B = new class() {
126 public $a;
127 public $c;
128 public $integer = 'Not an integer';
129 };
130
131 $A->b = [$B];
132 $B->a = $A;
133 $B->c = [$A];
134
135 // Create validators
136 $aValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class)
137 ->setMethods(['translateErrorMessage'])
138 ->setConstructorArgs([[]])
139 ->getMock();
140 $this->validator->_set('options', ['elementValidator' => 'Integer']);
141 $integerValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator::class)
142 ->setMethods(['translateErrorMessage'])
143 ->setConstructorArgs([[]])
144 ->getMock();
145
146 $this->mockValidatorResolver->expects($this->any())
147 ->method('createValidator')
148 ->with('Integer')
149 ->will($this->returnValue($integerValidator));
150 $this->mockValidatorResolver->expects($this->any())
151 ->method('buildBaseValidatorConjunction')
152 ->will($this->returnValue($aValidator));
153
154 // Add validators to properties
155 $aValidator->addPropertyValidator('b', $this->validator);
156 $aValidator->addPropertyValidator('integer', $integerValidator);
157
158 $result = $aValidator->validate($A)->getFlattenedErrors();
159 $this->assertEquals(1221560494, $result['b.0'][0]->getCode());
160 }
161
162 /**
163 * @test
164 */
165 public function collectionValidatorIsValidEarlyReturnsOnUnitializedLazyObjectStorages()
166 {
167 // todo: this test is rather complex, consider making it a functional test with fixtures
168
169 $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
170 $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
171 $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
172 $parentObject,
173 'someProperty',
174 ['someNotEmptyValue']
175 );
176 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($lazyObjectStorage, 'isInitialized', false, true);
177 // only in this test case we want to mock the isValid method
178 $validator = $this->getValidator(['elementType' => $elementType], ['isValid']);
179 $validator->expects($this->never())->method('isValid');
180 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
181 $validator->validate($lazyObjectStorage);
182 }
183
184 /**
185 * @test
186 */
187 public function collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages()
188 {
189 // todo: this test is rather complex, consider making it a functional test with fixtures
190
191 $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
192 $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
193 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
194 $objectStorage->attach($entity);
195 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
196
197 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
198 $this->mockValidatorResolver->expects($this->once())
199 ->method('getBaseValidatorConjunction')
200 ->with($elementType)
201 ->will($this->returnValue($aValidator));
202
203 $this->validator->_set('options', ['elementType' => $elementType]);
204
205 $this->validator->validate($objectStorage);
206 }
207 }