7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Validate
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
26 require_once dirname(__FILE__
) . '/../TestHelper.php';
31 require_once 'Zend/Validate.php';
34 * @see Zend_Validate_Abstract
36 require_once 'Zend/Validate/Abstract.php';
41 require_once 'Zend/Translate.php';
45 * @package Zend_Validate
46 * @subpackage UnitTests
47 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
48 * @license http://framework.zend.com/license/new-bsd New BSD License
49 * @group Zend_Validate
51 class Zend_ValidateTest
extends PHPUnit_Framework_TestCase
54 * Zend_Validate object
58 protected $_validator;
61 * Creates a new Zend_Validate object for each test method
65 public function setUp()
67 $this->_validator
= new Zend_Validate();
71 * Resets the default namespaces
75 public function tearDown()
77 Zend_Validate
::setDefaultNamespaces(array());
81 * Ensures expected results from empty validator chain
85 public function testEmpty()
87 $this->assertEquals(array(), $this->_validator
->getMessages());
88 $this->assertEquals(array(), $this->_validator
->getErrors());
89 $this->assertTrue($this->_validator
->isValid('something'));
90 $this->assertEquals(array(), $this->_validator
->getErrors());
94 * Ensures expected behavior from a validator known to succeed
98 public function testTrue()
100 $this->_validator
->addValidator(new Zend_ValidateTest_True());
101 $this->assertTrue($this->_validator
->isValid(null));
102 $this->assertEquals(array(), $this->_validator
->getMessages());
103 $this->assertEquals(array(), $this->_validator
->getErrors());
107 * Ensures expected behavior from a validator known to fail
111 public function testFalse()
113 $this->_validator
->addValidator(new Zend_ValidateTest_False());
114 $this->assertFalse($this->_validator
->isValid(null));
115 $this->assertEquals(array('error' => 'validation failed'), $this->_validator
->getMessages());
119 * Ensures that a validator may break the chain
123 public function testBreakChainOnFailure()
125 $this->_validator
->addValidator(new Zend_ValidateTest_False(), true)
126 ->addValidator(new Zend_ValidateTest_False());
127 $this->assertFalse($this->_validator
->isValid(null));
128 $this->assertEquals(array('error' => 'validation failed'), $this->_validator
->getMessages());
132 * Ensures that we can call the static method is()
133 * to instantiate a named validator by its class basename
134 * and it returns the result of isValid() with the input.
136 public function testStaticFactory()
138 $this->assertTrue(Zend_Validate
::is('1234', 'Digits'));
139 $this->assertFalse(Zend_Validate
::is('abc', 'Digits'));
143 * Ensures that a validator with constructor arguments can be called
144 * with the static method is().
146 public function testStaticFactoryWithConstructorArguments()
148 $this->assertTrue(Zend_Validate
::is('12', 'Between', array('min' => 1, 'max' => 12)));
149 $this->assertFalse(Zend_Validate
::is('24', 'Between', array('min' => 1, 'max' => 12)));
153 * Ensures that if we specify a validator class basename that doesn't
154 * exist in the namespace, is() throws an exception.
156 * Refactored to conform with ZF-2724.
160 * @expectedException Zend_Validate_Exception
162 public function testStaticFactoryClassNotFound()
164 Zend_Validate
::is('1234', 'UnknownValidator');
172 public function testNamespaces()
174 $this->assertEquals(array(), Zend_Validate
::getDefaultNamespaces());
175 $this->assertFalse(Zend_Validate
::hasDefaultNamespaces());
177 Zend_Validate
::setDefaultNamespaces('TestDir');
178 $this->assertEquals(array('TestDir'), Zend_Validate
::getDefaultNamespaces());
180 Zend_Validate
::setDefaultNamespaces('OtherTestDir');
181 $this->assertEquals(array('OtherTestDir'), Zend_Validate
::getDefaultNamespaces());
183 $this->assertTrue(Zend_Validate
::hasDefaultNamespaces());
185 Zend_Validate
::setDefaultNamespaces(array());
187 $this->assertEquals(array(), Zend_Validate
::getDefaultNamespaces());
188 $this->assertFalse(Zend_Validate
::hasDefaultNamespaces());
190 Zend_Validate
::addDefaultNamespaces(array('One', 'Two'));
191 $this->assertEquals(array('One', 'Two'), Zend_Validate
::getDefaultNamespaces());
193 Zend_Validate
::addDefaultNamespaces('Three');
194 $this->assertEquals(array('One', 'Two', 'Three'), Zend_Validate
::getDefaultNamespaces());
196 Zend_Validate
::setDefaultNamespaces(array());
199 public function testIsValidWithParameters()
201 $this->assertTrue(Zend_Validate
::is(5, 'Between', array(1, 10)));
202 $this->assertTrue(Zend_Validate
::is(5, 'Between', array('min' => 1, 'max' => 10)));
205 public function testSetGetMessageLengthLimitation()
207 Zend_Validate
::setMessageLength(5);
208 $this->assertEquals(5, Zend_Validate
::getMessageLength());
210 $valid = new Zend_Validate_Between(1, 10);
211 $this->assertFalse($valid->isValid(24));
212 $message = current($valid->getMessages());
213 $this->assertTrue(strlen($message) <= 5);
216 public function testSetGetDefaultTranslator()
218 set_error_handler(array($this, 'errorHandlerIgnore'));
219 $translator = new Zend_Translate('array', array(), 'en');
220 restore_error_handler();
221 Zend_Validate_Abstract
::setDefaultTranslator($translator);
222 $this->assertSame($translator->getAdapter(), Zend_Validate_Abstract
::getDefaultTranslator());
226 * Handle file not found errors
230 * @param string $errstr
233 public function handleNotFoundError($errnum, $errstr)
235 if (strstr($errstr, 'No such file')) {
241 * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
243 * @param integer $errno
244 * @param string $errstr
245 * @param string $errfile
246 * @param integer $errline
247 * @param array $errcontext
250 public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $errcontext)
252 $this->_errorOccurred
= true;
258 * Validator to return true to any input.
260 class Zend_ValidateTest_True
extends Zend_Validate_Abstract
262 public function isValid($value)
270 * Validator to return false to any input.
272 class Zend_ValidateTest_False
extends Zend_Validate_Abstract
274 public function isValid($value)
276 $this->_messages
= array('error' => 'validation failed');