TYPO3 API  SVNRelease
StringLengthValidatorTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * Testcase for the string length validator
00030  *
00031  * @package Extbase
00032  * @subpackage extbase
00033  * @version $Id: StringLengthValidator_testcase.php 1408 2009-10-08 13:15:09Z jocrau $
00034  */
00035 class Tx_Extbase_Tests_Unit_Validation_Validator_StringLengthValidatorTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00036 
00037     /**
00038      * @test
00039      */
00040     public function stgringLengthValidatorReturnsTrueForAStringShorterThanMaxLengthAndLongerThanMinLength() {
00041         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00042         $stringLengthValidator->setOptions(array('minimum' => 0, 'maximum' => 50));
00043         $this->assertTrue($stringLengthValidator->isValid('this is a very simple string'));
00044     }
00045 
00046     /**
00047      * @test
00048      */
00049     public function stringLengthValidatorReturnsFalseForAStringShorterThanThanMinLength() {
00050         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00051         $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
00052         $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
00053     }
00054 
00055     /**
00056      * @test
00057      */
00058     public function stringLengthValidatorReturnsFalseForAStringLongerThanThanMaxLength() {
00059         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00060         $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 10));
00061         $this->assertFalse($stringLengthValidator->isValid('this is a very short string'));
00062     }
00063 
00064     /**
00065      * @test
00066      */
00067     public function stringLengthValidatorReturnsTrueForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
00068         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00069         $stringLengthValidator->setOptions(array('minimum' => 5));
00070         $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
00071     }
00072 
00073     /**
00074      * @test
00075      */
00076     public function stringLengthValidatorReturnsTrueForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
00077         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00078         $stringLengthValidator->setOptions(array('maximum' => 100));
00079         $this->assertTrue($stringLengthValidator->isValid('this is a very short string'));
00080     }
00081 
00082     /**
00083      * @test
00084      */
00085     public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
00086         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00087         $stringLengthValidator->setOptions(array('maximum' => 10));
00088         $this->assertTrue($stringLengthValidator->isValid('1234567890'));
00089     }
00090 
00091     /**
00092      * @test
00093      */
00094     public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
00095         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00096         $stringLengthValidator->setOptions(array('minimum' => 10));
00097         $this->assertTrue($stringLengthValidator->isValid('1234567890'));
00098     }
00099 
00100     /**
00101      * @test
00102      */
00103     public function stringLengthValidatorReturnsTrueIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
00104         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00105         $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 10));
00106         $this->assertTrue($stringLengthValidator->isValid('1234567890'));
00107     }
00108 
00109     /**
00110      * @test
00111      */
00112     public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMaxLength() {
00113         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00114         $stringLengthValidator->setOptions(array('minimum' => 1, 'maximum' => 10));
00115         $this->assertTrue($stringLengthValidator->isValid('1234567890'));
00116     }
00117 
00118     /**
00119      * @test
00120      */
00121     public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMinLength() {
00122         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00123         $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 100));
00124         $this->assertTrue($stringLengthValidator->isValid('1234567890'));
00125     }
00126 
00127     /**
00128      * @test
00129      * @expectedException Tx_Extbase_Validation_Exception_InvalidValidationOptions
00130      */
00131     public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
00132         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00133         $stringLengthValidator->setOptions(array('minimum' => 101, 'maximum' => 100));
00134         $stringLengthValidator->isValid('1234567890');
00135     }
00136 
00137     /**
00138      * @test
00139      */
00140     public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
00141         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00142         $stringLengthValidator->expects($this->once())->method('addError');
00143         $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
00144 
00145         $stringLengthValidator->isValid('this is a very short string');
00146     }
00147 
00148     /**
00149      * @test
00150      */
00151     public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
00152         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00153         $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
00154 
00155         $className = uniqid('TestClass');
00156 
00157         eval('
00158             class ' . $className . ' {
00159                 public function __toString() {
00160                     return \'some string\';
00161                 }
00162             }
00163         ');
00164 
00165         $object = new $className();
00166         $this->assertTrue($stringLengthValidator->isValid($object));
00167     }
00168 
00169     /**
00170      * @test
00171      * @expectedException Tx_Extbase_Validation_Exception_InvalidSubject
00172      */
00173     public function stringLengthValidatorThrowsAnExceptionIfTheGivenObjectCanNotBeConvertedToAString() {
00174         $stringLengthValidator = $this->getMock('Tx_Extbase_Validation_Validator_StringLengthValidator', array('addError'), array(), '', FALSE);
00175         $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
00176 
00177         $className = uniqid('TestClass');
00178 
00179         eval('
00180             class ' . $className . ' {
00181                 protected $someProperty;
00182             }
00183         ');
00184 
00185         $object = new $className();
00186         $stringLengthValidator->isValid($object);
00187     }
00188 }
00189 
00190 ?>