TYPO3 CMS  TYPO3_6-2
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';
33 
38 
43 
47  protected $validator;
48 
54  protected function getValidator(array $options = array(), array $mockedMethods = array('translateErrorMessage')) {
55  return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, array($options), '', TRUE);
56  }
57 
61  public function setUp() {
62  $this->mockValidatorResolver = $this->getAccessibleMock(
63  'TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver',
64  array('createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction')
65  );
66  $this->validator = $this->getValidator();
67  $this->configurationManager = $this->getMock(
68  'TYPO3\CMS\Extbase\Configuration\ConfigurationManager',
69  array('isFeatureEnabled'),
70  array(),
71  '',
72  FALSE
73  );
74  $this->configurationManager->expects($this->any())
75  ->method('isFeatureEnabled')
76  ->with('rewrittenPropertyMapper')
77  ->will($this->returnValue(TRUE));
78  $this->validator->injectConfigurationManager($this->configurationManager);
79  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
80  }
81 
86  $this->assertFalse($this->validator->validate(NULL)->hasErrors());
87  }
88 
93  $this->assertTrue($this->validator->validate(new \StdClass())->hasErrors());
94  }
95 
100  $this->validator->_set('options', array('elementValidator' => 'EmailAddress'));
101  $this->mockValidatorResolver->expects($this->exactly(4))
102  ->method('createValidator')
103  ->with('EmailAddress')
104  ->will($this->returnValue($this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\EmailAddressValidator', array('translateErrorMessage'))));
105  $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
106  $arrayOfEmailAddresses = array(
107  'foo@bar.de',
108  'not a valid address',
109  'dummy@typo3.org',
110  'also not valid'
111  );
112 
113  $result = $this->validator->validate($arrayOfEmailAddresses);
114 
115  $this->assertTrue($result->hasErrors());
116  $this->assertSame(2, count($result->getFlattenedErrors()));
117  }
118 
123  $classNameA = $this->getUniqueId('A');
124  eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
125  $classNameB = $this->getUniqueId('B');
126  eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
127  $A = new $classNameA();
128  $B = new $classNameB();
129  $A->b = array($B);
130  $B->a = $A;
131  $B->c = array($A);
132 
133 
134  // Create validators
135  $aValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator', array('translateErrorMessage'), array(array()));
136  $aValidator->injectConfigurationManager($this->configurationManager);
137  $this->validator->_set('options', array('elementValidator' => 'Integer'));
138  $integerValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\IntegerValidator', array('translateErrorMessage'), array(array()));
139 
140  $this->mockValidatorResolver->expects($this->any())
141  ->method('createValidator')
142  ->with('Integer')
143  ->will($this->returnValue($integerValidator));
144  $this->mockValidatorResolver->expects($this->any())
145  ->method('buildBaseValidatorConjunction')
146  ->will($this->returnValue($aValidator));
147 
148  // Add validators to properties
149  $aValidator->addPropertyValidator('b', $this->validator);
150  $aValidator->addPropertyValidator('integer', $integerValidator);
151 
152  $result = $aValidator->validate($A)->getFlattenedErrors();
153  $this->assertEquals(1221560494, $result['b.0'][0]->getCode());
154  }
155 
160  $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
161  $elementType = '\TYPO3\CMS\Extbase\Tests\Fixture\Entity';
162  $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
163  $parentObject,
164  'someProperty',
165  array('someNotEmptyValue')
166  );
167  \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($lazyObjectStorage, 'isInitialized', FALSE, TRUE);
168  // only in this test case we want to mock the isValid method
169  $validator = $this->getValidator(array('elementType' => $elementType), array('isValid'));
170  $validator->expects($this->never())->method('isValid');
171  $validator->injectConfigurationManager($this->configurationManager);
172  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
173  $validator->validate($lazyObjectStorage);
174  }
175 
180  $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
181  $elementType = '\TYPO3\CMS\Extbase\Tests\Fixture\Entity';
182  $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
183  $objectStorage->attach($entity);
184  $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
185  $aValidator->injectConfigurationManager($this->configurationManager);
186 
187  $this->mockValidatorResolver->expects($this->never())->method('createValidator');
188  $this->mockValidatorResolver->expects($this->once())
189  ->method('getBaseValidatorConjunction')
190  ->with($elementType)
191  ->will($this->returnValue($aValidator));
192 
193  $this->validator->_set('options', array('elementType' => $elementType));
194 
195  $this->validator->validate($objectStorage);
196  }
197 
198 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=FALSE)
getValidator(array $options=array(), array $mockedMethods=array('translateErrorMessage'))