TYPO3 CMS  TYPO3_7-6
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 
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($this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator::class, ['translateErrorMessage'])));
93  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
94  $arrayOfEmailAddresses = [
95  'foo@bar.de',
96  'not a valid address',
97  'dummy@typo3.org',
98  'also not valid'
99  ];
100 
101  $result = $this->validator->validate($arrayOfEmailAddresses);
102 
103  $this->assertTrue($result->hasErrors());
104  $this->assertSame(2, count($result->getFlattenedErrors()));
105  }
106 
111  {
112  $classNameA = $this->getUniqueId('A');
113  eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
114  $classNameB = $this->getUniqueId('B');
115  eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
116  $A = new $classNameA();
117  $B = new $classNameB();
118  $A->b = [$B];
119  $B->a = $A;
120  $B->c = [$A];
121 
122  // Create validators
123  $aValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class, ['translateErrorMessage'], [[]]);
124  $this->validator->_set('options', ['elementValidator' => 'Integer']);
125  $integerValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator::class, ['translateErrorMessage'], [[]]);
126 
127  $this->mockValidatorResolver->expects($this->any())
128  ->method('createValidator')
129  ->with('Integer')
130  ->will($this->returnValue($integerValidator));
131  $this->mockValidatorResolver->expects($this->any())
132  ->method('buildBaseValidatorConjunction')
133  ->will($this->returnValue($aValidator));
134 
135  // Add validators to properties
136  $aValidator->addPropertyValidator('b', $this->validator);
137  $aValidator->addPropertyValidator('integer', $integerValidator);
138 
139  $result = $aValidator->validate($A)->getFlattenedErrors();
140  $this->assertEquals(1221560494, $result['b.0'][0]->getCode());
141  }
142 
147  {
148  $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
149  $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
150  $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
151  $parentObject,
152  'someProperty',
153  ['someNotEmptyValue']
154  );
155  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($lazyObjectStorage, 'isInitialized', false, true);
156  // only in this test case we want to mock the isValid method
157  $validator = $this->getValidator(['elementType' => $elementType], ['isValid']);
158  $validator->expects($this->never())->method('isValid');
159  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
160  $validator->validate($lazyObjectStorage);
161  }
162 
167  {
168  $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
169  $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
170  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
171  $objectStorage->attach($entity);
172  $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
173 
174  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
175  $this->mockValidatorResolver->expects($this->once())
176  ->method('getBaseValidatorConjunction')
177  ->with($elementType)
178  ->will($this->returnValue($aValidator));
179 
180  $this->validator->_set('options', ['elementType' => $elementType]);
181 
182  $this->validator->validate($objectStorage);
183  }
184 }
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)