[TASK] Replace @expectedException annotation in sysext:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / StringLengthValidatorTest.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\InvalidValidationOptionsException;
24
25 /**
26 * Test case
27 */
28 class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
29 {
30 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator::class;
31
32 public function setup()
33 {
34 $this->validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'));
35 }
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
39 */
40 protected $validator;
41
42 /**
43 * @test
44 */
45 public function validateReturnsNoErrorIfTheGivenValueIsNull()
46 {
47 $this->assertFalse($this->validator->validate(null)->hasErrors());
48 }
49
50 /**
51 * @test
52 */
53 public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
54 {
55 $this->assertFalse($this->validator->validate('')->hasErrors());
56 }
57
58 /**
59 * @test
60 */
61 public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
62 {
63 $options = array('minimum' => 0, 'maximum' => 50);
64 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
65 $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
66 }
67
68 /**
69 * @test
70 */
71 public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
72 {
73 $options = array('minimum' => 50, 'maximum' => 100);
74 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
75 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
76 }
77
78 /**
79 * @test
80 */
81 public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
82 {
83 $options = array('minimum' => 5, 'maximum' => 10);
84 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
85 $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
86 }
87
88 /**
89 * @test
90 */
91 public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
92 {
93 $options = array('minimum' => 5);
94 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
95 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
96 }
97
98 /**
99 * @test
100 */
101 public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
102 {
103 $options = array('maximum' => 100);
104 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
105 $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
106 }
107
108 /**
109 * @test
110 */
111 public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
112 {
113 $options = array('maximum' => 10);
114 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
115 $this->assertFalse($validator->validate('1234567890')->hasErrors());
116 }
117
118 /**
119 * @test
120 */
121 public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
122 {
123 $options = array('minimum' => 10);
124 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
125 $this->assertFalse($validator->validate('1234567890')->hasErrors());
126 }
127
128 /**
129 * @test
130 */
131 public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
132 {
133 $options = array('minimum' => 10, 'maximum' => 10);
134 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
135 $this->assertFalse($validator->validate('1234567890')->hasErrors());
136 }
137
138 /**
139 * @test
140 */
141 public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
142 {
143 $options = array('minimum' => 1, 'maximum' => 10);
144 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
145 $this->assertFalse($validator->validate('1234567890')->hasErrors());
146 }
147
148 /**
149 * @test
150 */
151 public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
152 {
153 $options = array('minimum' => 10, 'maximum' => 100);
154 $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
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 $options = array('minimum' => 101, 'maximum' => 100);
166 $validator = $this->getMock($this->validatorClassName, array('addError', 'translateErrorMessage'), array($options));
167 $validator->validate('1234567890');
168 }
169
170 /**
171 * @test
172 */
173 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
174 {
175 $options = array('minimum' => 50, 'maximum' => 100);
176 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
177 $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
178 }
179
180 /**
181 * @test
182 */
183 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
184 {
185 $options = array('minimum' => 5, 'maximum' => 100);
186 $validator = $this->getMock($this->validatorClassName, array('addError', 'translateErrorMessage'), array($options));
187
188 $className = $this->getUniqueId('TestClass');
189
190 eval('
191 class ' . $className . ' {
192 public function __toString() {
193 return \'some string\';
194 }
195 }
196 ');
197
198 $object = new $className();
199 $this->assertFalse($validator->validate($object)->hasErrors());
200 }
201
202 /**
203 * @test
204 */
205 public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
206 {
207 $options = array('minimum' => 5, 'maximum' => 100);
208 $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
209
210 $className = $this->getUniqueId('TestClass');
211
212 eval('
213 class ' . $className . ' {
214 protected $someProperty;
215 }
216 ');
217
218 $object = new $className();
219 $this->assertTrue($validator->validate($object)->hasErrors());
220 }
221
222 /**
223 * @test
224 */
225 public function validateRegardsMultibyteStringsCorrectly()
226 {
227 // $this->validatorOptions(array('maximum' => 8));
228 // $this->assertFalse($this->validator->validate('├╝berlang')->hasErrors());
229 $this->markTestSkipped('Validator needs to be adjusted regarding multibyte char lengths.');
230 }
231 }