DateTest.php 7.7 KB

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