TYPO3 CMS  TYPO3_6-2
GenericObjectValidatorTest.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 
30 
31  protected $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator';
32 
37 
38  public function setUp() {
39  parent::setUp();
40 
41  $this->configurationManager = $this->getMock('TYPO3\CMS\Extbase\Configuration\ConfigurationManager', array('isFeatureEnabled'), array(), '', FALSE);
42  $this->configurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(TRUE));
43  $this->validator->injectConfigurationManager($this->configurationManager);
44  }
45 
51  $this->assertTrue($this->validator->validate('foo')->hasErrors());
52  }
53 
59  $this->assertFalse($this->validator->validate(NULL)->hasErrors());
60  }
61 
65  public function dataProviderForValidator() {
66  $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 1);
67  $error2 = new \TYPO3\CMS\Extbase\Error\Error('error2', 2);
68  $emptyResult1 = new \TYPO3\CMS\Extbase\Error\Result();
69  $emptyResult2 = new \TYPO3\CMS\Extbase\Error\Result();
70  $resultWithError1 = new \TYPO3\CMS\Extbase\Error\Result();
71  $resultWithError1->addError($error1);
72  $resultWithError2 = new \TYPO3\CMS\Extbase\Error\Result();
73  $resultWithError2->addError($error2);
74  $classNameForObjectWithPrivateProperties = $this->getUniqueId('B');
75  eval('class ' . $classNameForObjectWithPrivateProperties . '{ protected $foo = \'foovalue\'; protected $bar = \'barvalue\'; }');
76  $objectWithPrivateProperties = new $classNameForObjectWithPrivateProperties();
77  return array(
78  // If no errors happened, this is shown
79  array($objectWithPrivateProperties, $emptyResult1, $emptyResult2, array()),
80  // If errors on two properties happened, they are merged together.
81  array($objectWithPrivateProperties, $resultWithError1, $resultWithError2, array('foo' => array($error1), 'bar' => array($error2)))
82  );
83  }
84 
94  public function validateChecksAllPropertiesForWhichAPropertyValidatorExists($mockObject, $validationResultForFoo, $validationResultForBar, $errors) {
95  $validatorForFoo = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
96  $validatorForFoo->expects($this->once())->method('validate')->with('foovalue')->will($this->returnValue($validationResultForFoo));
97  $validatorForBar = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
98  $validatorForBar->expects($this->once())->method('validate')->with('barvalue')->will($this->returnValue($validationResultForBar));
99  $this->validator->addPropertyValidator('foo', $validatorForFoo);
100  $this->validator->addPropertyValidator('bar', $validatorForBar);
101  $this->assertEquals($errors, $this->validator->validate($mockObject)->getFlattenedErrors());
102  }
103 
109  $classNameA = $this->getUniqueId('B');
110  eval('class ' . $classNameA . '{ public $b; }');
111  $classNameB = $this->getUniqueId('B');
112  eval('class ' . $classNameB . '{ public $a; }');
113  $A = new $classNameA();
114  $B = new $classNameB();
115  $A->b = $B;
116  $B->a = $A;
117 
118  $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
119  $bValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
120 
121  $aValidator->injectConfigurationManager($this->configurationManager);
122  $bValidator->injectConfigurationManager($this->configurationManager);
123 
124  $aValidator->addPropertyValidator('b', $bValidator);
125  $bValidator->addPropertyValidator('a', $aValidator);
126  $this->assertFalse($aValidator->validate($A)->hasErrors());
127  }
128 
134  $classNameA = $this->getUniqueId('A');
135  eval('class ' . $classNameA . '{ public $b; }');
136  $classNameB = $this->getUniqueId('B');
137  eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
138  $A = new $classNameA();
139  $B = new $classNameB();
140  $A->b = $B;
141  $B->a = $A;
142  $aValidator = $this->getValidator();
143  $bValidator = $this->getValidator();
144 
145  $aValidator->injectConfigurationManager($this->configurationManager);
146  $bValidator->injectConfigurationManager($this->configurationManager);
147 
148  $aValidator->addPropertyValidator('b', $bValidator);
149  $bValidator->addPropertyValidator('a', $aValidator);
150  $error = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
151  $result = new \TYPO3\CMS\Extbase\Error\Result();
152  $result->addError($error);
153  $mockUuidValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
154  $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result));
155  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
156  $this->assertSame(array('b.uuid' => array($error)), $aValidator->validate($A)->getFlattenedErrors());
157  }
158 
164  $classNameA = $this->getUniqueId('A');
165  eval('class ' . $classNameA . '{ public $b; public $uuid = 0xF; }');
166  $classNameB = $this->getUniqueId('B');
167  eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
168  $A = new $classNameA();
169  $B = new $classNameB();
170  $A->b = $B;
171  $B->a = $A;
172  $aValidator = $this->getValidator();
173  $bValidator = $this->getValidator();
174 
175  $aValidator->injectConfigurationManager($this->configurationManager);
176  $bValidator->injectConfigurationManager($this->configurationManager);
177 
178  $aValidator->addPropertyValidator('b', $bValidator);
179  $bValidator->addPropertyValidator('a', $aValidator);
180  $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
181  $result1 = new \TYPO3\CMS\Extbase\Error\Result();
182  $result1->addError($error1);
183  $mockUuidValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
184  $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result1));
185  $aValidator->addPropertyValidator('uuid', $mockUuidValidator);
186  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
187  $this->assertSame(array('b.uuid' => array($error1), 'uuid' => array($error1)), $aValidator->validate($A)->getFlattenedErrors());
188  }
189 }
validateChecksAllPropertiesForWhichAPropertyValidatorExists($mockObject, $validationResultForFoo, $validationResultForBar, $errors)
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.