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