[TASK] Streamline phpdoc annotations in EXT:extbase
[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 file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException;
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 /**
21 * Test case
22 */
23 class ConjunctionValidatorTest extends UnitTestCase
24 {
25 /**
26 * @test
27 */
28 public function addingValidatorsToAJunctionValidatorWorks()
29 {
30 $proxyClassName = $this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class);
31 $conjunctionValidator = new $proxyClassName([]);
32 $mockValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
33 $conjunctionValidator->addValidator($mockValidator);
34 $this->assertTrue($conjunctionValidator->_get('validators')->contains($mockValidator));
35 }
36
37 /**
38 * @test
39 */
40 public function allValidatorsInTheConjunctionAreCalledEvenIfOneReturnsError()
41 {
42 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
43 $validatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
44 ->setMethods(['validate', 'getOptions'])
45 ->getMock();
46 $validatorObject->expects($this->once())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
47 $errors = new \TYPO3\CMS\Extbase\Error\Result();
48 $errors->addError(new \TYPO3\CMS\Extbase\Error\Error('Error', 123));
49 $secondValidatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
50 ->setMethods(['validate', 'getOptions'])
51 ->getMock();
52 $secondValidatorObject->expects($this->once())->method('validate')->will($this->returnValue($errors));
53 $thirdValidatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
54 ->setMethods(['validate', 'getOptions'])
55 ->getMock();
56 $thirdValidatorObject->expects($this->once())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
57 $validatorConjunction->addValidator($validatorObject);
58 $validatorConjunction->addValidator($secondValidatorObject);
59 $validatorConjunction->addValidator($thirdValidatorObject);
60 $validatorConjunction->validate('some subject');
61 }
62
63 /**
64 * @test
65 */
66 public function validatorConjunctionReturnsNoErrorsIfAllJunctionedValidatorsReturnNoErrors()
67 {
68 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
69 $validatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
70 ->setMethods(['validate', 'getOptions'])
71 ->getMock();
72 $validatorObject->expects($this->any())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
73 $secondValidatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
74 ->setMethods(['validate', 'getOptions'])
75 ->getMock();
76 $secondValidatorObject->expects($this->any())->method('validate')->will($this->returnValue(new \TYPO3\CMS\Extbase\Error\Result()));
77 $validatorConjunction->addValidator($validatorObject);
78 $validatorConjunction->addValidator($secondValidatorObject);
79 $this->assertFalse($validatorConjunction->validate('some subject')->hasErrors());
80 }
81
82 /**
83 * @test
84 */
85 public function validatorConjunctionReturnsErrorsIfOneValidatorReturnsErrors()
86 {
87 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
88 $validatorObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
89 ->setMethods(['validate', 'getOptions'])
90 ->getMock();
91 $errors = new \TYPO3\CMS\Extbase\Error\Result();
92 $errors->addError(new \TYPO3\CMS\Extbase\Error\Error('Error', 123));
93 $validatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors));
94 $validatorConjunction->addValidator($validatorObject);
95 $this->assertTrue($validatorConjunction->validate('some subject')->hasErrors());
96 }
97
98 /**
99 * @test
100 */
101 public function removingAValidatorOfTheValidatorConjunctionWorks()
102 {
103 /** @var \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
104 $validatorConjunction = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class, ['dummy'], [[]], '', true);
105 $validator1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
106 ->setMethods(['validate', 'getOptions'])
107 ->getMock();
108 $validator2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
109 ->setMethods(['validate', 'getOptions'])
110 ->getMock();
111 $validatorConjunction->addValidator($validator1);
112 $validatorConjunction->addValidator($validator2);
113 $validatorConjunction->removeValidator($validator1);
114 $this->assertFalse($validatorConjunction->_get('validators')->contains($validator1));
115 $this->assertTrue($validatorConjunction->_get('validators')->contains($validator2));
116 }
117
118 /**
119 * @test
120 */
121 public function removingANotExistingValidatorIndexThrowsException()
122 {
123 $this->expectException(NoSuchValidatorException::class);
124 $this->expectExceptionCode(1207020177);
125 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
126 $validator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
127 ->setMethods(['validate', 'getOptions'])
128 ->getMock();
129 $validatorConjunction->removeValidator($validator);
130 }
131
132 /**
133 * @test
134 */
135 public function countReturnesTheNumberOfValidatorsContainedInTheConjunction()
136 {
137 $validatorConjunction = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator([]);
138 $validator1 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
139 ->setMethods(['validate', 'getOptions'])
140 ->getMock();
141 $validator2 = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class)
142 ->setMethods(['validate', 'getOptions'])
143 ->getMock();
144 $this->assertSame(0, count($validatorConjunction));
145 $validatorConjunction->addValidator($validator1);
146 $validatorConjunction->addValidator($validator2);
147 $this->assertSame(2, count($validatorConjunction));
148 }
149 }