[TASK] Use name-resolution instead of strings where possible: 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / CollectionValidatorTest.php
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
24 /**
25 * Test case
26 */
27 class CollectionValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
28
29 /**
30 * @var string
31 */
32 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator::class;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
36 */
37 protected $mockValidatorResolver;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
41 */
42 protected $validator;
43
44 /**
45 * @param array $options
46 * @param array $mockedMethods
47 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
48 */
49 protected function getValidator(array $options = array(), array $mockedMethods = array('translateErrorMessage')) {
50 return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, array($options), '', TRUE);
51 }
52
53 /**
54 * @return void
55 */
56 public function setUp() {
57 $this->mockValidatorResolver = $this->getAccessibleMock(
58 \TYPO3\CMS\Extbase\Validation\ValidatorResolver::class,
59 array('createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction')
60 );
61 $this->validator = $this->getValidator();
62 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
63 }
64
65 /**
66 * @test
67 */
68 public function collectionValidatorReturnsNoErrorsForANullValue() {
69 $this->assertFalse($this->validator->validate(NULL)->hasErrors());
70 }
71
72 /**
73 * @test
74 */
75 public function collectionValidatorFailsForAValueNotBeingACollection() {
76 $this->assertTrue($this->validator->validate(new \StdClass())->hasErrors());
77 }
78
79 /**
80 * @test
81 */
82 public function collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator() {
83 $this->validator->_set('options', array('elementValidator' => 'EmailAddress'));
84 $this->mockValidatorResolver->expects($this->exactly(4))
85 ->method('createValidator')
86 ->with('EmailAddress')
87 ->will($this->returnValue($this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator::class, array('translateErrorMessage'))));
88 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
89 $arrayOfEmailAddresses = array(
90 'foo@bar.de',
91 'not a valid address',
92 'dummy@typo3.org',
93 'also not valid'
94 );
95
96 $result = $this->validator->validate($arrayOfEmailAddresses);
97
98 $this->assertTrue($result->hasErrors());
99 $this->assertSame(2, count($result->getFlattenedErrors()));
100 }
101
102 /**
103 * @test
104 */
105 public function collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping() {
106 $classNameA = 'A' . md5(uniqid(mt_rand(), TRUE));
107 eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
108 $classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
109 eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
110 $A = new $classNameA();
111 $B = new $classNameB();
112 $A->b = array($B);
113 $B->a = $A;
114 $B->c = array($A);
115
116
117 // Create validators
118 $aValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class, array('translateErrorMessage'), array(array()));
119 $this->validator->_set('options', array('elementValidator' => 'Integer'));
120 $integerValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator::class, array('translateErrorMessage'), array(array()));
121
122 $this->mockValidatorResolver->expects($this->any())
123 ->method('createValidator')
124 ->with('Integer')
125 ->will($this->returnValue($integerValidator));
126 $this->mockValidatorResolver->expects($this->any())
127 ->method('buildBaseValidatorConjunction')
128 ->will($this->returnValue($aValidator));
129
130 // Add validators to properties
131 $aValidator->addPropertyValidator('b', $this->validator);
132 $aValidator->addPropertyValidator('integer', $integerValidator);
133
134 $result = $aValidator->validate($A)->getFlattenedErrors();
135 $this->assertEquals(1221560494, $result['b.0'][0]->getCode());
136 }
137
138 /**
139 * @test
140 */
141 public function collectionValidatorIsValidEarlyReturnsOnUnitializedLazyObjectStorages() {
142 $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
143 $elementType = '\TYPO3\CMS\Extbase\Tests\Fixture\Entity';
144 $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
145 $parentObject,
146 'someProperty',
147 array('someNotEmptyValue')
148 );
149 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($lazyObjectStorage, 'isInitialized', FALSE, TRUE);
150 // only in this test case we want to mock the isValid method
151 $validator = $this->getValidator(array('elementType' => $elementType), array('isValid'));
152 $validator->expects($this->never())->method('isValid');
153 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
154 $validator->validate($lazyObjectStorage);
155 }
156
157 /**
158 * @test
159 */
160 public function collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages() {
161 $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
162 $elementType = '\TYPO3\CMS\Extbase\Tests\Fixture\Entity';
163 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
164 $objectStorage->attach($entity);
165 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
166
167 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
168 $this->mockValidatorResolver->expects($this->once())
169 ->method('getBaseValidatorConjunction')
170 ->with($elementType)
171 ->will($this->returnValue($aValidator));
172
173 $this->validator->_set('options', array('elementType' => $elementType));
174
175 $this->validator->validate($objectStorage);
176 }
177
178 }