Extbase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Validation / Validator / StringLengthValidator_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for the string length validator
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $Id: $
31 */
32 class Tx_Extbase_Validation_Validator_StringLengthValidator_testcase extends Tx_Extbase_Base_testcase {
33
34 /**
35 * @test
36 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
37 */
38 public function stgringLengthValidatorReturnsTrueForAStringShorterThanMaxLengthAndLongerThanMinLength() {
39 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
40 $stringLengthValidator->setOptions(array('minimum' => 0, 'maximum' => 50));
41 $this->assertTrue($stringLengthValidator->isValid('this is a very simple string'));
42 }
43
44 /**
45 * @test
46 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
47 */
48 public function stringLengthValidatorReturnsFalseForAStringShorterThanThanMinLength() {
49 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
50 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
51 $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
52 }
53
54 /**
55 * @test
56 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
57 */
58 public function stringLengthValidatorReturnsFalseForAStringLongerThanThanMaxLength() {
59 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
60 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 10));
61 $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
62 }
63
64 /**
65 * @test
66 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
67 */
68 public function stringLengthValidatorReturnsTrueForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
69 $stringLengthValidator = $this->getMock('Tx_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 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
77 */
78 public function stringLengthValidatorReturnsTrueForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
79 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
80 $stringLengthValidator->setOptions(array('maximum' => 100));
81 $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
82 }
83
84 /**
85 * @test
86 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
87 */
88 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
89 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
90 $stringLengthValidator->setOptions(array('maximum' => 10));
91 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
92 }
93
94 /**
95 * @test
96 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
97 */
98 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
99 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
100 $stringLengthValidator->setOptions(array('minimum' => 10));
101 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
102 }
103
104 /**
105 * @test
106 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
107 */
108 public function stringLengthValidatorReturnsTrueIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
109 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
110 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 10));
111 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
112 }
113
114 /**
115 * @test
116 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
117 */
118 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMaxLength() {
119 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
120 $stringLengthValidator->setOptions(array('minimum' => 1, 'maximum' => 10));
121 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
122 }
123
124 /**
125 * @test
126 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
127 */
128 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMinLength() {
129 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
130 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 100));
131 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
132 }
133
134 /**
135 * @test
136 * @expectedException Tx_Extbase_Validation_Exception_InvalidValidationOptions
137 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
138 */
139 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
140 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
141 $stringLengthValidator->setOptions(array('minimum' => 101, 'maximum' => 100));
142 $stringLengthValidator->isValid('1234567890');
143 }
144
145 /**
146 * @test
147 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
148 */
149 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
150 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
151 $stringLengthValidator->expects($this->once())->method('addError');
152 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
153
154 $stringLengthValidator->isValid('this is a very short string');
155 }
156
157 /**
158 * @test
159 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
160 */
161 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
162 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
163 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
164
165 $className = uniqid('TestClass');
166
167 eval('
168 class ' . $className . ' {
169 public function __toString() {
170 return \'some string\';
171 }
172 }
173 ');
174
175 $object = new $className();
176 $this->assertTrue($stringLengthValidator->isValid($object));
177 }
178
179 /**
180 * @test
181 * @expectedException Tx_Extbase_Validation_Exception_InvalidSubject
182 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
183 */
184 public function stringLengthValidatorThrowsAnExceptionIfTheGivenObjectCanNotBeConvertedToAString() {
185 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
186 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
187
188 $className = uniqid('TestClass');
189
190 eval('
191 class ' . $className . ' {
192 protected $someProperty;
193 }
194 ');
195
196 $object = new $className();
197 $stringLengthValidator->isValid($object);
198 }
199 }
200
201 ?>