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