2
0

ValidateTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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-2010 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Validate
  41. */
  42. class Zend_ValidateTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Zend_Validate object
  46. *
  47. * @var Zend_Validate
  48. */
  49. protected $_validator;
  50. /**
  51. * Creates a new Zend_Validate object for each test method
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. $this->_validator = new Zend_Validate();
  58. }
  59. /**
  60. * Resets the default namespaces
  61. *
  62. * @return void
  63. */
  64. public function tearDown()
  65. {
  66. Zend_Validate::setDefaultNamespaces(array());
  67. }
  68. /**
  69. * Ensures expected results from empty validator chain
  70. *
  71. * @return void
  72. */
  73. public function testEmpty()
  74. {
  75. $this->assertEquals(array(), $this->_validator->getMessages());
  76. $this->assertEquals(array(), $this->_validator->getErrors());
  77. $this->assertTrue($this->_validator->isValid('something'));
  78. $this->assertEquals(array(), $this->_validator->getErrors());
  79. }
  80. /**
  81. * Ensures expected behavior from a validator known to succeed
  82. *
  83. * @return void
  84. */
  85. public function testTrue()
  86. {
  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());
  91. }
  92. /**
  93. * Ensures expected behavior from a validator known to fail
  94. *
  95. * @return void
  96. */
  97. public function testFalse()
  98. {
  99. $this->_validator->addValidator(new Zend_ValidateTest_False());
  100. $this->assertFalse($this->_validator->isValid(null));
  101. $this->assertEquals(array('error' => 'validation failed'), $this->_validator->getMessages());
  102. }
  103. /**
  104. * Ensures that a validator may break the chain
  105. *
  106. * @return void
  107. */
  108. public function testBreakChainOnFailure()
  109. {
  110. $this->_validator->addValidator(new Zend_ValidateTest_False(), true)
  111. ->addValidator(new Zend_ValidateTest_False());
  112. $this->assertFalse($this->_validator->isValid(null));
  113. $this->assertEquals(array('error' => 'validation failed'), $this->_validator->getMessages());
  114. }
  115. /**
  116. * Ensures that we can call the static method is()
  117. * to instantiate a named validator by its class basename
  118. * and it returns the result of isValid() with the input.
  119. */
  120. public function testStaticFactory()
  121. {
  122. $this->assertTrue(Zend_Validate::is('1234', 'Digits'));
  123. $this->assertFalse(Zend_Validate::is('abc', 'Digits'));
  124. }
  125. /**
  126. * Ensures that a validator with constructor arguments can be called
  127. * with the static method is().
  128. */
  129. public function testStaticFactoryWithConstructorArguments()
  130. {
  131. $this->assertTrue(Zend_Validate::is('12', 'Between', array('min' => 1, 'max' => 12)));
  132. $this->assertFalse(Zend_Validate::is('24', 'Between', array('min' => 1, 'max' => 12)));
  133. }
  134. /**
  135. * Ensures that if we specify a validator class basename that doesn't
  136. * exist in the namespace, is() throws an exception.
  137. *
  138. * Refactored to conform with ZF-2724.
  139. *
  140. * @group ZF-2724
  141. * @return void
  142. */
  143. public function testStaticFactoryClassNotFound()
  144. {
  145. set_error_handler(array($this, 'handleNotFoundError'), E_WARNING);
  146. try {
  147. Zend_Validate::is('1234', 'UnknownValidator');
  148. } catch (Zend_Exception $e) {
  149. }
  150. restore_error_handler();
  151. $this->assertTrue($this->error);
  152. $this->assertTrue(isset($e));
  153. $this->assertContains('Validate class not found', $e->getMessage());
  154. }
  155. /**
  156. * Testing Namespaces
  157. *
  158. * @return void
  159. */
  160. public function testNamespaces()
  161. {
  162. $this->assertEquals(array(), Zend_Validate::getDefaultNamespaces());
  163. $this->assertFalse(Zend_Validate::hasDefaultNamespaces());
  164. Zend_Validate::setDefaultNamespaces('TestDir');
  165. $this->assertEquals(array('TestDir'), Zend_Validate::getDefaultNamespaces());
  166. Zend_Validate::setDefaultNamespaces('OtherTestDir');
  167. $this->assertEquals(array('OtherTestDir'), Zend_Validate::getDefaultNamespaces());
  168. $this->assertTrue(Zend_Validate::hasDefaultNamespaces());
  169. Zend_Validate::setDefaultNamespaces(array());
  170. $this->assertEquals(array(), Zend_Validate::getDefaultNamespaces());
  171. $this->assertFalse(Zend_Validate::hasDefaultNamespaces());
  172. Zend_Validate::addDefaultNamespaces(array('One', 'Two'));
  173. $this->assertEquals(array('One', 'Two'), Zend_Validate::getDefaultNamespaces());
  174. Zend_Validate::addDefaultNamespaces('Three');
  175. $this->assertEquals(array('One', 'Two', 'Three'), Zend_Validate::getDefaultNamespaces());
  176. Zend_Validate::setDefaultNamespaces(array());
  177. }
  178. public function testIsValidWithParameters()
  179. {
  180. $this->assertTrue(Zend_Validate::is(5, 'Between', array(1, 10)));
  181. $this->assertTrue(Zend_Validate::is(5, 'Between', array('min' => 1, 'max' => 10)));
  182. }
  183. public function testSetGetMessageLengthLimitation()
  184. {
  185. Zend_Validate::setMessageLength(5);
  186. $this->assertEquals(5, Zend_Validate::getMessageLength());
  187. $valid = new Zend_Validate_Between(1, 10);
  188. $this->assertFalse($valid->isValid(24));
  189. $message = current($valid->getMessages());
  190. $this->assertTrue(strlen($message) <= 5);
  191. }
  192. /**
  193. * Handle file not found errors
  194. *
  195. * @group ZF-2724
  196. * @param int $errnum
  197. * @param string $errstr
  198. * @return void
  199. */
  200. public function handleNotFoundError($errnum, $errstr)
  201. {
  202. if (strstr($errstr, 'No such file')) {
  203. $this->error = true;
  204. }
  205. }
  206. }
  207. /**
  208. * Validator to return true to any input.
  209. */
  210. class Zend_ValidateTest_True extends Zend_Validate_Abstract
  211. {
  212. public function isValid($value)
  213. {
  214. return true;
  215. }
  216. }
  217. /**
  218. * Validator to return false to any input.
  219. */
  220. class Zend_ValidateTest_False extends Zend_Validate_Abstract
  221. {
  222. public function isValid($value)
  223. {
  224. $this->_messages = array('error' => 'validation failed');
  225. return false;
  226. }
  227. }