Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Created provider for cron expressions. #498

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle

biasedNumberBetween($min, $max, $function) // 42

### `Faker\Provider\Cron`

cron() // 12-38 */4 * * 2000/5
cronMinute() // */12,30-39
cronHour() // 0-23
cronDayOfMonth() // L,14W,30
cronMonth() // 1/2,6-8
cronDayOfWeek() // 0-2,4W,3#2
cronYear() // 2012,2000-2039

## Unique and Optional modifiers

Faker provides two special providers, `unique()` and `optional()`, to be called before any provider. `optional()` can be useful for seeding non-required fields, like a mobile telephone number; `unique()` is required to populate fields that cannot accept twice the same value, like primary identifiers.
Expand Down
2 changes: 1 addition & 1 deletion src/Faker/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Factory
{
const DEFAULT_LOCALE = 'en_US';

protected static $defaultProviders = array('Address', 'Barcode', 'Biased', 'Color', 'Company', 'DateTime', 'File', 'Image', 'Internet', 'Lorem', 'Miscellaneous', 'Payment', 'Person', 'PhoneNumber', 'Text', 'UserAgent', 'Uuid');
protected static $defaultProviders = array('Address', 'Barcode', 'Biased', 'Color', 'Company', 'DateTime', 'File', 'Image', 'Internet', 'Lorem', 'Miscellaneous', 'Payment', 'Person', 'PhoneNumber', 'Text', 'UserAgent', 'Uuid', 'Cron');

public static function create($locale = self::DEFAULT_LOCALE)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@
* @property string $rgbCssColor
* @property string $safeColorName
* @property string $colorName
*
* @method string cronMinute()
* @method string cronHour()
* @method string cronDayOfMonth()
* @method string cronMonth()
* @method string cronDayOfWeek()
* @method string cronYear()
* @method string cron()
*/
class Generator
{
Expand Down
191 changes: 191 additions & 0 deletions src/Faker/Provider/Cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

namespace Faker\Provider;

/**
* @author Jake Soward
*/
class Cron extends \Faker\Provider\Base
{
public function cronMinute()
{
$possibilities = array();

for ($i = 0; $i < 3; $i++) {
$min = $this->randomElement(array( '*', $this->numberBetween(0, 59) ));

if ($this->generator->boolean(30)) {
$possibilities[] = $min.'/'.$this->numberBetween(1, 59);
continue;
}

if ($min !== '*' && $min < 58 && $this->generator->boolean(30)) {
$possibilities[] = $min.'-'.$this->numberBetween($min+1, 59);
continue;
}

$possibilities[] = $min;
}

$combined = array_unique($possibilities);
$possibilities[] = implode(',', $this->randomElements($combined, count($combined)));

return $this->randomElement($possibilities);
}

public function cronHour()
{
$possibilities = array();

for ($i = 0; $i < 3; $i++) {
$hour = $this->randomElement(array( '*', $this->numberBetween(0, 23) ));

if ($this->generator->boolean(30)) {
$possibilities[] = $hour.'/'.$this->numberBetween(1, 23);
continue;
}

if ($hour !== '*' && $hour < 20 && $this->generator->boolean(3)) {
$possibilities[] = $hour.'-'.$this->numberBetween($hour+1, 23);
continue;
}

$possibilities[] = $hour;
}

$combined = array_unique($possibilities);
$possibilities[] = implode(',', $this->randomElements($combined, count($combined)));

return $this->randomElement($possibilities);
}

public function cronDayOfMonth($extended = true)
{
$possibilities = array();

for ($i = 0; $i < 3; $i++) {
$elements = array( '*', $this->numberBetween(1, 31) );
if ($extended) {
$elements[] = 'L';
}
$day = $this->randomElement($elements);

if ($day !== 'L' && $this->generator->boolean(30)) {
$possibilities[] = $day.'/'.$this->numberBetween(1, 31);
continue;
}

if (is_numeric($day) && $day < 28 && $this->generator->boolean(30)) {
$possibilities[] = $day.'-'.$this->numberBetween($day+1, 31);
continue;
}

if ($extended && is_numeric($day) && $this->generator->boolean(30)) {
$possibilities[] = $day.'W';
continue;
}

$possibilities[] = $day;
}

$combined = array_unique($possibilities);
$possibilities[] = implode(',', $this->randomElements($combined, count($combined)));

return $this->randomElement($possibilities);
}

public function cronMonth()
{
$possibilities = array();

for ($i = 0; $i < 3; $i++) {
$month = $this->randomElement(array( '*', $this->numberBetween(1, 12) ));

if ($this->generator->boolean(30)) {
$possibilities[] = $month.'/'.$this->numberBetween(1, 12);
continue;
}

if ($month !== '*' && $month < 11 && $this->generator->boolean(30)) {
$possibilities[] = $month.'-'.$this->numberBetween($month+1, 12);
continue;
}

$possibilities[] = $month;
}

$combined = array_unique($possibilities);
$possibilities[] = implode(',', $this->randomElements($combined, count($combined)));

return $this->randomElement($possibilities);
}

public function cronDayOfWeek($extended = true)
{
$possibilities = array();

for ($i = 0; $i < 3; $i++) {
$day = $this->randomElement(array( '*', $this->numberBetween(0, 6) ));

if ($this->generator->boolean(30)) {
$possibilities[] = $day.'/'.$this->numberBetween(1, 6);
continue;
}

if ($day != '*' && $day < 5 && $this->generator->boolean(30)) {
$possibilities[] = $day.'-'.$this->numberBetween($day+1, 6);
continue;
}

if ($extended && $day !== '*' && $this->generator->boolean(30)) {
$possibilities[] = $day.'L';
continue;
}

if ($extended && $day != '*' && $this->generator->boolean(30)) {
$possibilities[] = $day.'#'.$this->numberBetween(1, 5);
continue;
}

$possibilities[] = $day;
}

$combined = array_unique($possibilities);
$possibilities[] = implode(',', $this->randomElements($combined, count($combined)));

return $this->randomElement($possibilities);
}

public function cronYear()
{
$possibilities = array();

for ($i = 0; $i < 3; $i++) {
$year = $this->randomElement(array( '*', $this->numberBetween(1970, 2099) ));

if ($year != '*' && $this->generator->boolean(30)) {
$possibilities[] = $year.'/'.$this->randomDigit();
continue;
}

if ($year != '*' && $year < 2098 && $this->generator->boolean(30)) {
$possibilities[] = $year.'-'.$this->numberBetween($year+1, 2099);
continue;
}

$possibilities[] = $year;
}

return $this->randomElement($possibilities);
}

public function cron($extended = true)
{
return $this->cronMinute()
. ' ' . $this->cronHour()
. ' ' . $this->cronDayOfMonth($extended)
. ' ' . $this->cronMonth()
. ' ' . $this->cronDayOfWeek($extended)
. ($extended ? ' ' . $this->cronYear() : '' );
}
}
74 changes: 74 additions & 0 deletions test/Faker/Provider/CronTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Faker\Test\Provider;

use Faker\Generator;
use Faker\Provider\Cron;
use Faker\Provider\Miscellaneous;

class CronTest extends \PHPUnit_Framework_TestCase
{
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new Cron($faker));
$faker->addProvider(new Miscellaneous($faker));
$this->faker = $faker;
}

public function testMinute()
{
$minute = $this->faker->cronMinute();
$this->assertRegExp(
'/^[0-9\*\/,-]+$/',
$minute
);
}

public function testHour()
{
$hour = $this->faker->cronHour();
$this->assertRegExp(
'/^[0-9\*\/,-]+$/',
$hour
);
}

public function testDayOfMonth()
{
$dayOfMonth = $this->faker->cronDayOfMonth();
$this->assertRegExp(
'/^[0-9\*\/,-?LW]+$/',
$dayOfMonth
);
}

public function testMonth()
{
$minute = $this->faker->cronMonth();
$this->assertRegExp(
'/^[0-9\*\/,-]+$/',
$minute
);
}

public function testDayOfWeek()
{
$minute = $this->faker->cronDayOfWeek();
$this->assertRegExp(
'/^[0-9\*\/,-?L#]+$/',
$minute
);
}

public function testYear()
{
$minute = $this->faker->cronYear();
$this->assertRegExp(
'/^[0-9\*\/,-]+$/',
$minute
);
}
}