[CLEANUP] Adjust code to coding guidelines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / BeforeExtbase14 / StringLengthValidatorTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\BeforeExtbase14;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Testcase for the string length validator
32 *
33 * This testcase checks the expected behavior for Extbase < 1.4.0, to make sure
34 * we do not break backwards compatibility.
35 */
36 class StringLengthValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
37
38 /**
39 * @test
40 */
41 public function stgringLengthValidatorReturnsTrueForAStringShorterThanMaxLengthAndLongerThanMinLength() {
42 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
43 $stringLengthValidator->setOptions(array('minimum' => 0, 'maximum' => 50));
44 $this->assertTrue($stringLengthValidator->isValid('this is a very simple string'));
45 }
46
47 /**
48 * @test
49 */
50 public function stringLengthValidatorReturnsFalseForAStringShorterThanThanMinLength() {
51 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
52 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
53 $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
54 }
55
56 /**
57 * @test
58 */
59 public function stringLengthValidatorReturnsFalseForAStringLongerThanThanMaxLength() {
60 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
61 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 10));
62 $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
63 }
64
65 /**
66 * @test
67 */
68 public function stringLengthValidatorReturnsTrueForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
69 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
70 $stringLengthValidator->setOptions(array('minimum' => 5));
71 $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
72 }
73
74 /**
75 * @test
76 */
77 public function stringLengthValidatorReturnsTrueForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
78 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
79 $stringLengthValidator->setOptions(array('maximum' => 100));
80 $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
81 }
82
83 /**
84 * @test
85 */
86 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
87 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
88 $stringLengthValidator->setOptions(array('maximum' => 10));
89 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
90 }
91
92 /**
93 * @test
94 */
95 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
96 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
97 $stringLengthValidator->setOptions(array('minimum' => 10));
98 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
99 }
100
101 /**
102 * @test
103 */
104 public function stringLengthValidatorReturnsTrueIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
105 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
106 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 10));
107 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
108 }
109
110 /**
111 * @test
112 */
113 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMaxLength() {
114 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
115 $stringLengthValidator->setOptions(array('minimum' => 1, 'maximum' => 10));
116 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
117 }
118
119 /**
120 * @test
121 */
122 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMinLength() {
123 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
124 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 100));
125 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
126 }
127
128 /**
129 * @test
130 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
131 */
132 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
133 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
134 $stringLengthValidator->setOptions(array('minimum' => 101, 'maximum' => 100));
135 $stringLengthValidator->isValid('1234567890');
136 }
137
138 /**
139 * @test
140 */
141 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
142 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
143 $stringLengthValidator->expects($this->once())->method('addError');
144 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
145 $stringLengthValidator->isValid('this is a very short string');
146 }
147
148 /**
149 * @test
150 */
151 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
152 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
153 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
154 $className = uniqid('TestClass');
155 eval('
156 class ' . $className . ' {
157 public function __toString() {
158 return \'some string\';
159 }
160 }
161 ');
162 $object = new $className();
163 $this->assertTrue($stringLengthValidator->isValid($object));
164 }
165
166 /**
167 * @test
168 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidSubjectException
169 */
170 public function stringLengthValidatorThrowsAnExceptionIfTheGivenObjectCanNotBeConvertedToAString() {
171 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
172 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
173 $className = uniqid('TestClass');
174 eval('
175 class ' . $className . ' {
176 protected $someProperty;
177 }
178 ');
179 $object = new $className();
180 $stringLengthValidator->isValid($object);
181 }
182 }
183
184 ?>