TYPO3 CMS  TYPO3_7-6
ConjunctionValidatorTest.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 {
33  {
34  $proxyClassName = $this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class);
35  $conjunctionValidator = new $proxyClassName([]);
36  $mockValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
37  $conjunctionValidator->addValidator($mockValidator);
38  $this->assertTrue($conjunctionValidator->_get('validators')->contains($mockValidator));
39  }
40 
45  {
46  $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
47  $validatorObject = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
48  $validatorObject->expects($this->once())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
49  $errors = new \TYPO3\CMS\Extbase\Error\Result();
50  $errors->addError(new \TYPO3\CMS\Extbase\Error\Error('Error', 123));
51  $secondValidatorObject = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
52  $secondValidatorObject->expects($this->once())->method('validate')->will($this->returnValue($errors));
53  $thirdValidatorObject = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
54  $thirdValidatorObject->expects($this->once())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
55  $validatorConjunction->addValidator($validatorObject);
56  $validatorConjunction->addValidator($secondValidatorObject);
57  $validatorConjunction->addValidator($thirdValidatorObject);
58  $validatorConjunction->validate('some subject');
59  }
60 
65  {
66  $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
67  $validatorObject = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
68  $validatorObject->expects($this->any())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
69  $secondValidatorObject = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
70  $secondValidatorObject->expects($this->any())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
71  $validatorConjunction->addValidator($validatorObject);
72  $validatorConjunction->addValidator($secondValidatorObject);
73  $this->assertFalse($validatorConjunction->validate('some subject')->hasErrors());
74  }
75 
80  {
81  $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
82  $validatorObject = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
83  $errors = new \TYPO3\CMS\Extbase\Error\Result();
84  $errors->addError(new \TYPO3\CMS\Extbase\Error\Error('Error', 123));
85  $validatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors));
86  $validatorConjunction->addValidator($validatorObject);
87  $this->assertTrue($validatorConjunction->validate('some subject')->hasErrors());
88  }
89 
93  public function removingAValidatorOfTheValidatorConjunctionWorks()
94  {
96  $validatorConjunction = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class, ['dummy'], [[]], '', true);
97  $validator1 = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
98  $validator2 = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
99  $validatorConjunction->addValidator($validator1);
100  $validatorConjunction->addValidator($validator2);
101  $validatorConjunction->removeValidator($validator1);
102  $this->assertFalse($validatorConjunction->_get('validators')->contains($validator1));
103  $this->assertTrue($validatorConjunction->_get('validators')->contains($validator2));
104  }
105 
111  {
112  $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
113  $validator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
114  $validatorConjunction->removeValidator($validator);
115  }
116 
121  {
122  $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
123  $validator1 = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
124  $validator2 = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, ['validate', 'getOptions']);
125  $this->assertSame(0, count($validatorConjunction));
126  $validatorConjunction->addValidator($validator1);
127  $validatorConjunction->addValidator($validator2);
128  $this->assertSame(2, count($validatorConjunction));
129  }
130 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)