2
0

ValidateTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-2008 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-2008 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. * Ensures expected results from empty validator chain
  60. *
  61. * @return void
  62. */
  63. public function testEmpty()
  64. {
  65. $this->assertEquals(array(), $this->_validator->getMessages());
  66. $this->assertEquals(array(), $this->_validator->getErrors());
  67. $this->assertTrue($this->_validator->isValid('something'));
  68. $this->assertEquals(array(), $this->_validator->getErrors());
  69. }
  70. /**
  71. * Ensures expected behavior from a validator known to succeed
  72. *
  73. * @return void
  74. */
  75. public function testTrue()
  76. {
  77. $this->_validator->addValidator(new Zend_ValidateTest_True());
  78. $this->assertTrue($this->_validator->isValid(null));
  79. $this->assertEquals(array(), $this->_validator->getMessages());
  80. $this->assertEquals(array(), $this->_validator->getErrors());
  81. }
  82. /**
  83. * Ensures expected behavior from a validator known to fail
  84. *
  85. * @return void
  86. */
  87. public function testFalse()
  88. {
  89. $this->_validator->addValidator(new Zend_ValidateTest_False());
  90. $this->assertFalse($this->_validator->isValid(null));
  91. $this->assertEquals(array('error' => 'validation failed'), $this->_validator->getMessages());
  92. }
  93. /**
  94. * Ensures that a validator may break the chain
  95. *
  96. * @return void
  97. */
  98. public function testBreakChainOnFailure()
  99. {
  100. $this->_validator->addValidator(new Zend_ValidateTest_False(), true)
  101. ->addValidator(new Zend_ValidateTest_False());
  102. $this->assertFalse($this->_validator->isValid(null));
  103. $this->assertEquals(array('error' => 'validation failed'), $this->_validator->getMessages());
  104. }
  105. /**
  106. * Ensures that we can call the static method is()
  107. * to instantiate a named validator by its class basename
  108. * and it returns the result of isValid() with the input.
  109. */
  110. public function testStaticFactory()
  111. {
  112. $this->assertTrue(Zend_Validate::is('1234', 'Digits'));
  113. $this->assertFalse(Zend_Validate::is('abc', 'Digits'));
  114. }
  115. /**
  116. * Ensures that a validator with constructor arguments can be called
  117. * with the static method is().
  118. */
  119. public function testStaticFactoryWithConstructorArguments()
  120. {
  121. $this->assertTrue(Zend_Validate::is('12', 'Between', array(1, 12)));
  122. $this->assertFalse(Zend_Validate::is('24', 'Between', array(1, 12)));
  123. }
  124. /**
  125. * Ensures that if we specify a validator class basename that doesn't
  126. * exist in the namespace, is() throws an exception.
  127. *
  128. * Refactored to conform with ZF-2724.
  129. *
  130. * @group ZF-2724
  131. * @return void
  132. */
  133. public function testStaticFactoryClassNotFound()
  134. {
  135. set_error_handler(array($this, 'handleNotFoundError'), E_WARNING);
  136. try {
  137. Zend_Validate::is('1234', 'UnknownValidator');
  138. } catch (Zend_Exception $e) {
  139. }
  140. restore_error_handler();
  141. $this->assertTrue($this->error);
  142. $this->assertTrue(isset($e));
  143. $this->assertContains('Validate class not found', $e->getMessage());
  144. }
  145. /**
  146. * Handle file not found errors
  147. *
  148. * @group ZF-2724
  149. * @param int $errnum
  150. * @param string $errstr
  151. * @return void
  152. */
  153. public function handleNotFoundError($errnum, $errstr)
  154. {
  155. if (strstr($errstr, 'No such file')) {
  156. $this->error = true;
  157. }
  158. }
  159. }
  160. /**
  161. * Validator to return true to any input.
  162. */
  163. class Zend_ValidateTest_True extends Zend_Validate_Abstract
  164. {
  165. public function isValid($value)
  166. {
  167. return true;
  168. }
  169. }
  170. /**
  171. * Validator to return false to any input.
  172. */
  173. class Zend_ValidateTest_False extends Zend_Validate_Abstract
  174. {
  175. public function isValid($value)
  176. {
  177. $this->_messages = array('error' => 'validation failed');
  178. return false;
  179. }
  180. }