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