HoursFieldTest.php 2.8 KB

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