HoursFieldTest.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Cron\Tests;
  3. use Cron\HoursField;
  4. use DateTime;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @author Michael Dowling <mtdowling@gmail.com>
  8. */
  9. class HoursFieldTest extends TestCase
  10. {
  11. /**
  12. * @covers \Cron\HoursField::validate
  13. */
  14. public function testValidatesField()
  15. {
  16. $f = new HoursField();
  17. $this->assertTrue($f->validate('1'));
  18. $this->assertTrue($f->validate('00'));
  19. $this->assertTrue($f->validate('01'));
  20. $this->assertTrue($f->validate('*'));
  21. $this->assertFalse($f->validate('*/3,1,1-12'));
  22. }
  23. /**
  24. * @covers \Cron\HoursField::increment
  25. */
  26. public function testIncrementsDate()
  27. {
  28. $d = new DateTime('2011-03-15 11:15:00');
  29. $f = new HoursField();
  30. $f->increment($d);
  31. $this->assertSame('2011-03-15 12:00:00', $d->format('Y-m-d H:i:s'));
  32. $d->setTime(11, 15, 0);
  33. $f->increment($d, true);
  34. $this->assertSame('2011-03-15 10:59:00', $d->format('Y-m-d H:i:s'));
  35. }
  36. /**
  37. * @covers \Cron\HoursField::increment
  38. */
  39. public function testIncrementsDateWithThirtyMinuteOffsetTimezone()
  40. {
  41. $tz = date_default_timezone_get();
  42. date_default_timezone_set('America/St_Johns');
  43. $d = new DateTime('2011-03-15 11:15:00');
  44. $f = new HoursField();
  45. $f->increment($d);
  46. $this->assertSame('2011-03-15 12:00:00', $d->format('Y-m-d H:i:s'));
  47. $d->setTime(11, 15, 0);
  48. $f->increment($d, true);
  49. $this->assertSame('2011-03-15 10:59:00', $d->format('Y-m-d H:i:s'));
  50. date_default_timezone_set($tz);
  51. }
  52. /**
  53. * @covers \Cron\HoursField::increment
  54. */
  55. public function testIncrementDateWithFifteenMinuteOffsetTimezone()
  56. {
  57. $tz = date_default_timezone_get();
  58. date_default_timezone_set('Asia/Kathmandu');
  59. $d = new DateTime('2011-03-15 11:15:00');
  60. $f = new HoursField();
  61. $f->increment($d);
  62. $this->assertSame('2011-03-15 12:00:00', $d->format('Y-m-d H:i:s'));
  63. $d->setTime(11, 15, 0);
  64. $f->increment($d, true);
  65. $this->assertSame('2011-03-15 10:59:00', $d->format('Y-m-d H:i:s'));
  66. date_default_timezone_set($tz);
  67. }
  68. }