updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -1,5 +1,23 @@
# Change Log
## [2.3.1] - 2020-10-12
### Added
- Added support for PHP 8 (#92)
### Changed
- N/A
### Fixed
- N/A
## [2.3.0] - 2019-03-30
### Added
- Added support for DateTimeImmutable via DateTimeInterface
- Added support for PHP 7.3
- Started listing projects that use the library
### Changed
- Errors should now report a human readable position in the cron expression, instead of starting at 0
### Fixed
- N/A
## [2.2.0] - 2018-06-05
### Added
- Added support for steps larger than field ranges (#6)

View File

@@ -6,11 +6,11 @@ PHP Cron Expression Parser
The PHP cron expression parser can parse a CRON expression, determine if it is
due to run, calculate the next run date of the expression, and calculate the previous
run date of the expression. You can calculate dates far into the future or past by
skipping n number of matching dates.
skipping **n** number of matching dates.
The parser can handle increments of ranges (e.g. */12, 2-59/3), intervals (e.g. 0-9),
lists (e.g. 1,2,3), W to find the nearest weekday for a given day of the month, L to
find the last day of the month, L to find the last given weekday of a month, and hash
lists (e.g. 1,2,3), **W** to find the nearest weekday for a given day of the month, **L** to
find the last day of the month, **L** to find the last given weekday of a month, and hash
(#) to find the nth weekday of a given month.
More information about this fork can be found in the blog post [here](http://ctankersley.com/2017/10/12/cron-expression-update/). tl;dr - v2.0.0 is a major breaking change, and @dragonmantank can better take care of the project in a separate fork.
@@ -71,3 +71,8 @@ Requirements
- PHP 7.0+
- PHPUnit is required to run the unit tests
- Composer is required to run the unit tests
Projects that Use cron-expression
=================================
* Part of the [Laravel Framework](https://github.com/laravel/framework/)
* Available as a [Symfony Bundle - setono/cron-expression-bundle](https://github.com/Setono/CronExpressionBundle)

View File

@@ -17,10 +17,10 @@
}
],
"require": {
"php": ">=7.0.0"
"php": "^7.0|^8.0"
},
"require-dev": {
"phpunit/phpunit": "~6.4"
"phpunit/phpunit": "^6.4|^7.0|^8.0|^9.0"
},
"autoload": {
"psr-4": {
@@ -31,5 +31,10 @@
"psr-4": {
"Tests\\": "tests/Cron/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.3-dev"
}
}
}

View File

@@ -31,7 +31,9 @@ abstract class AbstractField implements FieldInterface
*/
protected $rangeEnd;
/**
* Constructor
*/
public function __construct()
{
$this->fullRange = range($this->rangeStart, $this->rangeEnd);
@@ -204,12 +206,18 @@ abstract class AbstractField implements FieldInterface
return $values;
}
/**
* Convert literal
*
* @param string $value
* @return string
*/
protected function convertLiterals($value)
{
if (count($this->literals)) {
$key = array_search($value, $this->literals);
if ($key !== false) {
return $key;
return (string) $key;
}
}

View File

@@ -4,6 +4,7 @@ namespace Cron;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Exception;
use InvalidArgumentException;
@@ -62,7 +63,7 @@ class CronExpression
* `@weekly` - Run once a week, midnight on Sun - 0 0 * * 0
* `@daily` - Run once a day, midnight - 0 0 * * *
* `@hourly` - Run once an hour, first minute - 0 * * * *
* @param FieldFactory $fieldFactory Field factory to use
* @param FieldFactory|null $fieldFactory Field factory to use
*
* @return CronExpression
*/
@@ -107,9 +108,9 @@ class CronExpression
* Parse a CRON expression
*
* @param string $expression CRON expression (e.g. '8 * * * *')
* @param FieldFactory $fieldFactory Factory to create cron fields
* @param FieldFactory|null $fieldFactory Factory to create cron fields
*/
public function __construct($expression, FieldFactory $fieldFactory)
public function __construct($expression, FieldFactory $fieldFactory = null)
{
$this->fieldFactory = $fieldFactory;
$this->setExpression($expression);
@@ -178,16 +179,17 @@ class CronExpression
/**
* Get a next run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning a
* matching next run date. 0, the default, will return the current
* date and time if the next run date falls on the current date and
* time. Setting this value to 1 will skip the first match and go to
* the second match. Setting this value to 2 will skip the first 2
* matches and so on.
* @param bool $allowCurrentDate Set to TRUE to return the current date if
* it matches the cron expression.
* @param null|string $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning a
* matching next run date. 0, the default, will return the
* current date and time if the next run date falls on the
* current date and time. Setting this value to 1 will
* skip the first match and go to the second match.
* Setting this value to 2 will skip the first 2
* matches and so on.
* @param bool $allowCurrentDate Set to TRUE to return the current date if
* it matches the cron expression.
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
@@ -200,11 +202,11 @@ class CronExpression
/**
* Get a previous run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
@@ -218,14 +220,14 @@ class CronExpression
/**
* Get multiple run dates starting at the current date or a specific date
*
* @param int $total Set the total number of dates to calculate
* @param string|\DateTime $currentTime Relative calculation date
* @param bool $invert Set to TRUE to retrieve previous dates
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
* @param int $total Set the total number of dates to calculate
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param bool $invert Set to TRUE to retrieve previous dates
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return array Returns an array of run dates
* @return \DateTime[] Returns an array of run dates
*/
public function getMultipleRunDates($total, $currentTime = 'now', $invert = false, $allowCurrentDate = false, $timeZone = null)
{
@@ -276,8 +278,8 @@ class CronExpression
* specific date. This method assumes that the current number of
* seconds are irrelevant, and should be called once per minute.
*
* @param string|\DateTime $currentTime Relative calculation date
* @param null|string $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param null|string $timeZone TimeZone to use instead of the system default
*
* @return bool Returns TRUE if the cron is due to run or FALSE if not
*/
@@ -309,12 +311,12 @@ class CronExpression
/**
* Get the next or previous run date of the expression relative to a date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $invert Set to TRUE to go backwards in time
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param string|null $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $invert Set to TRUE to go backwards in time
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
* @param string|null $timeZone TimeZone to use instead of the system default
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
@@ -391,8 +393,8 @@ class CronExpression
/**
* Workout what timeZone should be used.
*
* @param string|\DateTime $currentTime Relative calculation date
* @param string|null $timeZone TimeZone to use instead of the system default
* @param string|\DateTimeInterface $currentTime Relative calculation date
* @param string|null $timeZone TimeZone to use instead of the system default
*
* @return string
*/
@@ -402,7 +404,7 @@ class CronExpression
return $timeZone;
}
if ($currentTime instanceOf Datetime) {
if ($currentTime instanceOf DateTimeInterface) {
return $currentTime->getTimeZone()->getName();
}

View File

@@ -3,6 +3,7 @@
namespace Cron;
use DateTime;
use DateTimeInterface;
/**
* Day of month field. Allows: * , / - ? L W
@@ -24,7 +25,14 @@ use DateTime;
*/
class DayOfMonthField extends AbstractField
{
/**
* @inheritDoc
*/
protected $rangeStart = 1;
/**
* @inheritDoc
*/
protected $rangeEnd = 31;
/**
@@ -59,7 +67,10 @@ class DayOfMonthField extends AbstractField
}
}
public function isSatisfiedBy(DateTime $date, $value)
/**
* @inheritDoc
*/
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
// ? states that the field value is to be skipped
if ($value == '?') {
@@ -88,14 +99,17 @@ class DayOfMonthField extends AbstractField
return $this->isSatisfied($date->format('d'), $value);
}
public function increment(DateTime $date, $invert = false)
/**
* @inheritDoc
*
* @param \DateTime|\DateTimeImmutable &$date
*/
public function increment(DateTimeInterface &$date, $invert = false)
{
if ($invert) {
$date->modify('previous day');
$date->setTime(23, 59);
$date = $date->modify('previous day')->setTime(23, 59);
} else {
$date->modify('next day');
$date->setTime(0, 0);
$date = $date->modify('next day')->setTime(0, 0);
}
return $this;

View File

@@ -3,9 +3,9 @@
namespace Cron;
use DateTime;
use DateTimeInterface;
use InvalidArgumentException;
/**
* Day of week field. Allows: * / , - ? L #
*
@@ -21,20 +21,41 @@ use InvalidArgumentException;
*/
class DayOfWeekField extends AbstractField
{
/**
* @inheritDoc
*/
protected $rangeStart = 0;
/**
* @inheritDoc
*/
protected $rangeEnd = 7;
/**
* @var array Weekday range
*/
protected $nthRange;
/**
* @inheritDoc
*/
protected $literals = [1 => 'MON', 2 => 'TUE', 3 => 'WED', 4 => 'THU', 5 => 'FRI', 6 => 'SAT', 7 => 'SUN'];
/**
* Constructor
*/
public function __construct()
{
$this->nthRange = range(1, 5);
parent::__construct();
}
public function isSatisfiedBy(DateTime $date, $value)
/**
* @inheritDoc
*
* @param \DateTime|\DateTimeImmutable $date
*/
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
if ($value == '?') {
return true;
@@ -49,9 +70,11 @@ class DayOfWeekField extends AbstractField
// Find out if this is the last specific weekday of the month
if (strpos($value, 'L')) {
$weekday = str_replace('7', '0', substr($value, 0, strpos($value, 'L')));
$weekday = (int) $this->convertLiterals(substr($value, 0, strpos($value, 'L')));
$weekday %= 7;
$tdate = clone $date;
$tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth);
$tdate = $tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth);
while ($tdate->format('w') != $weekday) {
$tdateClone = new DateTime();
$tdate = $tdateClone
@@ -94,7 +117,7 @@ class DayOfWeekField extends AbstractField
}
$tdate = clone $date;
$tdate->setDate($currentYear, $currentMonth, 1);
$tdate = $tdate->setDate($currentYear, $currentMonth, 1);
$dayCount = 0;
$currentDay = 1;
while ($currentDay < $lastDayOfMonth + 1) {
@@ -103,7 +126,7 @@ class DayOfWeekField extends AbstractField
break;
}
}
$tdate->setDate($currentYear, $currentMonth, ++$currentDay);
$tdate = $tdate->setDate($currentYear, $currentMonth, ++$currentDay);
}
return $date->format('j') == $currentDay;
@@ -127,14 +150,17 @@ class DayOfWeekField extends AbstractField
return $this->isSatisfied($fieldValue, $value);
}
public function increment(DateTime $date, $invert = false)
/**
* @inheritDoc
*
* @param \DateTime|\DateTimeImmutable &$date
*/
public function increment(DateTimeInterface &$date, $invert = false)
{
if ($invert) {
$date->modify('-1 day');
$date->setTime(23, 59, 0);
$date = $date->modify('-1 day')->setTime(23, 59, 0);
} else {
$date->modify('+1 day');
$date->setTime(0, 0, 0);
$date = $date->modify('+1 day')->setTime(0, 0, 0);
}
return $this;

View File

@@ -44,7 +44,7 @@ class FieldFactory
break;
default:
throw new InvalidArgumentException(
$position . ' is not a valid position'
($position + 1) . ' is not a valid position'
);
}
}

View File

@@ -1,7 +1,8 @@
<?php
namespace Cron;
use DateTime;
use DateTimeInterface;
/**
* CRON field interface
@@ -11,23 +12,23 @@ interface FieldInterface
/**
* Check if the respective value of a DateTime field satisfies a CRON exp
*
* @param DateTime $date DateTime object to check
* @param string $value CRON expression to test against
* @param DateTimeInterface $date DateTime object to check
* @param string $value CRON expression to test against
*
* @return bool Returns TRUE if satisfied, FALSE otherwise
*/
public function isSatisfiedBy(DateTime $date, $value);
public function isSatisfiedBy(DateTimeInterface $date, $value);
/**
* When a CRON expression is not satisfied, this method is used to increment
* or decrement a DateTime object by the unit of the cron field
*
* @param DateTime $date DateTime object to change
* @param bool $invert (optional) Set to TRUE to decrement
* @param DateTimeInterface &$date DateTime object to change
* @param bool $invert (optional) Set to TRUE to decrement
*
* @return FieldInterface
*/
public function increment(DateTime $date, $invert = false);
public function increment(DateTimeInterface &$date, $invert = false);
/**
* Validates a CRON expression for a given field

View File

@@ -1,39 +1,55 @@
<?php
namespace Cron;
use DateTime;
use DateTimeZone;
use DateTimeInterface;
use DateTimeZone;
/**
* Hours field. Allows: * , / -
*/
class HoursField extends AbstractField
{
/**
* @inheritDoc
*/
protected $rangeStart = 0;
/**
* @inheritDoc
*/
protected $rangeEnd = 23;
public function isSatisfiedBy(DateTime $date, $value)
/**
* @inheritDoc
*/
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
if ($value == '?') {
return true;
}
return $this->isSatisfied($date->format('H'), $value);
}
public function increment(DateTime $date, $invert = false, $parts = null)
/**
* {@inheritDoc}
*
* @param \DateTime|\DateTimeImmutable &$date
* @param string|null $parts
*/
public function increment(DateTimeInterface &$date, $invert = false, $parts = null)
{
// Change timezone to UTC temporarily. This will
// allow us to go back or forwards and hour even
// if DST will be changed between the hours.
if (is_null($parts) || $parts == '*') {
$timezone = $date->getTimezone();
$date->setTimezone(new DateTimeZone('UTC'));
if ($invert) {
$date->modify('-1 hour');
} else {
$date->modify('+1 hour');
}
$date->setTimezone($timezone);
$date = $date->setTimezone(new DateTimeZone('UTC'));
$date = $date->modify(($invert ? '-' : '+') . '1 hour');
$date = $date->setTimezone($timezone);
$date->setTime($date->format('H'), $invert ? 59 : 0);
$date = $date->setTime($date->format('H'), $invert ? 59 : 0);
return $this;
}
@@ -57,11 +73,11 @@ class HoursField extends AbstractField
$hour = $hours[$position];
if ((!$invert && $date->format('H') >= $hour) || ($invert && $date->format('H') <= $hour)) {
$date->modify(($invert ? '-' : '+') . '1 day');
$date->setTime($invert ? 23 : 0, $invert ? 59 : 0);
$date = $date->modify(($invert ? '-' : '+') . '1 day');
$date = $date->setTime($invert ? 23 : 0, $invert ? 59 : 0);
}
else {
$date->setTime($hour, $invert ? 59 : 0);
$date = $date->setTime($hour, $invert ? 59 : 0);
}
return $this;

View File

@@ -2,30 +2,45 @@
namespace Cron;
use DateTime;
use DateTimeInterface;
/**
* Minutes field. Allows: * , / -
*/
class MinutesField extends AbstractField
{
/**
* @inheritDoc
*/
protected $rangeStart = 0;
/**
* @inheritDoc
*/
protected $rangeEnd = 59;
public function isSatisfiedBy(DateTime $date, $value)
/**
* @inheritDoc
*/
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
if ($value == '?') {
return true;
}
return $this->isSatisfied($date->format('i'), $value);
}
public function increment(DateTime $date, $invert = false, $parts = null)
/**
* {@inheritDoc}
*
* @param \DateTime|\DateTimeImmutable &$date
* @param string|null $parts
*/
public function increment(DateTimeInterface &$date, $invert = false, $parts = null)
{
if (is_null($parts)) {
if ($invert) {
$date->modify('-1 minute');
} else {
$date->modify('+1 minute');
}
$date = $date->modify(($invert ? '-' : '+') . '1 minute');
return $this;
}
@@ -48,11 +63,11 @@ class MinutesField extends AbstractField
}
if ((!$invert && $current_minute >= $minutes[$position]) || ($invert && $current_minute <= $minutes[$position])) {
$date->modify(($invert ? '-' : '+') . '1 hour');
$date->setTime($date->format('H'), $invert ? 59 : 0);
$date = $date->modify(($invert ? '-' : '+') . '1 hour');
$date = $date->setTime($date->format('H'), $invert ? 59 : 0);
}
else {
$date->setTime($date->format('H'), $minutes[$position]);
$date = $date->setTime($date->format('H'), $minutes[$position]);
}
return $this;

View File

@@ -2,33 +2,54 @@
namespace Cron;
use DateTime;
use DateTimeInterface;
/**
* Month field. Allows: * , / -
*/
class MonthField extends AbstractField
{
/**
* @inheritDoc
*/
protected $rangeStart = 1;
/**
* @inheritDoc
*/
protected $rangeEnd = 12;
/**
* @inheritDoc
*/
protected $literals = [1 => 'JAN', 2 => 'FEB', 3 => 'MAR', 4 => 'APR', 5 => 'MAY', 6 => 'JUN', 7 => 'JUL',
8 => 'AUG', 9 => 'SEP', 10 => 'OCT', 11 => 'NOV', 12 => 'DEC'];
public function isSatisfiedBy(DateTime $date, $value)
/**
* @inheritDoc
*/
public function isSatisfiedBy(DateTimeInterface $date, $value)
{
if ($value == '?') {
return true;
}
$value = $this->convertLiterals($value);
return $this->isSatisfied($date->format('m'), $value);
}
public function increment(DateTime $date, $invert = false)
/**
* @inheritDoc
*
* @param \DateTime|\DateTimeImmutable &$date
*/
public function increment(DateTimeInterface &$date, $invert = false)
{
if ($invert) {
$date->modify('last day of previous month');
$date->setTime(23, 59);
$date = $date->modify('last day of previous month')->setTime(23, 59);
} else {
$date->modify('first day of next month');
$date->setTime(0, 0);
$date = $date->modify('first day of next month')->setTime(0, 0);
}
return $this;

View File

@@ -5,6 +5,7 @@ namespace Cron\Tests;
use Cron\CronExpression;
use Cron\MonthField;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
@@ -47,11 +48,12 @@ class CronExpressionTest extends TestCase
* @covers \Cron\CronExpression::__construct
* @covers \Cron\CronExpression::getExpression
* @covers \Cron\CronExpression::__toString
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid CRON field value A at position 0
*/
public function testParsesCronScheduleThrowsAnException()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid CRON field value A at position 0');
CronExpression::factory('A 1 2 3 4');
}
@@ -89,20 +91,22 @@ class CronExpressionTest extends TestCase
* @covers \Cron\CronExpression::__construct
* @covers \Cron\CronExpression::setExpression
* @covers \Cron\CronExpression::setPart
* @expectedException InvalidArgumentException
*/
public function testInvalidCronsWillFail()
{
$this->expectException(\InvalidArgumentException::class);
// Only four values
$cron = CronExpression::factory('* * * 1');
}
/**
* @covers \Cron\CronExpression::setPart
* @expectedException InvalidArgumentException
*/
public function testInvalidPartsWillFail()
{
$this->expectException(\InvalidArgumentException::class);
// Only four values
$cron = CronExpression::factory('* * * * *');
$cron->setPart(1, 'abc');
@@ -228,6 +232,7 @@ class CronExpressionTest extends TestCase
$this->assertTrue($cron->isDue('now'));
$this->assertTrue($cron->isDue(new DateTime('now')));
$this->assertTrue($cron->isDue(date('Y-m-d H:i')));
$this->assertTrue($cron->isDue(new DateTimeImmutable('now')));
}
/**
@@ -407,6 +412,18 @@ class CronExpressionTest extends TestCase
$this->assertEquals($nextRun, new DateTime("2008-11-30 00:00:00"));
}
/**
* @covers \Cron\CronExpression::getRunDate
*/
public function testGetRunDateHandlesDifferentDates()
{
$cron = CronExpression::factory('@weekly');
$date = new DateTime("2019-03-10 00:00:00");
$this->assertEquals($date, $cron->getNextRunDate("2019-03-03 08:00:00"));
$this->assertEquals($date, $cron->getNextRunDate(new DateTime("2019-03-03 08:00:00")));
$this->assertEquals($date, $cron->getNextRunDate(new DateTimeImmutable("2019-03-03 08:00:00")));
}
/**
* @covers \Cron\CronExpression::getRunDate
*/
@@ -557,4 +574,16 @@ class CronExpressionTest extends TestCase
$nextRunDate = $e->getNextRunDate(new DateTime('2014-05-07 00:00:00'));
$this->assertSame('2015-04-01 00:00:00', $nextRunDate->format('Y-m-d H:i:s'));
}
/**
* When there is an issue with a field, we should report the human readable position
*
* @see https://github.com/dragonmantank/cron-expression/issues/29
*/
public function testFieldPositionIsHumanAdjusted()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("6 is not a valid position");
$e = CronExpression::factory('0 * * * * ? *');
}
}

View File

@@ -4,6 +4,7 @@ namespace Cron\Tests;
use Cron\DayOfMonthField;
use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
/**
@@ -33,6 +34,7 @@ class DayOfMonthFieldTest extends TestCase
{
$f = new DayOfMonthField();
$this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
$this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
}
/**
@@ -50,6 +52,17 @@ class DayOfMonthFieldTest extends TestCase
$this->assertSame('2011-03-14 23:59:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\DayOfMonthField::increment
*/
public function testIncrementsDateTimeImmutable()
{
$d = new DateTimeImmutable('2011-03-15 11:15:00');
$f = new DayOfMonthField();
$f->increment($d);
$this->assertSame('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s'));
}
/**
* Day of the month cannot accept a 0 value, it must be between 1 and 31
* See Github issue #120

View File

@@ -4,6 +4,7 @@ namespace Cron\Tests;
use Cron\DayOfWeekField;
use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
/**
@@ -33,6 +34,7 @@ class DayOfWeekFieldTest extends TestCase
{
$f = new DayOfWeekField();
$this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
$this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
}
/**
@@ -50,24 +52,37 @@ class DayOfWeekFieldTest extends TestCase
$this->assertSame('2011-03-14 23:59:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\DayOfWeekField::increment
*/
public function testIncrementsDateTimeImmutable()
{
$d = new DateTimeImmutable('2011-03-15 11:15:00');
$f = new DayOfWeekField();
$f->increment($d);
$this->assertSame('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\DayOfWeekField::isSatisfiedBy
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Weekday must be a value between 0 and 7. 12 given
*/
public function testValidatesHashValueWeekday()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Weekday must be a value between 0 and 7. 12 given');
$f = new DayOfWeekField();
$this->assertTrue($f->isSatisfiedBy(new DateTime(), '12#1'));
}
/**
* @covers \Cron\DayOfWeekField::isSatisfiedBy
* @expectedException InvalidArgumentException
* @expectedExceptionMessage There are never more than 5 or less than 1 of a given weekday in a month
*/
public function testValidatesHashValueNth()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('There are never more than 5 or less than 1 of a given weekday in a month');
$f = new DayOfWeekField();
$this->assertTrue($f->isSatisfiedBy(new DateTime(), '3#6'));
}
@@ -103,6 +118,18 @@ class DayOfWeekFieldTest extends TestCase
$this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), '7#3'));
}
/**
* @covers \Cron\DayOfWeekField::isSatisfiedBy
*/
public function testHandlesLastWeekdayOfTheMonth()
{
$f = new DayOfWeekField();
$this->assertTrue($f->isSatisfiedBy(new DateTime('2018-12-28 00:00:00'), 'FRIL'));
$this->assertTrue($f->isSatisfiedBy(new DateTime('2018-12-28 00:00:00'), '5L'));
$this->assertFalse($f->isSatisfiedBy(new DateTime('2018-12-21 00:00:00'), 'FRIL'));
$this->assertFalse($f->isSatisfiedBy(new DateTime('2018-12-21 00:00:00'), '5L'));
}
/**
* @see https://github.com/mtdowling/cron-expression/issues/47
*/

View File

@@ -32,10 +32,11 @@ class FieldFactoryTest extends TestCase
/**
* @covers \Cron\FieldFactory::getField
* @expectedException InvalidArgumentException
*/
public function testValidatesFieldPosition()
{
$this->expectException(\InvalidArgumentException::class);
$f = new FieldFactory();
$f->getField(-1);
}

View File

@@ -4,6 +4,7 @@ namespace Cron\Tests;
use Cron\HoursField;
use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
/**
@@ -22,7 +23,17 @@ class HoursFieldTest extends TestCase
$this->assertTrue($f->validate('01'));
$this->assertTrue($f->validate('*'));
$this->assertFalse($f->validate('*/3,1,1-12'));
}
}
/**
* @covers \Cron\HoursField::isSatisfiedBy
*/
public function testChecksIfSatisfied()
{
$f = new HoursField();
$this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
$this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
}
/**
* @covers \Cron\HoursField::increment
@@ -39,6 +50,17 @@ class HoursFieldTest extends TestCase
$this->assertSame('2011-03-15 10:59:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\HoursField::increment
*/
public function testIncrementsDateTimeImmutable()
{
$d = new DateTimeImmutable('2011-03-15 11:15:00');
$f = new HoursField();
$f->increment($d);
$this->assertSame('2011-03-15 12:00:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\HoursField::increment
*/

View File

@@ -4,6 +4,7 @@ namespace Cron\Tests;
use Cron\MinutesField;
use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
/**
@@ -22,6 +23,16 @@ class MinutesFieldTest extends TestCase
$this->assertFalse($f->validate('*/3,1,1-12'));
}
/**
* @covers \Cron\MinutesField::isSatisfiedBy
*/
public function testChecksIfSatisfied()
{
$f = new MinutesField();
$this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
$this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
}
/**
* @covers \Cron\MinutesField::increment
*/
@@ -35,6 +46,17 @@ class MinutesFieldTest extends TestCase
$this->assertSame('2011-03-15 11:15:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\MinutesField::increment
*/
public function testIncrementsDateTimeImmutable()
{
$d = new DateTimeImmutable('2011-03-15 11:15:00');
$f = new MinutesField();
$f->increment($d);
$this->assertSame('2011-03-15 11:16:00', $d->format('Y-m-d H:i:s'));
}
/**
* Various bad syntaxes that are reported to work, but shouldn't.
*

View File

@@ -4,6 +4,7 @@ namespace Cron\Tests;
use Cron\MonthField;
use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
/**
@@ -23,6 +24,16 @@ class MonthFieldTest extends TestCase
$this->assertFalse($f->validate('1.fix-regexp'));
}
/**
* @covers \Cron\MonthField::isSatisfiedBy
*/
public function testChecksIfSatisfied()
{
$f = new MonthField();
$this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
$this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
}
/**
* @covers \Cron\MonthField::increment
*/
@@ -38,6 +49,17 @@ class MonthFieldTest extends TestCase
$this->assertSame('2011-02-28 23:59:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\MonthField::increment
*/
public function testIncrementsDateTimeImmutable()
{
$d = new DateTimeImmutable('2011-03-15 11:15:00');
$f = new MonthField();
$f->increment($d);
$this->assertSame('2011-04-01 00:00:00', $d->format('Y-m-d H:i:s'));
}
/**
* @covers \Cron\MonthField::increment
*/