HoursFieldTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }