[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / ConjunctionValidatorTest.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 use TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException;
24
25 /**
26 * Test case
27 */
28 class ConjunctionValidatorTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
29 {
30 /**
31 * @test
32 */
33 public function addingValidatorsToAJunctionValidatorWorks()
34 {
35 $proxyClassName = $this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class);
36 $conjunctionValidator = new $proxyClassName([]);
37 $mockValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
38 $conjunctionValidator->addValidator($mockValidator);
39 $this->assertTrue($conjunctionValidator->_get('validators')->contains($mockValidator));
40 }
41
42 /**
43 * @test
44 */
45 public function allValidatorsInTheConjunctionAreCalledEvenIfOneReturnsError()
46 {
47 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
48 $validatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
49 ->setMethods(['validate', 'getOptions'])
50 ->getMock();
51 $validatorObject->expects($this->once())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
52 $errors = new \TYPO3\CMS\Extbase\Error\Result();
53 $errors->addError(new \TYPO3\CMS\Extbase\Error\Error('Error', 123));
54 $secondValidatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
55 ->setMethods(['validate', 'getOptions'])
56 ->getMock();
57 $secondValidatorObject->expects($this->once())->method('validate')->will($this->returnValue($errors));
58 $thirdValidatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
59 ->setMethods(['validate', 'getOptions'])
60 ->getMock();
61 $thirdValidatorObject->expects($this->once())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
62 $validatorConjunction->addValidator($validatorObject);
63 $validatorConjunction->addValidator($secondValidatorObject);
64 $validatorConjunction->addValidator($thirdValidatorObject);
65 $validatorConjunction->validate('some subject');
66 }
67
68 /**
69 * @test
70 */
71 public function validatorConjunctionReturnsNoErrorsIfAllJunctionedValidatorsReturnNoErrors()
72 {
73 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
74 $validatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
75 ->setMethods(['validate', 'getOptions'])
76 ->getMock();
77 $validatorObject->expects($this->any())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
78 $secondValidatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
79 ->setMethods(['validate', 'getOptions'])
80 ->getMock();
81 $secondValidatorObject->expects($this->any())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
82 $validatorConjunction->addValidator($validatorObject);
83 $validatorConjunction->addValidator($secondValidatorObject);
84 $this->assertFalse($validatorConjunction->validate('some subject')->hasErrors());
85 }
86
87 /**
88 * @test
89 */
90 public function validatorConjunctionReturnsErrorsIfOneValidatorReturnsErrors()
91 {
92 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
93 $validatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
94 ->setMethods(['validate', 'getOptions'])
95 ->getMock();
96 $errors = new \TYPO3\CMS\Extbase\Error\Result();
97 $errors->addError(new \TYPO3\CMS\Extbase\Error\Error('Error', 123));
98 $validatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors));
99 $validatorConjunction->addValidator($validatorObject);
100 $this->assertTrue($validatorConjunction->validate('some subject')->hasErrors());
101 }
102
103 /**
104 * @test
105 */
106 public function removingAValidatorOfTheValidatorConjunctionWorks()
107 {
108 /** @var \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\Components\TestingFramework\Core\AccessibleObjectInterface */
109 $validatorConjunction = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class, ['dummy'], [[]], '', true);
110 $validator1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
111 ->setMethods(['validate', 'getOptions'])
112 ->getMock();
113 $validator2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
114 ->setMethods(['validate', 'getOptions'])
115 ->getMock();
116 $validatorConjunction->addValidator($validator1);
117 $validatorConjunction->addValidator($validator2);
118 $validatorConjunction->removeValidator($validator1);
119 $this->assertFalse($validatorConjunction->_get('validators')->contains($validator1));
120 $this->assertTrue($validatorConjunction->_get('validators')->contains($validator2));
121 }
122
123 /**
124 * @test
125 */
126 public function removingANotExistingValidatorIndexThrowsException()
127 {
128 $this->expectException(NoSuchValidatorException::class);
129 $this->expectExceptionCode(1207020177);
130 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
131 $validator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
132 ->setMethods(['validate', 'getOptions'])
133 ->getMock();
134 $validatorConjunction->removeValidator($validator);
135 }
136
137 /**
138 * @test
139 */
140 public function countReturnesTheNumberOfValidatorsContainedInTheConjunction()
141 {
142 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
143 $validator1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
144 ->setMethods(['validate', 'getOptions'])
145 ->getMock();
146 $validator2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
147 ->setMethods(['validate', 'getOptions'])
148 ->getMock();
149 $this->assertSame(0, count($validatorConjunction));
150 $validatorConjunction->addValidator($validator1);
151 $validatorConjunction->addValidator($validator2);
152 $this->assertSame(2, count($validatorConjunction));
153 }
154 }