2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Testcase for the string length validator
32 class Tx_Extbase_Validation_Validator_StringLengthValidator_testcase
extends Tx_Extbase_Base_testcase
{
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'));
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'));
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'));
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'));
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'));
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'));
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'));
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'));
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'));
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'));
126 * @expectedException Tx_Extbase_Validation_Exception_InvalidValidationOptions
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');
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));
142 $stringLengthValidator->isValid('this is a very short string');
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));
152 $className = uniqid('TestClass');
155 class ' . $className . ' {
156 public function __toString() {
157 return \'some string\';
162 $object = new $className();
163 $this->assertTrue($stringLengthValidator->isValid($object));
168 * @expectedException Tx_Extbase_Validation_Exception_InvalidSubject
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));
174 $className = uniqid('TestClass');
177 class ' . $className . ' {
178 protected $someProperty;
182 $object = new $className();
183 $stringLengthValidator->isValid($object);