TYPO3 CMS  TYPO3_8-7
CollectionValidatorTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the Extbase framework. *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License as published by the *
9  * Free Software Foundation, either version 3 of the License, or (at your *
10  * option) any later version. *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
23 
27 class CollectionValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
28 {
32  protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator::class;
33 
38 
42  protected $validator;
43 
49  protected function getValidator(array $options = [], array $mockedMethods = ['translateErrorMessage'])
50  {
51  return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, [$options], '', true);
52  }
53 
56  protected function setUp()
57  {
58  $this->mockValidatorResolver = $this->getAccessibleMock(
59  \TYPO3\CMS\Extbase\Validation\ValidatorResolver::class,
60  ['createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction']
61  );
62  $this->validator = $this->getValidator();
63  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
64  }
65 
70  {
71  $this->assertFalse($this->validator->validate(null)->hasErrors());
72  }
73 
78  {
79  $this->assertTrue($this->validator->validate(new \stdClass())->hasErrors());
80  }
81 
86  {
87  $this->validator->_set('options', ['elementValidator' => 'EmailAddress']);
88  $this->mockValidatorResolver->expects($this->exactly(4))
89  ->method('createValidator')
90  ->with('EmailAddress')
91  ->will($this->returnValue(
92  $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator::class)
93  ->setMethods(['translateErrorMessage'])
94  ->getMock()
95  ));
96  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
97  $arrayOfEmailAddresses = [
98  'foo@bar.de',
99  'not a valid address',
100  'dummy@typo3.org',
101  'also not valid'
102  ];
103 
104  $result = $this->validator->validate($arrayOfEmailAddresses);
105 
106  $this->assertTrue($result->hasErrors());
107  $this->assertSame(2, count($result->getFlattenedErrors()));
108  }
109 
114  {
115  $classNameA = $this->getUniqueId('A');
116  eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
117  $classNameB = $this->getUniqueId('B');
118  eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
119  $A = new $classNameA();
120  $B = new $classNameB();
121  $A->b = [$B];
122  $B->a = $A;
123  $B->c = [$A];
124 
125  // Create validators
126  $aValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class)
127  ->setMethods(['translateErrorMessage'])
128  ->setConstructorArgs([[]])
129  ->getMock();
130  $this->validator->_set('options', ['elementValidator' => 'Integer']);
131  $integerValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator::class)
132  ->setMethods(['translateErrorMessage'])
133  ->setConstructorArgs([[]])
134  ->getMock();
135 
136  $this->mockValidatorResolver->expects($this->any())
137  ->method('createValidator')
138  ->with('Integer')
139  ->will($this->returnValue($integerValidator));
140  $this->mockValidatorResolver->expects($this->any())
141  ->method('buildBaseValidatorConjunction')
142  ->will($this->returnValue($aValidator));
143 
144  // Add validators to properties
145  $aValidator->addPropertyValidator('b', $this->validator);
146  $aValidator->addPropertyValidator('integer', $integerValidator);
147 
148  $result = $aValidator->validate($A)->getFlattenedErrors();
149  $this->assertEquals(1221560494, $result['b.0'][0]->getCode());
150  }
151 
156  {
157  $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
158  $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
159  $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
160  $parentObject,
161  'someProperty',
162  ['someNotEmptyValue']
163  );
164  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($lazyObjectStorage, 'isInitialized', false, true);
165  // only in this test case we want to mock the isValid method
166  $validator = $this->getValidator(['elementType' => $elementType], ['isValid']);
167  $validator->expects($this->never())->method('isValid');
168  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
169  $validator->validate($lazyObjectStorage);
170  }
171 
176  {
177  $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
178  $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
179  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
180  $objectStorage->attach($entity);
181  $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
182 
183  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
184  $this->mockValidatorResolver->expects($this->once())
185  ->method('getBaseValidatorConjunction')
186  ->with($elementType)
187  ->will($this->returnValue($aValidator));
188 
189  $this->validator->_set('options', ['elementType' => $elementType]);
190 
191  $this->validator->validate($objectStorage);
192  }
193 }
getValidator(array $options=[], array $mockedMethods=['translateErrorMessage'])
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=false)