8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
17
* @package Zend_Validate
18
* @subpackage UnitTests
19
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
20
* @license http://framework.zend.com/license/new-bsd New BSD License
21
* @version $Id: ValidateTest.php 12770 2008-11-22 16:12:23Z matthew $
28
require_once dirname(__FILE__) . '/../TestHelper.php';
33
require_once 'Zend/Validate.php';
36
* @see Zend_Validate_Abstract
38
require_once 'Zend/Validate/Abstract.php';
43
* @package Zend_Validate
44
* @subpackage UnitTests
45
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
46
* @license http://framework.zend.com/license/new-bsd New BSD License
48
class Zend_ValidateTest extends PHPUnit_Framework_TestCase
51
* Zend_Validate object
55
protected $_validator;
58
* Creates a new Zend_Validate object for each test method
62
public function setUp()
64
$this->_validator = new Zend_Validate();
68
* Ensures expected results from empty validator chain
72
public function testEmpty()
74
$this->assertEquals(array(), $this->_validator->getMessages());
75
$this->assertEquals(array(), $this->_validator->getErrors());
76
$this->assertTrue($this->_validator->isValid('something'));
77
$this->assertEquals(array(), $this->_validator->getErrors());
81
* Ensures expected behavior from a validator known to succeed
85
public function testTrue()
87
$this->_validator->addValidator(new Zend_ValidateTest_True());
88
$this->assertTrue($this->_validator->isValid(null));
89
$this->assertEquals(array(), $this->_validator->getMessages());
90
$this->assertEquals(array(), $this->_validator->getErrors());
94
* Ensures expected behavior from a validator known to fail
98
public function testFalse()
100
$this->_validator->addValidator(new Zend_ValidateTest_False());
101
$this->assertFalse($this->_validator->isValid(null));
102
$this->assertEquals(array('error' => 'validation failed'), $this->_validator->getMessages());
106
* Ensures that a validator may break the chain
110
public function testBreakChainOnFailure()
112
$this->_validator->addValidator(new Zend_ValidateTest_False(), true)
113
->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 we can call the static method is()
120
* to instantiate a named validator by its class basename
121
* and it returns the result of isValid() with the input.
123
public function testStaticFactory()
125
$this->assertTrue(Zend_Validate::is('1234', 'Digits'));
126
$this->assertFalse(Zend_Validate::is('abc', 'Digits'));
130
* Ensures that a validator with constructor arguments can be called
131
* with the static method is().
133
public function testStaticFactoryWithConstructorArguments()
135
$this->assertTrue(Zend_Validate::is('12', 'Between', array(1, 12)));
136
$this->assertFalse(Zend_Validate::is('24', 'Between', array(1, 12)));
140
* Ensures that if we specify a validator class basename that doesn't
141
* exist in the namespace, is() throws an exception.
143
* Refactored to conform with ZF-2724.
148
public function testStaticFactoryClassNotFound()
150
set_error_handler(array($this, 'handleNotFoundError'), E_WARNING);
152
Zend_Validate::is('1234', 'UnknownValidator');
153
} catch (Zend_Exception $e) {
155
restore_error_handler();
156
$this->assertTrue($this->error);
157
$this->assertTrue(isset($e));
158
$this->assertContains('Validate class not found', $e->getMessage());
162
* Handle file not found errors
166
* @param string $errstr
169
public function handleNotFoundError($errnum, $errstr)
171
if (strstr($errstr, 'No such file')) {
179
* Validator to return true to any input.
181
class Zend_ValidateTest_True extends Zend_Validate_Abstract
183
public function isValid($value)
191
* Validator to return false to any input.
193
class Zend_ValidateTest_False extends Zend_Validate_Abstract
195
public function isValid($value)
197
$this->_messages = array('error' => 'validation failed');