/lib/vendor/symfony/test/unit/validator/sfValidatorDateTest.php

https://github.com/proyectoalba/alba · PHP · 218 lines · 175 code · 25 blank · 18 comment · 2 complexity · e42ff16f8dc38893785aa3fb83dc8778 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(45, new lime_output_color());
  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. try
  78. {
  79. $v->clean('2005-10-18');
  80. $t->fail('->clean() throws a sfValidatorError if the date does not match the regex');
  81. $t->skip('', 2);
  82. }
  83. catch (sfValidatorError $e)
  84. {
  85. $t->pass('->clean() throws a sfValidatorError if the date does not match the regex');
  86. $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');
  87. $t->is($e->getCode(), 'bad_format', '->clean() throws a sfValidatorError');
  88. }
  89. $v->setOption('date_format_error', 'dd/mm/YYYY');
  90. try
  91. {
  92. $v->clean('2005-10-18');
  93. $t->skip('', 1);
  94. }
  95. catch (sfValidatorError $e)
  96. {
  97. $t->like($e->getMessage(), '/'.preg_quote('dd/mm/YYYY', '/').'/', '->clean() returns the expected date format error if provided');
  98. }
  99. $v->setOption('date_format', null);
  100. // option with_time
  101. $t->diag('option with_time');
  102. $v->setOption('with_time', true);
  103. $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');
  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' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '')), null, '->clean() accepts an array as an input');
  106. $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');
  107. $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');
  108. $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');
  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' => 10)), '2005-10-15 10:00:00', '->clean() accepts an array as an input');
  111. $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');
  112. try
  113. {
  114. $v->clean(array('year' => 2005, 'month' => 1, 'day' => 15, 'hour' => 12, 'minute' => '', 'second' => 12));
  115. $t->fail('->clean() throws a sfValidatorError if the time is not valid');
  116. }
  117. catch (sfValidatorError $e)
  118. {
  119. $t->pass('->clean() throws a sfValidatorError if the time is not valid');
  120. }
  121. $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');
  122. $t->is($v->clean(time()), date('Y-m-d H:i:s', time()), '->clean() can accept date time with the with_time option');
  123. $v->setOption('date_format', '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~');
  124. $t->is($v->clean('18/10/2005'), '2005-10-18 00:00:00', '->clean() can accept date time with the with_time option');
  125. $v->setOption('date_format', '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4}) (?P<hour>\d{2})\:(?P<minute>\d{2})~');
  126. $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');
  127. $v->setOption('date_format', null);
  128. // change date output
  129. $t->diag('change date output');
  130. $v->setOption('with_time', false);
  131. $v->setOption('date_output', 'U');
  132. $t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
  133. $v->setOption('datetime_output', 'U');
  134. $v->setOption('with_time', true);
  135. $t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
  136. // required
  137. $v = new sfValidatorDate();
  138. foreach (array(
  139. array('year' => '', 'month' => '', 'day' => ''),
  140. array('year' => null, 'month' => null, 'day' => null),
  141. '',
  142. null,
  143. ) as $input)
  144. {
  145. try
  146. {
  147. $v->clean($input);
  148. $t->fail('->clean() throws an exception if the date is empty and required is true');
  149. }
  150. catch (sfValidatorError $e)
  151. {
  152. $t->pass('->clean() throws an exception if the date is empty and required is true');
  153. }
  154. }
  155. // max and min options
  156. $t->diag('max and min options');
  157. $v->setOption('min', strtotime('1 Jan 2005'));
  158. $v->setOption('max', strtotime('31 Dec 2007'));
  159. $t->is($v->clean('18 october 2005'), '2005-10-18', '->clean() can accept a max/min option');
  160. try
  161. {
  162. $v->clean('18 october 2004');
  163. $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
  164. }
  165. catch (sfValidatorError $e)
  166. {
  167. $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
  168. }
  169. try
  170. {
  171. $v->clean('18 october 2008');
  172. $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
  173. }
  174. catch (sfValidatorError $e)
  175. {
  176. $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
  177. }
  178. // timezones
  179. $defaultTimezone = new DateTimeZone(date_default_timezone_get());
  180. $otherTimezone = new DateTimeZone('US/Pacific');
  181. if ($defaultTimezone->getOffset(new DateTime()) == $otherTimezone->getOffset(new DateTime()))
  182. {
  183. $otherTimezone = new DateTimeZone('US/Eastern');
  184. }
  185. $date = new DateTime('2000-01-01T00:00:00-00:00');
  186. $date->setTimezone($otherTimezone);
  187. $v->setOption('min', null);
  188. $v->setOption('max', null);
  189. $v->setOption('with_time', true);
  190. $clean = $v->clean($date->format(DATE_ATOM));
  191. // did it convert from the other timezone to the default timezone?
  192. $date->setTimezone($defaultTimezone);
  193. $t->is($clean, $date->format('Y-m-d H:i:s'), '->clean() respects incoming and default timezones');