ZF-8222: format class name for class checks
[zend.git] / tests / Zend / ValidateTest.php
blob761f78fa38b2b75efcb0003bb3d3b80fd679566a
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
20 * @version $Id$
23 /**
24 * Test helper
26 require_once dirname(__FILE__) . '/../TestHelper.php';
28 /**
29 * @see Zend_Validate
31 require_once 'Zend/Validate.php';
33 /**
34 * @see Zend_Validate_Abstract
36 require_once 'Zend/Validate/Abstract.php';
38 /**
39 * @see Zend_Translate
41 require_once 'Zend/Translate.php';
43 /**
44 * @category Zend
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
53 /**
54 * Zend_Validate object
56 * @var Zend_Validate
58 protected $_validator;
60 /**
61 * Creates a new Zend_Validate object for each test method
63 * @return void
65 public function setUp()
67 $this->_validator = new Zend_Validate();
70 /**
71 * Resets the default namespaces
73 * @return void
75 public function tearDown()
77 Zend_Validate::setDefaultNamespaces(array());
80 /**
81 * Ensures expected results from empty validator chain
83 * @return void
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());
93 /**
94 * Ensures expected behavior from a validator known to succeed
96 * @return void
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
109 * @return void
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
121 * @return void
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.
158 * @group ZF-2724
159 * @return void
160 * @expectedException Zend_Validate_Exception
162 public function testStaticFactoryClassNotFound()
164 Zend_Validate::is('1234', 'UnknownValidator');
168 * Testing Namespaces
170 * @return void
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
228 * @group ZF-2724
229 * @param int $errnum
230 * @param string $errstr
231 * @return void
233 public function handleNotFoundError($errnum, $errstr)
235 if (strstr($errstr, 'No such file')) {
236 $this->error = true;
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
248 * @return void
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)
264 return true;
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');
277 return false;