TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
CollectionValidatorTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
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 
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 
57  protected function setUp()
58  {
59  $this->mockValidatorResolver = $this->getAccessibleMock(
60  \TYPO3\CMS\Extbase\Validation\ValidatorResolver::class,
61  ['createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction']
62  );
63  $this->validator = $this->getValidator();
64  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
65  }
66 
71  {
72  $this->assertFalse($this->validator->validate(null)->hasErrors());
73  }
74 
79  {
80  $this->assertTrue($this->validator->validate(new \stdClass())->hasErrors());
81  }
82 
87  {
88  $this->validator->_set('options', ['elementValidator' => 'EmailAddress']);
89  $this->mockValidatorResolver->expects($this->exactly(4))
90  ->method('createValidator')
91  ->with('EmailAddress')
92  ->will($this->returnValue(
93  $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator::class)
94  ->setMethods(['translateErrorMessage'])
95  ->getMock()
96  ));
97  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
98  $arrayOfEmailAddresses = [
99  'foo@bar.de',
100  'not a valid address',
101  'dummy@typo3.org',
102  'also not valid'
103  ];
104 
105  $result = $this->validator->validate($arrayOfEmailAddresses);
106 
107  $this->assertTrue($result->hasErrors());
108  $this->assertSame(2, count($result->getFlattenedErrors()));
109  }
110 
115  {
116  $classNameA = $this->getUniqueId('A');
117  eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
118  $classNameB = $this->getUniqueId('B');
119  eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
120  $A = new $classNameA();
121  $B = new $classNameB();
122  $A->b = [$B];
123  $B->a = $A;
124  $B->c = [$A];
125 
126  // Create validators
127  $aValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class)
128  ->setMethods(['translateErrorMessage'])
129  ->setConstructorArgs([[]])
130  ->getMock();
131  $this->validator->_set('options', ['elementValidator' => 'Integer']);
132  $integerValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator::class)
133  ->setMethods(['translateErrorMessage'])
134  ->setConstructorArgs([[]])
135  ->getMock();
136 
137  $this->mockValidatorResolver->expects($this->any())
138  ->method('createValidator')
139  ->with('Integer')
140  ->will($this->returnValue($integerValidator));
141  $this->mockValidatorResolver->expects($this->any())
142  ->method('buildBaseValidatorConjunction')
143  ->will($this->returnValue($aValidator));
144 
145  // Add validators to properties
146  $aValidator->addPropertyValidator('b', $this->validator);
147  $aValidator->addPropertyValidator('integer', $integerValidator);
148 
149  $result = $aValidator->validate($A)->getFlattenedErrors();
150  $this->assertEquals(1221560494, $result['b.0'][0]->getCode());
151  }
152 
157  {
158  $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
159  $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
160  $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
161  $parentObject,
162  'someProperty',
163  ['someNotEmptyValue']
164  );
165  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($lazyObjectStorage, 'isInitialized', false, true);
166  // only in this test case we want to mock the isValid method
167  $validator = $this->getValidator(['elementType' => $elementType], ['isValid']);
168  $validator->expects($this->never())->method('isValid');
169  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
170  $validator->validate($lazyObjectStorage);
171  }
172 
177  {
178  $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
179  $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
180  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
181  $objectStorage->attach($entity);
182  $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
183 
184  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
185  $this->mockValidatorResolver->expects($this->once())
186  ->method('getBaseValidatorConjunction')
187  ->with($elementType)
188  ->will($this->returnValue($aValidator));
189 
190  $this->validator->_set('options', ['elementType' => $elementType]);
191 
192  $this->validator->validate($objectStorage);
193  }
194 }
getValidator(array $options=[], array $mockedMethods=['translateErrorMessage'])
getAccessibleMock($originalClassName, $methods=[], array $arguments=[], $mockClassName= '', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=false)