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