[!!!][TASK] Remove deprecated Extbase-related code (Part 2)
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Validation / ValidatorResolverTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Functional\Validation;
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\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Extbase\Reflection\ReflectionService;
21 use TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException;
22 use TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator;
23 use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
24 use TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator;
25 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
26 use TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator;
27 use TYPO3\CMS\Extbase\Validation\ValidatorResolver;
28
29 /**
30 * Test case
31 */
32 class ValidatorResolverTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
33 {
34 /**
35 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
36 */
37 private $validatorResolver;
38
39 protected function setUp()
40 {
41 parent::setUp();
42
43 $this->validatorResolver = $this->getAccessibleMock(
44 ValidatorResolver::class,
45 ['dummy']
46 );
47 $this->validatorResolver->injectObjectManager(GeneralUtility::makeInstance(ObjectManager::class));
48 $this->validatorResolver->injectReflectionService(GeneralUtility::makeInstance(ReflectionService::class));
49 }
50
51 /**
52 * @return array
53 */
54 public function namespacedShorthandValidatorNamesDataProvider(): array
55 {
56 return [
57 'TYPO3.CMS.Extbase:NotEmpty' => [
58 'TYPO3.CMS.Extbase:NotEmpty',
59 NotEmptyValidator::class
60 ],
61 'TYPO3.CMS.Extbase.Tests.Functional.Validation.Fixture:Custom' => [
62 'TYPO3.CMS.Extbase.Tests.Functional.Validation.Fixture:Custom',
63 Fixture\Validation\Validator\CustomValidator::class
64 ]
65 ];
66 }
67
68 /**
69 * @test
70 * @dataProvider namespacedShorthandValidatorNamesDataProvider
71 *
72 * @param string $validatorName
73 * @param string $expectedClassName
74 */
75 public function resolveValidatorObjectNameWithShortHandNotationReturnsValidatorNameIfClassExists(string $validatorName, string $expectedClassName)
76 {
77 static::assertEquals(
78 $expectedClassName,
79 $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName)
80 );
81 }
82
83 /**
84 * @test
85 */
86 public function resolveValidatorObjectNameWithShortHandNotationThrowsExceptionIfClassDoesNotExist()
87 {
88 $this->expectException(NoSuchValidatorException::class);
89 $this->expectExceptionCode(1365799920);
90
91 $validatorName = 'TYPO3.CMS.Extbase.Tests.Functional.Validation.Fixture:NonExistentValidator';
92 $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName);
93 }
94
95 /**
96 * @test
97 */
98 public function resolveValidatorObjectNameWithShortHandNotationReturnsValidatorNameIfClassExistsButDoesNotImplementValidatorInterface()
99 {
100 $this->expectException(NoSuchValidatorException::class);
101 $this->expectExceptionCode(1365776838);
102
103 $validatorName = 'TYPO3.CMS.Extbase.Tests.Functional.Validation.Fixture:CustomValidatorThatDoesNotImplementValidatorInterface';
104 $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName);
105 }
106
107 /**
108 * @test
109 */
110 public function resolveValidatorObjectNameReturnsValidatorNameForFullQualifiedValidatorName()
111 {
112 $validatorName = Fixture\Validation\Validator\CustomValidator::class;
113 $className = Fixture\Validation\Validator\CustomValidator::class;
114
115 static::assertEquals(
116 $className,
117 $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName)
118 );
119 }
120
121 /**
122 * @test
123 */
124 public function resolveValidatorObjectNameReturnsValidatorNameForFullQualifiedValidatorNameWithLeadingBackslash()
125 {
126 $validatorName = '\\' . Fixture\Validation\Validator\CustomValidator::class;
127 $className = Fixture\Validation\Validator\CustomValidator::class;
128
129 static::assertEquals(
130 $className,
131 $this->validatorResolver->_call('resolveValidatorObjectName', $validatorName)
132 );
133 }
134
135 /**
136 * @test
137 */
138 public function resolveValidatorObjectNameThrowsNoSuchValidatorExceptionIfClassDoesNotExist()
139 {
140 $className = $this->getUniqueId('Foo\\Bar');
141 $this->expectException(NoSuchValidatorException::class);
142 $this->expectExceptionCode(1365799920);
143 $this->validatorResolver->_call('resolveValidatorObjectName', $className);
144 }
145
146 /**
147 * @test
148 */
149 public function buildBaseValidatorConjunctionAddsValidatorsDefinedByAnnotationsInTheClassToTheReturnedConjunction()
150 {
151 $this->validatorResolver->_call(
152 'buildBaseValidatorConjunction',
153 \TYPO3\CMS\Extbase\Tests\Functional\Validation\Fixture\Domain\Model\Model::class,
154 \TYPO3\CMS\Extbase\Tests\Functional\Validation\Fixture\Domain\Model\Model::class
155 );
156
157 /** @var array $baseValidatorConjunctions */
158 $baseValidatorConjunctions = $this->validatorResolver->_get('baseValidatorConjunctions');
159 static::assertTrue(is_array($baseValidatorConjunctions));
160 static::assertCount(2, $baseValidatorConjunctions);
161 static::assertArrayHasKey(\TYPO3\CMS\Extbase\Tests\Functional\Validation\Fixture\Domain\Model\Model::class, $baseValidatorConjunctions);
162 static::assertArrayHasKey(\TYPO3\CMS\Extbase\Tests\Functional\Validation\Fixture\Domain\Model\AnotherModel::class, $baseValidatorConjunctions);
163
164 /** @var ConjunctionValidator $conjunctionValidator */
165 $conjunctionValidator = $baseValidatorConjunctions[\TYPO3\CMS\Extbase\Tests\Functional\Validation\Fixture\Domain\Model\Model::class];
166 static::assertInstanceOf(ConjunctionValidator::class, $conjunctionValidator);
167
168 $baseValidators = $conjunctionValidator->getValidators();
169 static::assertInstanceOf(\SplObjectStorage::class, $baseValidators);
170 $baseValidators->rewind();
171
172 /** @var GenericObjectValidator $validator */
173 $validator = $baseValidators->current();
174 static::assertInstanceOf(GenericObjectValidator::class, $validator);
175
176 $propertyValidators = $validator->getPropertyValidators();
177 static::assertCount(4, $propertyValidators);
178 static::assertArrayHasKey('foo', $propertyValidators);
179 static::assertArrayHasKey('bar', $propertyValidators);
180 static::assertArrayHasKey('baz', $propertyValidators);
181 static::assertArrayHasKey('qux', $propertyValidators);
182
183 /** @var \SplObjectStorage $fooPropertyValidators */
184 $fooPropertyValidators = $propertyValidators['foo'];
185 static::assertInstanceOf(\SplObjectStorage::class, $fooPropertyValidators);
186 static::assertCount(3, $fooPropertyValidators);
187
188 $fooPropertyValidators->rewind();
189 /** @var StringLengthValidator $propertyValidator */
190 $propertyValidator = $fooPropertyValidators->current();
191 static::assertInstanceOf(StringLengthValidator::class, $propertyValidator);
192 static::assertSame(['minimum' => 1, 'maximum' => PHP_INT_MAX], $propertyValidator->getOptions());
193
194 $fooPropertyValidators->next();
195 /** @var StringLengthValidator $propertyValidator */
196 $propertyValidator = $fooPropertyValidators->current();
197 static::assertInstanceOf(StringLengthValidator::class, $propertyValidator);
198 static::assertSame(['minimum' => 0, 'maximum' => 10], $propertyValidator->getOptions());
199
200 $fooPropertyValidators->next();
201 $propertyValidator = $fooPropertyValidators->current();
202 static::assertInstanceOf(NotEmptyValidator::class, $propertyValidator);
203
204 /** @var \SplObjectStorage $barPropertyValidators */
205 $barPropertyValidators = $propertyValidators['bar'];
206 static::assertInstanceOf(\SplObjectStorage::class, $barPropertyValidators);
207 static::assertCount(1, $barPropertyValidators);
208
209 $barPropertyValidators->rewind();
210 $propertyValidator = $barPropertyValidators->current();
211 static::assertInstanceOf(Fixture\Validation\Validator\CustomValidator::class, $propertyValidator);
212
213 /** @var \SplObjectStorage $bazPropertyValidators */
214 $bazPropertyValidators = $propertyValidators['baz'];
215 static::assertInstanceOf(\SplObjectStorage::class, $bazPropertyValidators);
216 static::assertCount(1, $bazPropertyValidators);
217
218 $bazPropertyValidators->rewind();
219 $propertyValidator = $bazPropertyValidators->current();
220 static::assertInstanceOf(CollectionValidator::class, $propertyValidator);
221
222 /** @var \SplObjectStorage $quxPropertyValidators */
223 $quxPropertyValidators = $propertyValidators['qux'];
224 static::assertInstanceOf(\SplObjectStorage::class, $quxPropertyValidators);
225 static::assertCount(1, $quxPropertyValidators);
226
227 $quxPropertyValidators->rewind();
228 $propertyValidator = $quxPropertyValidators->current();
229 static::assertInstanceOf(ConjunctionValidator::class, $propertyValidator);
230 static::assertSame(
231 $baseValidatorConjunctions[\TYPO3\CMS\Extbase\Tests\Functional\Validation\Fixture\Domain\Model\AnotherModel::class],
232 $propertyValidator
233 );
234
235 $baseValidators->next();
236 $validator = $baseValidators->current();
237 static::assertInstanceOf(Fixture\Domain\Validator\ModelValidator::class, $validator);
238 }
239 }