DateTest.php 8.0 KB

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