Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate most "Event::every*" methods #344

Merged
merged 2 commits into from
Dec 27, 2020
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/code_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ jobs:
- '8.0'
vars:
- symfony_version: "^v3.4.5"
phpunit_bridge_version: "^v3.4.40"
phpunit_bridge_version: "^v3.4.41"
dependencies: "high"
- symfony_version: "^v3.4.5"
phpunit_bridge_version: "^v3.4.40"
phpunit_bridge_version: "^v3.4.41"
dependencies: "low"
- symfony_version: "~v4.4.0"
phpunit_bridge_version: "~v4.4.8"
phpunit_bridge_version: "~v4.4.9"
dependencies: "high"
- symfony_version: "~v4.4.0"
phpunit_bridge_version: "~v4.4.8"
phpunit_bridge_version: "~v4.4.9"
dependencies: "low"
- symfony_version: "~v5.1.0"
phpunit_bridge_version: "~v5.1.0"
Expand Down
41 changes: 0 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,47 +368,6 @@ $task
>(An easier to read alternative with a similar result ->weeklyOn(0,'13:30') to that shown in a previously example above)


### Dynamic Methods

Dynamic methods give us a wide variety of frequency options on the fly. We just need to follow this pattern:

```text
every[NumberInCamelCaseWords]Minute|Hour|Day|Months?
```

As we can see, the method should start with the word `every`, followed by a number in camel-case words, ending with one of these units of time: **minute, hour, day, and month**.

The `s` at the end is optional and it's just used for grammar's sake.

With that said, the following methods are valid:

* `everyFiveMinutes()`
* `everyMinute()`
* `everyHour()` (same result as `hourly()`)
* `everyTwelveHours()`
* `everyMonth` (same result as `monthly()`)
* `everySixMonths()`
* `everyFifteenDays()`
* `everyFiveHundredThirtySevenMinutes()`
* `everyThreeThousandAndFiveHundredFiftyNineMinutes()`
* ...

This is how it is used in a task file:

```php
<?php
// ...

$task = $schedule->run(PHP_BINARY . ' email.php');
$task->everyTenDays();

$task = $schedule->run(PHP_BINARY . ' some_other_stuff.php');
$task->everyThirteenMinutes();
// ...

return $schedule;
```

### The Classic Way

We can also do the scheduling the old way, just like we do in a crontab file:
Expand Down
74 changes: 67 additions & 7 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
use Symfony\Component\Lock\Store\FlockStore;
use Symfony\Component\Lock\StoreInterface;

/**
* @method self everyMinute() Run task every minute.
* @method self everyFiveMinutes() Run task every five minutes.
* @method self everyHour() Run task every hour.
* @method self everyDay() Run task every day.
* @method self everyMonth() Run task every month.
*/
class Event implements PingableInterface
{
use PingableTrait;
Expand Down Expand Up @@ -205,6 +198,8 @@ public function __construct($id, $command)
*/
public function __call($methodName, $params)
{
// @TODO remove this method after v3.0 release

\preg_match('/^every([A-Z][a-zA-Z]+)?(Minute|Hour|Day|Month)s?$/', $methodName, $matches);

if (!\count($matches) || 'Zero' === $matches[1]) {
Expand All @@ -217,6 +212,11 @@ public function __call($methodName, $params)
throw new \BadMethodCallException();
}

@\trigger_error(
"Method '{$methodName}' is deprecated since v2.3, use 'cron' method instead.",
\E_USER_DEPRECATED
);

return $this->every(\mb_strtolower($matches[2]), $amount);
}

Expand Down Expand Up @@ -1055,6 +1055,66 @@ public function refreshLock(): void
}
}

public function everyMinute(): self
{
return $this->cron('* * * * *');
}

public function everyTwoMinutes(): self
{
return $this->cron('*/2 * * * *');
}

public function everyThreeMinutes(): self
{
return $this->cron('*/3 * * * *');
}

public function everyFourMinutes(): self
{
return $this->cron('*/4 * * * *');
}

public function everyFiveMinutes(): self
{
return $this->cron('*/5 * * * *');
}

public function everyTenMinutes(): self
{
return $this->cron('*/10 * * * *');
}

public function everyFifteenMinutes(): self
{
return $this->cron('*/15 * * * *');
}

public function everyThirtyMinutes(): self
{
return $this->cron('*/30 * * * *');
}

public function everyTwoHours(): self
{
return $this->cron('0 */2 * * *');
}

public function everyThreeHours(): self
{
return $this->cron('0 */3 * * *');
}

public function everyFourHours(): self
{
return $this->cron('0 */4 * * *');
}

public function everySixHours(): self
{
return $this->cron('0 */6 * * *');
}

/**
* Get the symfony lock object for the task.
*
Expand Down
85 changes: 59 additions & 26 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,6 @@ public function tearDown(): void
\date_default_timezone_set($this->defaultTimezone);
}

/**
* @group cronCompile
*/
public function testDynamicMethods(): void
{
$e = new Event($this->id, 'php foo');
$this->assertEquals('*/6 * * * *', $e->everySixMinutes()->getExpression());

$e = new Event($this->id, 'php bar');
$this->assertEquals('0 */12 * * *', $e->everyTwelveHours()->getExpression());

$e = new Event($this->id, 'php foo');
$this->assertEquals('*/35 * * * *', $e->everyThirtyFiveMinutes()->getExpression());

$e = new Event($this->id, 'php bar');
$this->assertEquals('*/578 * * * *', $e->everyFiveHundredSeventyEightMinutes()->getExpression());

$this->setClockNow(new \DateTimeImmutable('2018-10-23 11:33:18'));

$e = new Event($this->id, 'php foo');
$e->everyFiftyMinutes()->mondays();

$this->assertEquals('*/50 * * * 1', $e->getExpression());
$this->assertFalse($e->isDue(new \DateTimeZone('UTC')));
}

/**
* @group cronCompile
*/
Expand Down Expand Up @@ -404,6 +378,65 @@ public function taskWillPreventOverlappingWithSemaphoreStore(): void
$this->assertPreventOverlapping(new SemaphoreStore());
}

/**
* @group legacy
* @expectedDeprecation Method '%s' is deprecated since v2.3, use 'cron' method instead.
* @dataProvider deprecatedEveryProvider
*/
public function test_most_every_methods_are_deprecated(string $method): void
{
// Arrange
$event = new Event($this->id, 'php -i');
/** @var callable $methodCall */
$methodCall = [$event, $method];
$methodCallClosure = \Closure::fromCallable($methodCall);

// Act
$methodCallClosure();
}

/** @dataProvider everyMethodProvider */
public function test_every_methods(string $method, string $expectedCronExpression): void
{
// Arrange
$event = new Event($this->id, 'php -i');
/** @var callable $methodCall */
$methodCall = [$event, $method];
$methodCallClosure = \Closure::fromCallable($methodCall);

// Act
$methodCallClosure();

// Assert
$this->assertSame($expectedCronExpression, $event->getExpression());
}

/** @return iterable<string,array> */
public function deprecatedEveryProvider(): iterable
{
yield 'every seven minutes' => ['everySevenMinutes'];
yield 'every five hours' => ['everyFiveHours'];
yield 'every two days' => ['everyTwoDays'];
yield 'every five months' => ['everyFiveMonths'];
}

/** @return iterable<string,array> */
public function everyMethodProvider(): iterable
{
yield 'every minute' => ['everyMinute', '* * * * *'];
yield 'every two minutes' => ['everyTwoMinutes', '*/2 * * * *'];
yield 'every three minutes' => ['everyThreeMinutes', '*/3 * * * *'];
yield 'every four minutes' => ['everyFourMinutes', '*/4 * * * *'];
yield 'every five minutes' => ['everyFiveMinutes', '*/5 * * * *'];
yield 'every ten minutes' => ['everyTenMinutes', '*/10 * * * *'];
yield 'every fifteen minutes' => ['everyFifteenMinutes', '*/15 * * * *'];
yield 'every thirty minutes' => ['everyThirtyMinutes', '*/30 * * * *'];
yield 'every two hours' => ['everyTwoHours', '0 */2 * * *'];
yield 'every three hours' => ['everyThreeHours', '0 */3 * * *'];
yield 'every four hours' => ['everyFourHours', '0 */4 * * *'];
yield 'every six hours' => ['everySixHours', '0 */6 * * *'];
}

/** @param StoreInterface|BlockingStoreInterface $store */
private function assertPreventOverlapping($store = null): void
{
Expand Down