‪TYPO3CMS  9.5
CollectionValidatorTest.php
Go to the documentation of this file.
1 <?php
2 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 
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19 
23 class ‪CollectionValidatorTest extends UnitTestCase
24 {
28  protected ‪$validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator::class;
29 
33  protected ‪$mockValidatorResolver;
34 
38  protected ‪$validator;
39 
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 
64  {
65  $this->assertFalse($this->validator->validate(null)->hasErrors());
66  }
67 
72  {
73  $this->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($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 
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 
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 
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 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$mockValidatorResolver
‪TYPO3 CMS Extbase Validation ValidatorResolver $mockValidatorResolver
Definition: CollectionValidatorTest.php:31
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\setProperty
‪static bool setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=false)
Definition: ObjectAccess.php:176
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorReturnsNoErrorsForANullValue
‪collectionValidatorReturnsNoErrorsForANullValue()
Definition: CollectionValidatorTest.php:60
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$validatorClassName
‪string $validatorClassName
Definition: CollectionValidatorTest.php:27
‪TYPO3
‪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:42
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface\validate
‪TYPO3 CMS Extbase Error Result validate($value)
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest
Definition: CollectionValidatorTest.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\setUp
‪setUp()
Definition: CollectionValidatorTest.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping
‪collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping()
Definition: CollectionValidatorTest.php:106
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorIsValidEarlyReturnsOnUnitializedLazyObjectStorages
‪collectionValidatorIsValidEarlyReturnsOnUnitializedLazyObjectStorages()
Definition: CollectionValidatorTest.php:155
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:2
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator
‪collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator()
Definition: CollectionValidatorTest.php:76
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorFailsForAValueNotBeingACollection
‪collectionValidatorFailsForAValueNotBeingACollection()
Definition: CollectionValidatorTest.php:68
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\$validator
‪TYPO3 CMS Extbase Validation Validator ValidatorInterface $validator
Definition: CollectionValidatorTest.php:35
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\CollectionValidatorTest\collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages
‪collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages()
Definition: CollectionValidatorTest.php:177