‪TYPO3CMS  10.4
CollectionValidatorTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪CollectionValidatorTest extends UnitTestCase
34 {
38  protected ‪$validatorClassName = CollectionValidator::class;
39 
43  protected ‪$mockValidatorResolver;
44 
48  protected ‪$validator;
49 
55  protected function ‪getValidator(array $options = [], array $mockedMethods = ['translateErrorMessage'])
56  {
57  return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, [$options], '', true);
58  }
59 
60  protected function ‪setUp(): void
61  {
62  parent::setUp();
63  $this->mockValidatorResolver = $this->getAccessibleMock(
64  ValidatorResolver::class,
65  ['createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction']
66  );
67  $this->validator = $this->‪getValidator();
68  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
69  }
70 
75  {
76  self::assertFalse($this->validator->validate(null)->hasErrors());
77  }
78 
83  {
84  self::assertTrue($this->validator->validate(new \stdClass())->hasErrors());
85  }
86 
91  {
92  // todo: this test is rather complex, consider making it a functional test with fixtures
93 
94  $this->validator->_set('options', ['elementValidator' => 'EmailAddress']);
95  $this->mockValidatorResolver->expects(self::exactly(4))
96  ->method('createValidator')
97  ->with('EmailAddress')
98  ->willReturn(
99  $this->getMockBuilder(EmailAddressValidator::class)
100  ->setMethods(['translateErrorMessage'])
101  ->getMock()
102  );
103  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
104  $arrayOfEmailAddresses = [
105  'foo@bar.de',
106  'not a valid address',
107  'dummy@typo3.org',
108  'also not valid'
109  ];
110 
111  $result = $this->validator->validate($arrayOfEmailAddresses);
112 
113  self::assertTrue($result->hasErrors());
114  self::assertCount(2, $result->getFlattenedErrors());
115  }
116 
121  {
122  // todo: this test is rather complex, consider making it a functional test with fixtures
123 
124  $A = new class() {
125  public $b = [];
126  public $integer = 5;
127  };
128 
129  $B = new class() {
130  public $a;
131  public $c;
132  public $integer = 'Not an integer';
133  };
134 
135  $A->b = [$B];
136  $B->a = $A;
137  $B->c = [$A];
138 
139  // Create validators
140  $aValidator = $this->getMockBuilder(GenericObjectValidator::class)
141  ->setMethods(['translateErrorMessage'])
142  ->setConstructorArgs([[]])
143  ->getMock();
144  $this->validator->_set('options', ['elementValidator' => 'Integer']);
145  $integerValidator = $this->getMockBuilder(IntegerValidator::class)
146  ->setMethods(['translateErrorMessage'])
147  ->setConstructorArgs([[]])
148  ->getMock();
149 
150  $this->mockValidatorResolver->expects(self::any())
151  ->method('createValidator')
152  ->with('Integer')
153  ->willReturn($integerValidator);
154  $this->mockValidatorResolver->expects(self::any())
155  ->method('buildBaseValidatorConjunction')
156  ->willReturn($aValidator);
157 
158  // Add validators to properties
159  $aValidator->addPropertyValidator('b', $this->validator);
160  $aValidator->addPropertyValidator('integer', $integerValidator);
161 
162  $result = $aValidator->validate($A)->getFlattenedErrors();
163  self::assertEquals(1221560494, $result['b.0'][0]->getCode());
164  }
165 
170  {
171  // todo: this test is rather complex, consider making it a functional test with fixtures
172 
173  $parentObject = new Entity('Foo');
174  $elementType = Entity::class;
175  $lazyObjectStorage = new LazyObjectStorage(
176  $parentObject,
177  'someProperty',
178  ['someNotEmptyValue']
179  );
180  // only in this test case we want to mock the isValid method
181  ‪$validator = $this->‪getValidator(['elementType' => $elementType], ['isValid']);
182  ‪$validator->expects(self::never())->method('isValid');
183  $this->mockValidatorResolver->expects(self::never())->method('createValidator');
184  ‪$validator->‪validate($lazyObjectStorage);
185  }
186 
191  {
192  // todo: this test is rather complex, consider making it a functional test with fixtures
193 
194  $entity = new Entity('Foo');
195  $elementType = Entity::class;
196  $objectStorage = new ObjectStorage();
197  $objectStorage->attach($entity);
198  $aValidator = new GenericObjectValidator([]);
199 
200  $this->mockValidatorResolver->expects(self::never())->method('createValidator');
201  $this->mockValidatorResolver->expects(self::once())
202  ->method('getBaseValidatorConjunction')
203  ->with($elementType)
204  ->willReturn($aValidator);
205 
206  $this->validator->_set('options', ['elementType' => $elementType]);
207 
208  $this->validator->validate($objectStorage);
209  }
210 }
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator
Definition: CollectionValidator.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\getValidator
‪PHPUnit Framework MockObject MockObject TYPO3 TestingFramework Core AccessibleObjectInterface getValidator(array $options=[], array $mockedMethods=['translateErrorMessage'])
Definition: CollectionValidatorTest.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$mockValidatorResolver
‪TYPO3 CMS Extbase Validation ValidatorResolver $mockValidatorResolver
Definition: CollectionValidatorTest.php:41
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorReturnsNoErrorsForANullValue
‪collectionValidatorReturnsNoErrorsForANullValue()
Definition: CollectionValidatorTest.php:71
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$validatorClassName
‪string $validatorClassName
Definition: CollectionValidatorTest.php:37
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:35
‪TYPO3\CMS\Extbase\Tests\Fixture\Entity
Definition: Entity.php:24
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface\validate
‪TYPO3 CMS Extbase Error Result validate($value)
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator
Definition: EmailAddressValidator.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorIsValidEarlyReturnsOnUninitializedLazyObjectStorages
‪collectionValidatorIsValidEarlyReturnsOnUninitializedLazyObjectStorages()
Definition: CollectionValidatorTest.php:166
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest
Definition: CollectionValidatorTest.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\setUp
‪setUp()
Definition: CollectionValidatorTest.php:57
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping
‪collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping()
Definition: CollectionValidatorTest.php:117
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:16
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator
‪collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator()
Definition: CollectionValidatorTest.php:87
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorFailsForAValueNotBeingACollection
‪collectionValidatorFailsForAValueNotBeingACollection()
Definition: CollectionValidatorTest.php:79
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$validator
‪TYPO3 CMS Extbase Validation Validator ValidatorInterface $validator
Definition: CollectionValidatorTest.php:45
‪TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator
Definition: IntegerValidator.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages
‪collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages()
Definition: CollectionValidatorTest.php:187