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 */
37 public function stgringLengthValidatorReturnsTrueForAStringShorterThanMaxLengthAndLongerThanMinLength() {
38 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
39 $stringLengthValidator->setOptions(array('minimum' => 0, 'maximum' => 50));
40 $this->assertTrue($stringLengthValidator->isValid('this is a very simple string'));
41 }
42
43 /**
44 * @test
45 */
46 public function stringLengthValidatorReturnsFalseForAStringShorterThanThanMinLength() {
47 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
48 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
49 $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
50 }
51
52 /**
53 * @test
54 */
55 public function stringLengthValidatorReturnsFalseForAStringLongerThanThanMaxLength() {
56 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
57 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 10));
58 $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
59 }
60
61 /**
62 * @test
63 */
64 public function stringLengthValidatorReturnsTrueForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
65 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
66 $stringLengthValidator->setOptions(array('minimum' => 5));
67 $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
68 }
69
70 /**
71 * @test
72 */
73 public function stringLengthValidatorReturnsTrueForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
74 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
75 $stringLengthValidator->setOptions(array('maximum' => 100));
76 $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
77 }
78
79 /**
80 * @test
81 */
82 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
83 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
84 $stringLengthValidator->setOptions(array('maximum' => 10));
85 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
86 }
87
88 /**
89 * @test
90 */
91 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
92 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
93 $stringLengthValidator->setOptions(array('minimum' => 10));
94 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
95 }
96
97 /**
98 * @test
99 */
100 public function stringLengthValidatorReturnsTrueIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
101 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
102 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 10));
103 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
104 }
105
106 /**
107 * @test
108 */
109 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMaxLength() {
110 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
111 $stringLengthValidator->setOptions(array('minimum' => 1, 'maximum' => 10));
112 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
113 }
114
115 /**
116 * @test
117 */
118 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMinLength() {
119 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
120 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 100));
121 $this->assertTrue($stringLengthValidator->isValid('1234567890'));
122 }
123
124 /**
125 * @test
126 * @expectedException Tx_Extbase_Validation_Exception_InvalidValidationOptions
127 */
128 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
129 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
130 $stringLengthValidator->setOptions(array('minimum' => 101, 'maximum' => 100));
131 $stringLengthValidator->isValid('1234567890');
132 }
133
134 /**
135 * @test
136 */
137 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
138 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
139 $stringLengthValidator->expects($this->once())->method('addError');
140 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
141
142 $stringLengthValidator->isValid('this is a very short string');
143 }
144
145 /**
146 * @test
147 */
148 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
149 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
150 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
151
152 $className = uniqid('TestClass');
153
154 eval('
155 class ' . $className . ' {
156 public function __toString() {
157 return \'some string\';
158 }
159 }
160 ');
161
162 $object = new $className();
163 $this->assertTrue($stringLengthValidator->isValid($object));
164 }
165
166 /**
167 * @test
168 * @expectedException Tx_Extbase_Validation_Exception_InvalidSubject
169 */
170 public function stringLengthValidatorThrowsAnExceptionIfTheGivenObjectCanNotBeConvertedToAString() {
171 $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
172 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
173
174 $className = uniqid('TestClass');
175
176 eval('
177 class ' . $className . ' {
178 protected $someProperty;
179 }
180 ');
181
182 $object = new $className();
183 $stringLengthValidator->isValid($object);
184 }
185 }
186
187 ?>