ValidateTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../TestHelper.php';
  26. /**
  27. * @see Zend_Validate
  28. */
  29. require_once 'Zend/Validate.php';
  30. /**
  31. * @see Zend_Validate_Abstract
  32. */
  33. require_once 'Zend/Validate/Abstract.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Validate
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_ValidateTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Zend_Validate object
  45. *
  46. * @var Zend_Validate
  47. */
  48. protected $_validator;
  49. /**
  50. * Creates a new Zend_Validate object for each test method
  51. *
  52. * @return void
  53. */
  54. public function setUp()
  55. {
  56. $this->_validator = new Zend_Validate();
  57. }
  58. /**
  59. * Resets the default namespaces
  60. *
  61. * @return void
  62. */
  63. public function tearDown()
  64. {
  65. Zend_Validate::setDefaultNamespaces(array());
  66. }
  67. /**
  68. * Ensures expected results from empty validator chain
  69. *
  70. * @return void
  71. */
  72. public function testEmpty()
  73. {
  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());
  78. }
  79. /**
  80. * Ensures expected behavior from a validator known to succeed
  81. *
  82. * @return void
  83. */
  84. public function testTrue()
  85. {
  86. $this->_validator->addValidator(new Zend_ValidateTest_True());
  87. $this->assertTrue($this->_validator->isValid(null));
  88. $this->assertEquals(array(), $this->_validator->getMessages());
  89. $this->assertEquals(array(), $this->_validator->getErrors());
  90. }
  91. /**
  92. * Ensures expected behavior from a validator known to fail
  93. *
  94. * @return void
  95. */
  96. public function testFalse()
  97. {
  98. $this->_validator->addValidator(new Zend_ValidateTest_False());
  99. $this->assertFalse($this->_validator->isValid(null));
  100. $this->assertEquals(array('error' => 'validation failed'), $this->_validator->getMessages());
  101. }
  102. /**
  103. * Ensures that a validator may break the chain
  104. *
  105. * @return void
  106. */
  107. public function testBreakChainOnFailure()
  108. {
  109. $this->_validator->addValidator(new Zend_ValidateTest_False(), true)
  110. ->addValidator(new Zend_ValidateTest_False());
  111. $this->assertFalse($this->_validator->isValid(null));
  112. $this->assertEquals(array('error' => 'validation failed'), $this->_validator->getMessages());
  113. }
  114. /**
  115. * Ensures that we can call the static method is()
  116. * to instantiate a named validator by its class basename
  117. * and it returns the result of isValid() with the input.
  118. */
  119. public function testStaticFactory()
  120. {
  121. $this->assertTrue(Zend_Validate::is('1234', 'Digits'));
  122. $this->assertFalse(Zend_Validate::is('abc', 'Digits'));
  123. }
  124. /**
  125. * Ensures that a validator with constructor arguments can be called
  126. * with the static method is().
  127. */
  128. public function testStaticFactoryWithConstructorArguments()
  129. {
  130. $this->assertTrue(Zend_Validate::is('12', 'Between', array(1, 12)));
  131. $this->assertFalse(Zend_Validate::is('24', 'Between', array(1, 12)));
  132. }
  133. /**
  134. * Ensures that if we specify a validator class basename that doesn't
  135. * exist in the namespace, is() throws an exception.
  136. *
  137. * Refactored to conform with ZF-2724.
  138. *
  139. * @group ZF-2724
  140. * @return void
  141. */
  142. public function testStaticFactoryClassNotFound()
  143. {
  144. set_error_handler(array($this, 'handleNotFoundError'), E_WARNING);
  145. try {
  146. Zend_Validate::is('1234', 'UnknownValidator');
  147. } catch (Zend_Exception $e) {
  148. }
  149. restore_error_handler();
  150. $this->assertTrue($this->error);
  151. $this->assertTrue(isset($e));
  152. $this->assertContains('Validate class not found', $e->getMessage());
  153. }
  154. /**
  155. * Handle file not found errors
  156. *
  157. * @group ZF-2724
  158. * @param int $errnum
  159. * @param string $errstr
  160. * @return void
  161. */
  162. public function handleNotFoundError($errnum, $errstr)
  163. {
  164. if (strstr($errstr, 'No such file')) {
  165. $this->error = true;
  166. }
  167. }
  168. /**
  169. * Testing Namespaces
  170. *
  171. * @return void
  172. */
  173. public function testNamespaces()
  174. {
  175. $this->assertEquals(array(), Zend_Validate::getDefaultNamespaces());
  176. $this->assertFalse(Zend_Validate::hasDefaultNamespaces());
  177. Zend_Validate::setDefaultNamespaces('TestDir');
  178. $this->assertEquals(array('TestDir'), Zend_Validate::getDefaultNamespaces());
  179. Zend_Validate::setDefaultNamespaces('OtherTestDir');
  180. $this->assertEquals(array('OtherTestDir'), Zend_Validate::getDefaultNamespaces());
  181. $this->assertTrue(Zend_Validate::hasDefaultNamespaces());
  182. Zend_Validate::setDefaultNamespaces(array());
  183. $this->assertEquals(array(), Zend_Validate::getDefaultNamespaces());
  184. $this->assertFalse(Zend_Validate::hasDefaultNamespaces());
  185. Zend_Validate::addDefaultNamespaces(array('One', 'Two'));
  186. $this->assertEquals(array('One', 'Two'), Zend_Validate::getDefaultNamespaces());
  187. Zend_Validate::addDefaultNamespaces('Three');
  188. $this->assertEquals(array('One', 'Two', 'Three'), Zend_Validate::getDefaultNamespaces());
  189. Zend_Validate::setDefaultNamespaces(array());
  190. }
  191. }
  192. /**
  193. * Validator to return true to any input.
  194. */
  195. class Zend_ValidateTest_True extends Zend_Validate_Abstract
  196. {
  197. public function isValid($value)
  198. {
  199. return true;
  200. }
  201. }
  202. /**
  203. * Validator to return false to any input.
  204. */
  205. class Zend_ValidateTest_False extends Zend_Validate_Abstract
  206. {
  207. public function isValid($value)
  208. {
  209. $this->_messages = array('error' => 'validation failed');
  210. return false;
  211. }
  212. }