TYPO3 CMS  TYPO3_7-6
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::class;
32 
33  protected function setUp()
34  {
35  parent::setUp();
36  }
37 
42  {
43  $this->assertTrue($this->validator->validate('foo')->hasErrors());
44  }
45 
50  {
51  $this->assertFalse($this->validator->validate(null)->hasErrors());
52  }
53 
57  public function dataProviderForValidator()
58  {
59  $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 1);
60  $error2 = new \TYPO3\CMS\Extbase\Error\Error('error2', 2);
61  $emptyResult1 = new \TYPO3\CMS\Extbase\Error\Result();
62  $emptyResult2 = new \TYPO3\CMS\Extbase\Error\Result();
63  $resultWithError1 = new \TYPO3\CMS\Extbase\Error\Result();
64  $resultWithError1->addError($error1);
65  $resultWithError2 = new \TYPO3\CMS\Extbase\Error\Result();
66  $resultWithError2->addError($error2);
67  $classNameForObjectWithPrivateProperties = $this->getUniqueId('B');
68  eval('class ' . $classNameForObjectWithPrivateProperties . '{ protected $foo = \'foovalue\'; protected $bar = \'barvalue\'; }');
69  $objectWithPrivateProperties = new $classNameForObjectWithPrivateProperties();
70  return [
71  // If no errors happened, this is shown
72  [$objectWithPrivateProperties, $emptyResult1, $emptyResult2, []],
73  // If errors on two properties happened, they are merged together.
74  [$objectWithPrivateProperties, $resultWithError1, $resultWithError2, ['foo' => [$error1], 'bar' => [$error2]]]
75  ];
76  }
77 
86  public function validateChecksAllPropertiesForWhichAPropertyValidatorExists($mockObject, $validationResultForFoo, $validationResultForBar, $errors)
87  {
88  $validatorForFoo = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
89  $validatorForFoo->expects($this->once())->method('validate')->with('foovalue')->will($this->returnValue($validationResultForFoo));
90  $validatorForBar = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
91  $validatorForBar->expects($this->once())->method('validate')->with('barvalue')->will($this->returnValue($validationResultForBar));
92  $this->validator->addPropertyValidator('foo', $validatorForFoo);
93  $this->validator->addPropertyValidator('bar', $validatorForBar);
94  $this->assertEquals($errors, $this->validator->validate($mockObject)->getFlattenedErrors());
95  }
96 
101  {
102  $classNameA = $this->getUniqueId('B');
103  eval('class ' . $classNameA . '{ public $b; }');
104  $classNameB = $this->getUniqueId('B');
105  eval('class ' . $classNameB . '{ public $a; }');
106  $A = new $classNameA();
107  $B = new $classNameB();
108  $A->b = $B;
109  $B->a = $A;
110 
111  $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
112  $bValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
113 
114  $aValidator->addPropertyValidator('b', $bValidator);
115  $bValidator->addPropertyValidator('a', $aValidator);
116  $this->assertFalse($aValidator->validate($A)->hasErrors());
117  }
118 
123  {
124  $classNameA = $this->getUniqueId('A');
125  eval('class ' . $classNameA . '{ public $b; }');
126  $classNameB = $this->getUniqueId('B');
127  eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
128  $A = new $classNameA();
129  $B = new $classNameB();
130  $A->b = $B;
131  $B->a = $A;
132  $aValidator = $this->getValidator();
133  $bValidator = $this->getValidator();
134 
135  $aValidator->addPropertyValidator('b', $bValidator);
136  $bValidator->addPropertyValidator('a', $aValidator);
137  $error = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
138  $result = new \TYPO3\CMS\Extbase\Error\Result();
139  $result->addError($error);
140  $mockUuidValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
141  $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result));
142  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
143  $this->assertSame(['b.uuid' => [$error]], $aValidator->validate($A)->getFlattenedErrors());
144  }
145 
150  {
151  $classNameA = $this->getUniqueId('A');
152  eval('class ' . $classNameA . '{ public $b; public $uuid = 0xF; }');
153  $classNameB = $this->getUniqueId('B');
154  eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
155  $A = new $classNameA();
156  $B = new $classNameB();
157  $A->b = $B;
158  $B->a = $A;
159  $aValidator = $this->getValidator();
160  $bValidator = $this->getValidator();
161 
162  $aValidator->addPropertyValidator('b', $bValidator);
163  $bValidator->addPropertyValidator('a', $aValidator);
164  $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
165  $result1 = new \TYPO3\CMS\Extbase\Error\Result();
166  $result1->addError($error1);
167  $mockUuidValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
168  $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result1));
169  $aValidator->addPropertyValidator('uuid', $mockUuidValidator);
170  $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
171  $this->assertSame(['b.uuid' => [$error1], 'uuid' => [$error1]], $aValidator->validate($A)->getFlattenedErrors());
172  }
173 }
validateChecksAllPropertiesForWhichAPropertyValidatorExists($mockObject, $validationResultForFoo, $validationResultForBar, $errors)