DateTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_Date
  28. */
  29. require_once 'Zend/Validate/Date.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Validate_DateTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Zend_Validate_Date object
  41. *
  42. * @var Zend_Validate_Date
  43. */
  44. protected $_validator;
  45. /**
  46. * Whether an error occurred
  47. *
  48. * @var boolean
  49. */
  50. protected $_errorOccurred = false;
  51. /**
  52. * Creates a new Zend_Validate_Date object for each test method
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. $this->_validator = new Zend_Validate_Date();
  59. }
  60. /**
  61. * Ensures that the validator follows expected behavior
  62. *
  63. * @return void
  64. */
  65. public function testBasic()
  66. {
  67. $valuesExpected = array(
  68. '2007-01-01' => true,
  69. '2007-02-28' => true,
  70. '2007-02-29' => false,
  71. '2008-02-29' => true,
  72. '2007-02-30' => false,
  73. '2007-02-99' => false,
  74. '9999-99-99' => false,
  75. 0 => false,
  76. 999999999999 => false,
  77. 'Jan 1 2007' => false,
  78. 'asdasda' => false,
  79. 'sdgsdg' => false
  80. );
  81. foreach ($valuesExpected as $input => $result) {
  82. $this->assertEquals($result, $this->_validator->isValid($input),
  83. "'$input' expected to be " . ($result ? '' : 'in') . 'valid');
  84. }
  85. }
  86. /**
  87. * Ensures that characters trailing an otherwise valid date cause the input to be invalid
  88. *
  89. * @see http://framework.zend.com/issues/browse/ZF-1804
  90. * @return void
  91. */
  92. public function testCharactersTrailingInvalid()
  93. {
  94. $dateValid = '2007-08-02';
  95. $charactersTrailing = 'something';
  96. $this->assertTrue($this->_validator->isValid($dateValid));
  97. $this->assertFalse($this->_validator->isValid($dateValid . $charactersTrailing));
  98. }
  99. /**
  100. * Ensures that characters leading an otherwise valid date cause the input to be invalid
  101. *
  102. * @see http://framework.zend.com/issues/browse/ZF-1804
  103. * @return void
  104. */
  105. public function testCharactersLeadingInvalid()
  106. {
  107. $dateValid = '2007-08-02';
  108. $charactersLeading = 'something';
  109. $this->assertTrue($this->_validator->isValid($dateValid));
  110. $this->assertFalse($this->_validator->isValid($charactersLeading . $dateValid));
  111. }
  112. /**
  113. * Ensures that getMessages() returns expected default value
  114. *
  115. * @return void
  116. */
  117. public function testGetMessages()
  118. {
  119. $this->assertEquals(array(), $this->_validator->getMessages());
  120. }
  121. /**
  122. * Ensures that the validator can handle different manual dateformats
  123. *
  124. * @see http://framework.zend.com/issues/browse/ZF-2003
  125. * @return void
  126. */
  127. public function testUseManualFormat()
  128. {
  129. $this->assertTrue($this->_validator->setFormat('dd.MM.YYYY')->isValid('10.01.2008'));
  130. $this->assertEquals('dd.MM.YYYY', $this->_validator->getFormat());
  131. $this->assertTrue($this->_validator->setFormat('MMM yyyy')->isValid('Jan 2010'));
  132. $this->assertFalse($this->_validator->setFormat('dd/MM/yyyy')->isValid('2008/10/22'));
  133. $this->assertTrue($this->_validator->setFormat('dd/MM/yy')->isValid('22/10/08'));
  134. $this->assertFalse($this->_validator->setFormat('dd/MM/yy')->isValid('22/10'));
  135. set_error_handler(array($this, 'errorHandlerIgnore'));
  136. $result = $this->_validator->setFormat('s')->isValid(0);
  137. restore_error_handler();
  138. if (!$this->_errorOccurred) {
  139. $this->assertTrue($result);
  140. } else {
  141. $this->markTestSkipped('Affected by bug described in ZF-2789');
  142. }
  143. $this->_errorOccurred = false;
  144. }
  145. /**
  146. * Ensures that the validator can handle different dateformats from locale
  147. *
  148. * @see http://framework.zend.com/issues/browse/ZF-2003
  149. * @return void
  150. */
  151. public function testUseLocaleFormat()
  152. {
  153. $errorOccurredLocal = false;
  154. set_error_handler(array($this, 'errorHandlerIgnore'));
  155. $valuesExpected = array(
  156. '10.01.2008' => true,
  157. '32.02.2008' => false,
  158. '20 April 2008' => true,
  159. '1 Jul 2008' => true,
  160. '2008/20/03' => false,
  161. '99/99/2000' => false,
  162. 0 => false,
  163. 999999999999 => false,
  164. 'Jan 1 2007' => false
  165. );
  166. foreach ($valuesExpected as $input => $resultExpected) {
  167. $resultActual = $this->_validator->setLocale('de_AT')->isValid($input);
  168. if (!$this->_errorOccurred) {
  169. $this->assertEquals($resultExpected, $resultActual, "'$input' expected to be "
  170. . ($resultExpected ? '' : 'in') . 'valid');
  171. } else {
  172. $errorOccurredLocal = true;
  173. }
  174. $this->_errorOccurred = false;
  175. }
  176. $this->assertEquals('de_AT', $this->_validator->getLocale());
  177. restore_error_handler();
  178. if ($errorOccurredLocal) {
  179. $this->markTestSkipped('Affected by bug described in ZF-2789');
  180. }
  181. }
  182. /**
  183. * Ensures that the validator can handle different dateformats from locale
  184. *
  185. * @see http://framework.zend.com/issues/browse/ZF-2003
  186. * @return void
  187. */
  188. public function testLocaleContructor()
  189. {
  190. $valid = new Zend_Validate_Date('dd.MM.YYYY', 'de');
  191. $this->assertTrue($valid->isValid('10.April.2008'));
  192. }
  193. /**
  194. * @ZF-4352
  195. */
  196. public function testNonStringValidation()
  197. {
  198. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  199. }
  200. /**
  201. * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
  202. *
  203. * @param integer $errno
  204. * @param string $errstr
  205. * @param string $errfile
  206. * @param integer $errline
  207. * @param array $errcontext
  208. * @return void
  209. * @see http://framework.zend.com/issues/browse/ZF-2789
  210. */
  211. public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $errcontext)
  212. {
  213. $this->_errorOccurred = true;
  214. }
  215. }