Skip to content
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
16 changes: 15 additions & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Crunz\Application\Service\ClosureSerializerInterface;
use Crunz\Clock\Clock;
use Crunz\Clock\ClockInterface;
use Crunz\Exception\CrunzException;
use Crunz\Exception\NotImplementedException;
use Crunz\Infrastructure\Laravel\LaravelClosureSerializer;
use Crunz\Logger\Logger;
Expand Down Expand Up @@ -352,7 +353,20 @@ public function cron(string $expression): self
*/
public function hourly(): self
{
return $this->cron('0 * * * *');
return $this->hourlyAt(0);
}

public function hourlyAt(int $minute): self
{
if ($minute < 0) {
throw new CrunzException("Minute cannot be lower than '0'.");
}

if ($minute > 59) {
throw new CrunzException("Minute cannot be greater than '59'.");
}

return $this->cron("{$minute} * * * *");
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Crunz\Tests\Unit;

use Crunz\Event;
use Crunz\Exception\CrunzException;
use Crunz\Task\TaskException;
use Crunz\Tests\TestCase\Faker;
use Crunz\Tests\TestCase\TestClock;
use Crunz\Tests\TestCase\UnitTestCase;
use Symfony\Component\Lock\BlockingStoreInterface;
Expand Down Expand Up @@ -383,6 +385,35 @@ public function test_every_methods(string $method, string $expectedCronExpressio
$this->assertSame($expectedCronExpression, $event->getExpression());
}

public function test_hourly_at_with_valid_minute(): void
{
// Arrange
$event = $this->createEvent();
$minute = Faker::int(0, 59);

// Act
$event->hourlyAt($minute);

// Assert
$this->assertSame("{$minute} * * * *", $event->getExpression());
}

/** @dataProvider hourlyAtInvalidProvider */
public function test_hourly_at_with_invalid_minute(
int $minute,
string $expectedExceptionMessage
): void {
// Arrange
$event = $this->createEvent();

// Expect
$this->expectException(CrunzException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

// Act
$event->hourlyAt($minute);
}

/** @return iterable<string,array> */
public function deprecatedEveryProvider(): iterable
{
Expand All @@ -409,6 +440,20 @@ public function everyMethodProvider(): iterable
yield 'every six hours' => ['everySixHours', '0 */6 * * *'];
}

/** @return iterable<string,array> */
public function hourlyAtInvalidProvider(): iterable
{
yield 'minute below zero' => [
Faker::int(-100, -1),
"Minute cannot be lower than '0'.",
];

yield 'minute above fifty nine' => [
Faker::int(60, 120),
"Minute cannot be greater than '59'.",
];
}

private function assertPreventOverlapping(BlockingStoreInterface $store = null): void
{
$event = $this->createPreventOverlappingEvent($store);
Expand Down Expand Up @@ -443,4 +488,15 @@ private function isWindows(): bool
{
return DIRECTORY_SEPARATOR === '\\';
}

private function createEvent(): Event
{
return new Event(
\uniqid(
'c',
true,
),
'php -i',
);
}
}