[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / StringLengthValidatorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
4
5 /* *
6 * This script belongs to the Extbase framework. *
7 * *
8 * It is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU Lesser General Public License as published by the *
10 * Free Software Foundation, either version 3 of the License, or (at your *
11 * option) any later version. *
12 * *
13 * This script is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
15 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
16 * General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with the script. *
20 * If not, see http://www.gnu.org/licenses/lgpl.html *
21 * *
22 * The TYPO3 project - inspiring people to share! *
23 * */
24
25 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
26 use TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28
29 /**
30 * Test case
31 */
32 class StringLengthValidatorTest extends UnitTestCase
33 {
34 /**
35 * @test
36 */
37 public function validateReturnsNoErrorIfTheGivenValueIsNull()
38 {
39 $this->assertFalse((new StringLengthValidator())->validate(null)->hasErrors());
40 }
41
42 /**
43 * @test
44 */
45 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
46 {
47 $this->assertFalse((new StringLengthValidator())->validate('')->hasErrors());
48 }
49
50 /**
51 * @test
52 */
53 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
54 {
55 $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 50]);
56
57 $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
58 }
59
60 /**
61 * @test
62 */
63 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
64 {
65 /** @var StringLengthValidator $validator */
66 $validator = $this->getMockBuilder(StringLengthValidator::class)
67 ->setMethods(['translateErrorMessage'])
68 ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
69 ->getMock();
70
71 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
72 }
73
74 /**
75 * @test
76 */
77 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
78 {
79 /** @var StringLengthValidator $validator */
80 $validator = $this->getMockBuilder(StringLengthValidator::class)
81 ->setMethods(['translateErrorMessage'])
82 ->setConstructorArgs([['minimum' => 5, 'maximum' => 10]])
83 ->getMock();
84
85 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
86 }
87
88 /**
89 * @test
90 */
91 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
92 {
93 $validator = new StringLengthValidator(['minimum' => 5]);
94
95 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
96 }
97
98 /**
99 * @test
100 */
101 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
102 {
103 $validator = new StringLengthValidator(['maximum' => 100]);
104
105 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
106 }
107
108 /**
109 * @test
110 */
111 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
112 {
113 $validator = new StringLengthValidator(['maximum' => 10]);
114
115 $this->assertFalse($validator->validate('1234567890')->hasErrors());
116 }
117
118 /**
119 * @test
120 */
121 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
122 {
123 $validator = new StringLengthValidator(['minimum' => 10]);
124
125 $this->assertFalse($validator->validate('1234567890')->hasErrors());
126 }
127
128 /**
129 * @test
130 */
131 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
132 {
133 $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 10]);
134
135 $this->assertFalse($validator->validate('1234567890')->hasErrors());
136 }
137
138 /**
139 * @test
140 */
141 public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
142 {
143 $validator = new StringLengthValidator(['minimum' => 1, 'maximum' => 10]);
144
145 $this->assertFalse($validator->validate('1234567890')->hasErrors());
146 }
147
148 /**
149 * @test
150 */
151 public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
152 {
153 $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 100]);
154
155 $this->assertFalse($validator->validate('1234567890')->hasErrors());
156 }
157
158 /**
159 * @test
160 */
161 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength()
162 {
163 $this->expectException(InvalidValidationOptionsException::class);
164 $this->expectExceptionCode(1238107096);
165
166 /** @var StringLengthValidator $validator */
167 $validator = $this->getMockBuilder(StringLengthValidator::class)
168 ->setMethods(['addError', 'translateErrorMessage'])
169 ->setConstructorArgs([['minimum' => 101, 'maximum' => 100]])
170 ->getMock();
171 $validator->validate('1234567890');
172 }
173
174 /**
175 * @test
176 */
177 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
178 {
179 /** @var StringLengthValidator $validator */
180 $validator = $this->getMockBuilder(StringLengthValidator::class)
181 ->setMethods(['translateErrorMessage'])
182 ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
183 ->getMock();
184
185 $this->assertCount(1, $validator->validate('this is a very short string')->getErrors());
186 }
187
188 /**
189 * @test
190 */
191 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
192 {
193 $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
194 $object = new class() {
195 /** @return string */
196 public function __toString()
197 {
198 return 'some string';
199 }
200 };
201
202 $this->assertFalse($validator->validate($object)->hasErrors());
203 }
204
205 /**
206 * @test
207 */
208 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
209 {
210 $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
211 $object = new class() {
212 };
213
214 $result = $validator->validate($object);
215
216 $this->assertTrue($result->hasErrors());
217 $this->assertCount(1, $result->getErrors());
218
219 /** @var \TYPO3\CMS\Extbase\Validation\Error $error */
220 $error = current($result->getErrors());
221 $this->assertSame(1238110957, $error->getCode());
222 }
223
224 /**
225 * @test
226 */
227 public function validateRegardsMultibyteStringsCorrectly()
228 {
229 $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 8]);
230 $result = $validator->validate('├╝berlang');
231
232 $this->assertFalse($result->hasErrors());
233 }
234 }