Skip to content

Commit

Permalink
Added some positive test cases, reduced complexity in validator
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Aug 29, 2016
1 parent 12f112f commit 0a7249f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 31 deletions.
50 changes: 30 additions & 20 deletions src/CrontabValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,70 @@
* @author hollodotme
*/

namespace hollodotme\CrontabIntervalValidator;
namespace hollodotme\CrontabValidator;

use hollodotme\CrontabIntervalValidator\Exceptions\CrontabIntervalValidatorException;
use hollodotme\CrontabValidator\Exceptions\InvalidCrontabInterval;

/**
* Class CrontabIntervalValidator
* @package hollodotme\CrontabIntervalValidator
* Class CrontabValidator
* @package hollodotme\CrontabValidator
*/
class CrontabIntervalValidator
class CrontabValidator
{
/** @var string */
private $regexp;
private $intervalRegexp;

public function __construct()
{
$this->regexp = $this->buildCrontabRegexp();
$this->intervalRegexp = $this->buildCrontabIntervalRegexp();
}

private function buildCrontabRegexp() : string
/**
* @return string
*/
private function buildCrontabIntervalRegexp() : string
{
$numbers = [
'min' => '[0-5]?\d',
'hour' => '[01]?\d|2[0-3]',
'date' => '0?[1-9]|[12]\d|3[01]',
'month' => '[1-9]|1[012]',
'weekday' => '[0-7]',
'month' => '[1-9]|1[012]|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec',
'weekday' => '[0-7]|mon|tue|wed|thu|fri|sat|sun',
];

$sections = [];
foreach ( $numbers as $section => $number )
{
$range = "({$number})(-({$number})(\/\d+)?)?";
$sections[ $section ] = "\*(\/\d+)?|{$number}(\/\d+)?|{$range}(,{$range})*";
$range = "({$number})(-({$number})(/\d+)?)?";
$sections[ $section ] = "\*(/\d+)?|{$number}(/\d+)?|{$range}(,{$range})*";
}

$sections['month'] .= '|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec';
$sections['weekday'] .= '|mon|tue|wed|thu|fri|sat|sun';

$joinedSections = '(' . join( ')\s+(', $sections ) . ')';
$replacements = '@reboot|@yearly|@annually|@monthly|@weekly|@daily|@midnight|@hourly';

return "^({$joinedSections}|({$replacements}))$";
}

public function isValid( string $crontabInterval ) : bool
/**
* @param string $crontabInterval
*
* @return bool
*/
public function isIntervalValid( string $crontabInterval ) : bool
{
return (bool)preg_match( "/$this->regexp/umsi", $crontabInterval );
return (bool)preg_match( "#{$this->intervalRegexp}#i", $crontabInterval );
}

public function guardIsValid( string $crontabInterval )
/**
* @param string $crontabInterval
*
* @throws InvalidCrontabInterval
*/
public function guardIntervalIsValid( string $crontabInterval )
{
if ( !$this->isValid( $crontabInterval ) )
if ( !$this->isIntervalValid( $crontabInterval ) )
{
throw new CrontabIntervalValidatorException( 'Invalid crontab interval' );
throw (new InvalidCrontabInterval( 'Invalid crontab interval' ))->withCrontabInterval( $crontabInterval );
}
}
}
8 changes: 4 additions & 4 deletions src/Exceptions/CrontabValidatorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* @author hollodotme
*/

namespace hollodotme\CrontabIntervalValidator\Exceptions;
namespace hollodotme\CrontabValidator\Exceptions;

/**
* Class CrontabIntervalValidatorException
* @package hollodotme\CrontabIntervalValidator\Exceptions
* Class CrontabValidatorException
* @package hollodotme\CrontabValidator\Exceptions
*/
class CrontabIntervalValidatorException extends \Exception
class CrontabValidatorException extends \Exception
{

}
50 changes: 47 additions & 3 deletions tests/Unit/CrontabValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,57 @@
* @author hollodotme
*/

namespace hollodotme\CrontabIntervalValidator\Tests\Unit;
namespace hollodotme\CrontabValidator\Tests\Unit;

use hollodotme\CrontabValidator\CrontabValidator;
use hollodotme\CrontabValidator\Exceptions\InvalidCrontabInterval;

/**
* Class CrontabIntervalValidatorTest
* Class CrontabValidatorTest
* @package hollodotme\CrontabValidator\Tests\Unit
*/
class CrontabIntervalValidatorTest extends \PHPUnit_Framework_TestCase
class CrontabValidatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @param string $crontabInterval
*
* @dataProvider validIntervalProvider
*/
public function testIntervalIsValid( string $crontabInterval )
{
$validator = new CrontabValidator();

$this->assertTrue( $validator->isIntervalValid( $crontabInterval ) );

$validator->guardIntervalIsValid( $crontabInterval );
}

public function validIntervalProvider() : array
{
return require(__DIR__ . '/data/ValidCrontabIntervals.php');
}

/**
* @param string $crontabInterval
*
* @dataProvider invalidIntervalProvider
*/
public function testIntervalIsInValid( string $crontabInterval )
{
$validator = new CrontabValidator();

$this->assertFalse( $validator->isIntervalValid( $crontabInterval ) );
}

public function invalidIntervalProvider() : array
{
return require(__DIR__ . '/data/InvalidCrontabIntervals.php');
}

public function testInvalidIntervalThrowsException()
{
$this->expectException( InvalidCrontabInterval::class );

(new CrontabValidator())->guardIntervalIsValid( ' abc def hij klm nop ' );
}
}
15 changes: 11 additions & 4 deletions tests/Unit/data/ValidCrontabIntervals.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@
['15 7-23/1 * * *'],
['25 7 * * *'],
['35 7 * * *'],
['8,23,38,53 7-23/1 * * *'],
['4,19,34,49 7-23/1 * * *'],
['6,21,36,51 7-23/1 * * *'],
['8,23,38,53 7-23/1 * JAN *'],
['4,19,34,49 7-23/1 * dec *'],
['6,21,36,51 7-23/1 * FEB-NOV/2 *'],
['6 9 * * *'],
['11 9 * * *'],
['10,25,40,55 7-23/1 * * *'],
Expand Down Expand Up @@ -358,7 +358,7 @@
['05,25 04 * * *'],
['45 3 * * *'],
['3 9,15 * * 1-5'],
['7,17 9,15 * * 1-5'],
['7,17 9,15 * * mon-fri'],
['26 0 * * 1-6'],
['*/1 * * * *'],
['2,32 0-21,23 * * *'],
Expand Down Expand Up @@ -414,4 +414,11 @@
['25 3 * * *'],
['30 3 * * *'],
['35 3 * * 3'],
['@annually'],
['@monthly'],
['@daily'],
['@reboot'],
['@yearly'],
['@midnight'],
['@hourly'],
];

0 comments on commit 0a7249f

Please sign in to comment.