Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[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 class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Testcase for the string length validator
30 *
31 * @package Extbase
32 * @subpackage extbase
33 * @version $Id: StringLengthValidator_testcase.php 1408 2009-10-08 13:15:09Z jocrau $
34 */
35 class Tx_Extbase_Validation_Validator_StringLengthValidator_testcase extends Tx_Extbase_BaseTestCase {
36
37 /**
38 * @test
39 */
40 public function stgringLengthValidatorReturnsTrueForAStringShorterThanMaxLengthAndLongerThanMinLength() {
41 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
42 $stringLengthValidator->setOptions(array('minimum' => 0, 'maximum' => 50));
43 $this->assertTrue($stringLengthValidator->isValid('this is a very simple string'));
44 }
45
46 /**
47 * @test
48 */
49 public function stringLengthValidatorReturnsFalseForAStringShorterThanThanMinLength() {
50 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
51 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
52 $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
53 }
54
55 /**
56 * @test
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 */
67 public function stringLengthValidatorReturnsTrueForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
68 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
69 $stringLengthValidator->setOptions(array('minimum' => 5));
70 $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
71 }
72
73 /**
74 * @test
75 */
76 public function stringLengthValidatorReturnsTrueForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
77 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
78 $stringLengthValidator->setOptions(array('maximum' => 100));
79 $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
80 }
81
82 /**
83 * @test
84 */
85 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
86 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
87 $stringLengthValidator->setOptions(array('maximum' => 10));
88 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
89 }
90
91 /**
92 * @test
93 */
94 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
95 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
96 $stringLengthValidator->setOptions(array('minimum' => 10));
97 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
98 }
99
100 /**
101 * @test
102 */
103 public function stringLengthValidatorReturnsTrueIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
104 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
105 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 10));
106 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
107 }
108
109 /**
110 * @test
111 */
112 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMaxLength() {
113 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
114 $stringLengthValidator->setOptions(array('minimum' => 1, 'maximum' => 10));
115 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
116 }
117
118 /**
119 * @test
120 */
121 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMinLength() {
122 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
123 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 100));
124 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
125 }
126
127 /**
128 * @test
129 * @expectedException Tx_Extbase_Validation_Exception_InvalidValidationOptions
130 */
131 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
132 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
133 $stringLengthValidator->setOptions(array('minimum' => 101, 'maximum' => 100));
134 $stringLengthValidator->isValid('1234567890');
135 }
136
137 /**
138 * @test
139 */
140 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
141 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
142 $stringLengthValidator->expects($this->once())->method('addError');
143 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
144
145 $stringLengthValidator->isValid('this is a very short string');
146 }
147
148 /**
149 * @test
150 */
151 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
152 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
153 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
154
155 $className = uniqid('TestClass');
156
157 eval('
158 class ' . $className . ' {
159 public function __toString() {
160 return \'some string\';
161 }
162 }
163 ');
164
165 $object = new $className();
166 $this->assertTrue($stringLengthValidator->isValid($object));
167 }
168
169 /**
170 * @test
171 * @expectedException Tx_Extbase_Validation_Exception_InvalidSubject
172 */
173 public function stringLengthValidatorThrowsAnExceptionIfTheGivenObjectCanNotBeConvertedToAString() {
174 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
175 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
176
177 $className = uniqid('TestClass');
178
179 eval('
180 class ' . $className . ' {
181 protected $someProperty;
182 }
183 ');
184
185 $object = new $className();
186 $stringLengthValidator->isValid($object);
187 }
188 }
189
190 ?>