[TASK] Add spaces around '=' of 'strict_types=1'
[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
28 /**
29 * Test case
30 */
31 class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
32 {
33 /**
34 * @test
35 */
36 public function validateReturnsNoErrorIfTheGivenValueIsNull()
37 {
38 $this->assertFalse((new StringLengthValidator())->validate(null)->hasErrors());
39 }
40
41 /**
42 * @test
43 */
44 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
45 {
46 $this->assertFalse((new StringLengthValidator())->validate('')->hasErrors());
47 }
48
49 /**
50 * @test
51 */
52 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
53 {
54 $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 50]);
55
56 $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
57 }
58
59 /**
60 * @test
61 */
62 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
63 {
64 /** @var StringLengthValidator $validator */
65 $validator = $this->getMockBuilder(StringLengthValidator::class)
66 ->setMethods(['translateErrorMessage'])
67 ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
68 ->getMock();
69
70 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
71 }
72
73 /**
74 * @test
75 */
76 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
77 {
78 /** @var StringLengthValidator $validator */
79 $validator = $this->getMockBuilder(StringLengthValidator::class)
80 ->setMethods(['translateErrorMessage'])
81 ->setConstructorArgs([['minimum' => 5, 'maximum' => 10]])
82 ->getMock();
83
84 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
85 }
86
87 /**
88 * @test
89 */
90 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
91 {
92 $validator = new StringLengthValidator(['minimum' => 5]);
93
94 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
95 }
96
97 /**
98 * @test
99 */
100 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
101 {
102 $validator = new StringLengthValidator(['maximum' => 100]);
103
104 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
105 }
106
107 /**
108 * @test
109 */
110 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
111 {
112 $validator = new StringLengthValidator(['maximum' => 10]);
113
114 $this->assertFalse($validator->validate('1234567890')->hasErrors());
115 }
116
117 /**
118 * @test
119 */
120 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
121 {
122 $validator = new StringLengthValidator(['minimum' => 10]);
123
124 $this->assertFalse($validator->validate('1234567890')->hasErrors());
125 }
126
127 /**
128 * @test
129 */
130 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
131 {
132 $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 10]);
133
134 $this->assertFalse($validator->validate('1234567890')->hasErrors());
135 }
136
137 /**
138 * @test
139 */
140 public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
141 {
142 $validator = new StringLengthValidator(['minimum' => 1, 'maximum' => 10]);
143
144 $this->assertFalse($validator->validate('1234567890')->hasErrors());
145 }
146
147 /**
148 * @test
149 */
150 public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
151 {
152 $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 100]);
153
154 $this->assertFalse($validator->validate('1234567890')->hasErrors());
155 }
156
157 /**
158 * @test
159 */
160 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength()
161 {
162 $this->expectException(InvalidValidationOptionsException::class);
163 $this->expectExceptionCode(1238107096);
164
165 /** @var StringLengthValidator $validator */
166 $validator = $this->getMockBuilder(StringLengthValidator::class)
167 ->setMethods(['addError', 'translateErrorMessage'])
168 ->setConstructorArgs([['minimum' => 101, 'maximum' => 100]])
169 ->getMock();
170 $validator->validate('1234567890');
171 }
172
173 /**
174 * @test
175 */
176 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
177 {
178 /** @var StringLengthValidator $validator */
179 $validator = $this->getMockBuilder(StringLengthValidator::class)
180 ->setMethods(['translateErrorMessage'])
181 ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
182 ->getMock();
183
184 $this->assertCount(1, $validator->validate('this is a very short string')->getErrors());
185 }
186
187 /**
188 * @test
189 */
190 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
191 {
192 $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
193 $object = new class() {
194 /** @return string */
195 public function __toString()
196 {
197 return 'some string';
198 }
199 };
200
201 $this->assertFalse($validator->validate($object)->hasErrors());
202 }
203
204 /**
205 * @test
206 */
207 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
208 {
209 $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
210 $object = new class() {
211 };
212
213 $result = $validator->validate($object);
214
215 $this->assertTrue($result->hasErrors());
216 $this->assertCount(1, $result->getErrors());
217
218 /** @var \TYPO3\CMS\Extbase\Validation\Error $error */
219 $error = current($result->getErrors());
220 $this->assertSame(1238110957, $error->getCode());
221 }
222
223 /**
224 * @test
225 */
226 public function validateRegardsMultibyteStringsCorrectly()
227 {
228 $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 8]);
229 $result = $validator->validate('├╝berlang');
230
231 $this->assertFalse($result->hasErrors());
232 }
233 }