PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/test/unit/validator/sfValidatorDateTest.php

https://github.com/bheneka/gitta
PHP | 246 lines | 201 code | 26 blank | 19 comment | 2 complexity | 39833d02e66668cdb5023d3b0f7edded MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  10. $t = new lime_test(52);
  11. $v = new sfValidatorDate();
  12. // ->clean()
  13. $t->diag('->clean()');
  14. $v->setOption('required', false);
  15. $t->ok($v->clean(null) === null, '->clean() returns null if not required');
  16. // validate strtotime formats
  17. $t->diag('validate strtotime formats');
  18. $t->is($v->clean('18 october 2005'), '2005-10-18', '->clean() accepts dates parsable by strtotime');
  19. $t->is($v->clean('+1 day'), date('Y-m-d', time() + 86400), '->clean() accepts dates parsable by strtotime');
  20. try
  21. {
  22. $v->clean('This is not a date');
  23. $t->fail('->clean() throws a sfValidatorError if the date is a string and is not parsable by strtotime');
  24. $t->skip('', 1);
  25. }
  26. catch (sfValidatorError $e)
  27. {
  28. $t->pass('->clean() throws a sfValidatorError if the date is a string and is not parsable by strtotime');
  29. $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
  30. }
  31. // validate timestamp
  32. $t->diag('validate timestamp');
  33. $t->is($v->clean(time()), date('Y-m-d', time()), '->clean() accepts timestamps as input');
  34. // validate date array
  35. $t->diag('validate date array');
  36. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15)), '2005-10-15', '->clean() accepts an array as an input');
  37. $t->is($v->clean(array('year' => '2005', 'month' => '10', 'day' => '15')), '2005-10-15', '->clean() accepts an array as an input');
  38. $t->is($v->clean(array('year' => '', 'month' => '', 'day' => '')), null, '->clean() accepts an array as an input');
  39. $t->is($v->clean(array('year' => 2008, 'month' => 02, 'day' => 29)), '2008-02-29', '->clean() recognises a leapyear');
  40. try
  41. {
  42. $v->clean(array('year' => '', 'month' => 1, 'day' => 15));
  43. $t->fail('->clean() throws a sfValidatorError if the date is not valid');
  44. $t->skip('', 1);
  45. }
  46. catch (sfValidatorError $e)
  47. {
  48. $t->pass('->clean() throws a sfValidatorError if the date is not valid');
  49. $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
  50. }
  51. try
  52. {
  53. $v->clean(array('year' => -2, 'month' => 1, 'day' => 15));
  54. $t->fail('->clean() throws a sfValidatorError if the date is not valid');
  55. $t->skip('', 1);
  56. }
  57. catch (sfValidatorError $e)
  58. {
  59. $t->pass('->clean() throws a sfValidatorError if the date is not valid');
  60. $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
  61. }
  62. try
  63. {
  64. $v->clean(array('year' => 2008, 'month' => 2, 'day' => 30));
  65. $t->fail('->clean() throws a sfValidatorError if the date is not valid');
  66. $t->skip('', 1);
  67. }
  68. catch (sfValidatorError $e)
  69. {
  70. $t->pass('->clean() throws a sfValidatorError if the date is not valid');
  71. $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
  72. }
  73. // validate regex
  74. $t->diag('validate regex');
  75. $v->setOption('date_format', '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~');
  76. $t->is($v->clean('18/10/2005'), '2005-10-18', '->clean() accepts a regular expression to match dates');
  77. $t->is($v->clean(array('year' => '2005', 'month' => '10', 'day' => '18')), '2005-10-18', '->clean() accepts a regular expression when cleaning an array');
  78. try
  79. {
  80. $v->clean('2005-10-18');
  81. $t->fail('->clean() throws a sfValidatorError if the date does not match the regex');
  82. $t->skip('', 2);
  83. }
  84. catch (sfValidatorError $e)
  85. {
  86. $t->pass('->clean() throws a sfValidatorError if the date does not match the regex');
  87. $t->like($e->getMessage(), '/'.preg_quote(htmlspecialchars($v->getOption('date_format'), ENT_QUOTES, 'UTF-8'), '/').'/', '->clean() returns the expected date format in the error message');
  88. $t->is($e->getCode(), 'bad_format', '->clean() throws a sfValidatorError');
  89. }
  90. $v->setOption('date_format_error', 'dd/mm/YYYY');
  91. try
  92. {
  93. $v->clean('2005-10-18');
  94. $t->skip('', 1);
  95. }
  96. catch (sfValidatorError $e)
  97. {
  98. $t->like($e->getMessage(), '/'.preg_quote('dd/mm/YYYY', '/').'/', '->clean() returns the expected date format error if provided');
  99. }
  100. $v->setOption('date_format', null);
  101. // option with_time
  102. $t->diag('option with_time');
  103. $v->setOption('with_time', true);
  104. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10, 'second' => 15)), '2005-10-15 12:10:15', '->clean() accepts an array as an input');
  105. $t->is($v->clean(array('year' => '2005', 'month' => '10', 'day' => '15', 'hour' => '12', 'minute' => '10', 'second' => '15')), '2005-10-15 12:10:15', '->clean() accepts an array as an input');
  106. $t->is($v->clean(array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '')), null, '->clean() accepts an array as an input');
  107. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10, 'second' => '')), '2005-10-15 12:10:00', '->clean() accepts an array as an input');
  108. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10)), '2005-10-15 12:10:00', '->clean() accepts an array as an input');
  109. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 0, 'minute' => 10)), '2005-10-15 00:10:00', '->clean() accepts an array as an input');
  110. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => '0', 'minute' => 10)), '2005-10-15 00:10:00', '->clean() accepts an array as an input');
  111. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 10)), '2005-10-15 10:00:00', '->clean() accepts an array as an input');
  112. $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 0)), '2005-10-15 00:00:00', '->clean() accepts an array as an input');
  113. try
  114. {
  115. $v->clean(array('year' => 2005, 'month' => 1, 'day' => 15, 'hour' => 12, 'minute' => '', 'second' => 12));
  116. $t->fail('->clean() throws a sfValidatorError if the time is not valid');
  117. }
  118. catch (sfValidatorError $e)
  119. {
  120. $t->pass('->clean() throws a sfValidatorError if the time is not valid');
  121. }
  122. $t->is($v->clean('18 october 2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option');
  123. $t->is($v->clean(time()), date('Y-m-d H:i:s', time()), '->clean() can accept date time with the with_time option');
  124. $v->setOption('date_format', '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~');
  125. $t->is($v->clean('18/10/2005'), '2005-10-18 00:00:00', '->clean() can accept date time with the with_time option');
  126. $v->setOption('date_format', '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4}) (?P<hour>\d{2})\:(?P<minute>\d{2})~');
  127. $t->is($v->clean('18/10/2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option');
  128. $v->setOption('date_format', null);
  129. // change date output
  130. $t->diag('change date output');
  131. $v->setOption('with_time', false);
  132. $v->setOption('date_output', 'U');
  133. $t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
  134. $v->setOption('datetime_output', 'U');
  135. $v->setOption('with_time', true);
  136. $t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
  137. // required
  138. $v = new sfValidatorDate();
  139. foreach (array(
  140. array('year' => '', 'month' => '', 'day' => ''),
  141. array('year' => null, 'month' => null, 'day' => null),
  142. '',
  143. null,
  144. ) as $input)
  145. {
  146. try
  147. {
  148. $v->clean($input);
  149. $t->fail('->clean() throws an exception if the date is empty and required is true');
  150. }
  151. catch (sfValidatorError $e)
  152. {
  153. $t->pass('->clean() throws an exception if the date is empty and required is true');
  154. }
  155. }
  156. // max and min options
  157. $t->diag('max and min options');
  158. $v->setOption('min', strtotime('1 Jan 2005'));
  159. $v->setOption('max', strtotime('31 Dec 2007'));
  160. $t->is($v->clean('18 october 2005'), '2005-10-18', '->clean() can accept a max/min option');
  161. try
  162. {
  163. $v->clean('18 october 2004');
  164. $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
  165. }
  166. catch (sfValidatorError $e)
  167. {
  168. $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
  169. }
  170. try
  171. {
  172. $v->clean('18 october 2008');
  173. $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
  174. }
  175. catch (sfValidatorError $e)
  176. {
  177. $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
  178. }
  179. // max and min options out off timestamp range
  180. $t->diag('max and min options out off timestamp range');
  181. $v->setOption('min', '1805-12-31 10:00:00');
  182. $v->setOption('max', '2107-12-31 10:50:00');
  183. $t->is($v->clean('18 october 2105'), '2105-10-18', '->clean() can accept a max/min option string');
  184. $t->is($v->clean(array('year' => 1906, 'month' => 2, 'day' => 13)), '1906-02-13', '->clean() can accept a max/min option array');
  185. try
  186. {
  187. $v->clean('18 october 1804');
  188. $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
  189. }
  190. catch (sfValidatorError $e)
  191. {
  192. $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
  193. $t->is($e->getMessage(), 'The date must be after 31/12/1805 10:00:00.', '->clean() check exception message');
  194. }
  195. try
  196. {
  197. $v->clean('18 october 2108');
  198. $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
  199. }
  200. catch (sfValidatorError $e)
  201. {
  202. $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
  203. $t->is($e->getMessage(), 'The date must be before 31/12/2107 10:50:00.', '->clean() check exception message');
  204. }
  205. // timezones
  206. $defaultTimezone = new DateTimeZone(date_default_timezone_get());
  207. $otherTimezone = new DateTimeZone('US/Pacific');
  208. if ($defaultTimezone->getOffset(new DateTime()) == $otherTimezone->getOffset(new DateTime()))
  209. {
  210. $otherTimezone = new DateTimeZone('US/Eastern');
  211. }
  212. $date = new DateTime('2000-01-01T00:00:00-00:00');
  213. $date->setTimezone($otherTimezone);
  214. $v->setOption('min', null);
  215. $v->setOption('max', null);
  216. $v->setOption('with_time', true);
  217. $clean = $v->clean($date->format(DATE_ATOM));
  218. // did it convert from the other timezone to the default timezone?
  219. $date->setTimezone($defaultTimezone);
  220. $t->is($clean, $date->format('Y-m-d H:i:s'), '->clean() respects incoming and default timezones');