‪TYPO3CMS  11.5
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 
20 use PHPUnit\Framework\MockObject\MockObject;
32 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
35 class ‪CollectionValidatorTest extends UnitTestCase
36 {
40  protected ‪$mockValidatorResolver;
41 
45  protected ‪$validator;
46 
47  protected function ‪setUp(): void
48  {
49  parent::setUp();
50  $this->resetSingletonInstances = true;
51  $this->mockValidatorResolver = $this->getAccessibleMock(
52  ValidatorResolver::class,
53  ['createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction']
54  );
55  GeneralUtility::setSingletonInstance(ValidatorResolver::class, $this->mockValidatorResolver);
56  $this->validator = $this->getAccessibleMock(CollectionValidator::class, ['translateErrorMessage'], [[]], '', true);
57  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
58  }
59 
64  {
65  self::assertFalse($this->validator->validate(null)->hasErrors());
66  }
67 
72  {
73  self::assertTrue($this->validator->validate(new \stdClass())->hasErrors());
74  }
75 
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(self::exactly(4))
85  ->method('createValidator')
86  ->with('EmailAddress')
87  ->willReturn(
88  $this->getMockBuilder(EmailAddressValidator::class)
89  ->onlyMethods(['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  self::assertTrue($result->hasErrors());
103  self::assertCount(2, $result->getFlattenedErrors());
104  }
105 
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(GenericObjectValidator::class)
130  ->onlyMethods(['translateErrorMessage'])
131  ->setConstructorArgs([[]])
132  ->getMock();
133  $this->validator->_set('options', ['elementValidator' => 'Integer']);
134  $integerValidator = $this->getMockBuilder(IntegerValidator::class)
135  ->onlyMethods(['translateErrorMessage'])
136  ->setConstructorArgs([[]])
137  ->getMock();
138 
139  $this->mockValidatorResolver
140  ->method('createValidator')
141  ->with('Integer')
142  ->willReturn($integerValidator);
143  $this->mockValidatorResolver
144  ->method('buildBaseValidatorConjunction')
145  ->willReturn($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  self::assertEquals(1221560494, $result['b.0'][0]->getCode());
153  }
154 
159  {
160  // todo: this test is rather complex, consider making it a functional test with fixtures
161 
162  $parentObject = new Entity('Foo');
163  $elementType = Entity::class;
164  $lazyObjectStorage = new LazyObjectStorage(
165  $parentObject,
166  'someProperty',
167  ['someNotEmptyValue'],
168  $this->createMock(DataMapper::class)
169  );
170  // only in this test case we want to mock the isValid method
171  ‪$validator = $this->getAccessibleMock(CollectionValidator::class, ['isValid'], [['elementType' => $elementType]], '', true);
172  ‪$validator->expects(self::never())->method('isValid');
173  $this->mockValidatorResolver->expects(self::never())->method('createValidator');
174  ‪$validator->‪validate($lazyObjectStorage);
175  }
176 
181  {
182  // todo: this test is rather complex, consider making it a functional test with fixtures
183 
184  $entity = new Entity('Foo');
185  $elementType = Entity::class;
186  $objectStorage = new ObjectStorage();
187  $objectStorage->attach($entity);
188  $aValidator = new GenericObjectValidator([]);
189 
190  $this->mockValidatorResolver->expects(self::never())->method('createValidator');
191  $this->mockValidatorResolver->expects(self::once())
192  ->method('getBaseValidatorConjunction')
193  ->with($elementType)
194  ->willReturn($aValidator);
195 
196  $this->validator->_set('options', ['elementType' => $elementType]);
197 
198  $this->validator->validate($objectStorage);
199  }
200 }
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator
Definition: CollectionValidator.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorReturnsNoErrorsForANullValue
‪collectionValidatorReturnsNoErrorsForANullValue()
Definition: CollectionValidatorTest.php:61
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$mockValidatorResolver
‪ValidatorResolver MockObject AccessibleObjectInterface $mockValidatorResolver
Definition: CollectionValidatorTest.php:39
‪TYPO3\CMS\Extbase\Tests\Fixture\Entity
Definition: Entity.php:26
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:51
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface\validate
‪TYPO3 CMS Extbase Error Result validate($value)
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:32
‪TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator
Definition: EmailAddressValidator.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorIsValidEarlyReturnsOnUninitializedLazyObjectStorages
‪collectionValidatorIsValidEarlyReturnsOnUninitializedLazyObjectStorages()
Definition: CollectionValidatorTest.php:156
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest
Definition: CollectionValidatorTest.php:36
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\setUp
‪setUp()
Definition: CollectionValidatorTest.php:45
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping
‪collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping()
Definition: CollectionValidatorTest.php:107
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$validator
‪ValidatorInterface MockObject AccessibleObjectInterface $validator
Definition: CollectionValidatorTest.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator
‪collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator()
Definition: CollectionValidatorTest.php:77
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorFailsForAValueNotBeingACollection
‪collectionValidatorFailsForAValueNotBeingACollection()
Definition: CollectionValidatorTest.php:69
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator
Definition: IntegerValidator.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages
‪collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages()
Definition: CollectionValidatorTest.php:178