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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require-dev": {
"blitz-php/coding-standard": "^1.4",
"blitz-php/framework": "^0.11.3",
"blitz-php/http-client": "^0.0.2",
"kahlan/kahlan": "^6.0",
"phpstan/phpstan": "^2.1"
},
Expand Down
52 changes: 51 additions & 1 deletion spec/FrequenciesTrait.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

use BlitzPHP\Tasks\FrequenciesTrait;
use BlitzPHP\Utilities\Date;

use function Kahlan\expect;

Expand Down Expand Up @@ -41,8 +42,13 @@

it('testDailyWithTime', function () {
$this->class->daily('4:08 pm');
expect('08 16 * * *')->toBe($this->class->getExpression());

$this->class->at('4:08 pm');
expect('08 16 * * *')->toBe($this->class->getExpression());

$this->class->at('04:28');
expect('28 4 * * *')->toBe($this->class->getExpression());
});

it('testTime', function () {
Expand Down Expand Up @@ -233,8 +239,19 @@

it('testEveryHourWithHour', function () {
$this->class->everyHour(3);
$this->assertSame('0 */3 * * *', $this->class->getExpression());

$this->class->everyTwoHours();
$this->assertSame('0 */2 * * *', $this->class->getExpression());

$this->class->everyThreeHours();
$this->assertSame('0 */3 * * *', $this->class->getExpression());

$this->class->everyFourHours();
$this->assertSame('0 */4 * * *', $this->class->getExpression());

$this->class->everySixHours();
$this->assertSame('0 */6 * * *', $this->class->getExpression());
});

it('testEveryHourWithHourAndMinutes', function () {
Expand All @@ -243,6 +260,12 @@
$this->assertSame('15 */3 * * *', $this->class->getExpression());
});

it('testEveryOddHour', function () {
$this->class->everyOddHour();

$this->assertSame('0 1-23/2 * * *', $this->class->getExpression());
});

it('testBetweenHours', function () {
$this->class->betweenHours(10, 12);

Expand Down Expand Up @@ -275,8 +298,22 @@

it('testEveryMinuteWithParameter', function () {
$this->class->everyMinute(15);

$this->assertSame('*/15 * * * *', $this->class->getExpression());

$this->class->everyTwoMinutes();
$this->assertSame('*/2 * * * *', $this->class->getExpression());

$this->class->everyThreeMinutes();
$this->assertSame('*/3 * * * *', $this->class->getExpression());

$this->class->everyFourMinutes();
$this->assertSame('*/4 * * * *', $this->class->getExpression());

$this->class->everyTenMinutes();
$this->assertSame('*/10 * * * *', $this->class->getExpression());

$this->class->everyThirtyMinutes();
$this->assertSame('*/30 * * * *', $this->class->getExpression());
});

it('testBetweenMinutes', function () {
Expand All @@ -303,6 +340,13 @@
$this->assertSame('* * 1,15 * *', $this->class->getExpression());
});

it('testLastDayOfMonth', function () {
$this->class->lastDayOfMonth();
$lastDay = Date::now()->endOfMonth()->getDay();

$this->assertSame('0 0 ' . $lastDay . ' * *', $this->class->getExpression());
});

it('testMonths', function () {
$this->class->months([1, 7]);

Expand All @@ -314,4 +358,10 @@

$this->assertSame('* * * 1-7 *', $this->class->getExpression());
});

it('testBetween', function () {
$this->class->between('10:00', '12:30');

$this->assertSame('0-30 10-12 * * *', $this->class->getExpression());
});
});
147 changes: 147 additions & 0 deletions spec/HooksTrait.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

/**
* This file is part of BlitzPHP Tasks.
*
* (c) 2025 Dimitri Sitchet Tomkeu <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

use BlitzPHP\Tasks\Task;

use function Kahlan\expect;

describe('HookTrait', function () {
it('testAfter et testBefore', function () {
expect(file_exists($path = __DIR__.'/test-hook.txt'))->toBeFalsy();

$task = new Task('closure', fn() => 'Hello');
$task->before(function() use($path) {
file_put_contents($path, 'before');
});

$task->after(function() use($path) {
file_put_contents($path, 'after', FILE_APPEND);
});

$result = $task->run();

expect(file_exists($path))->toBeTruthy();
$content = file_get_contents($path);

expect(str_contains($content, 'before'))->toBeTruthy();
expect(str_contains($content, 'after'))->toBeTruthy();
expect($result)->toBe('Hello');

unlink($path);
});

it('sendOutputTo', function () {
$task = new Task('closure', function() {
echo 'Hello';

return 'world';
});

$task->sendOutputTo($path = __DIR__ . '/output.txt');

$result = $task->run();

expect(file_exists($path))->toBeTruthy();
expect(file_get_contents($path))->toBe('Hello');
expect($result)->toBe('world');

unlink($path);
});

it('appendOutputTo', function () {
$task = new Task('closure', function() {
echo 'Hello';
});

$task->appendOutputTo($path = __DIR__ . '/output.txt');

file_put_contents($path, 'World');

$task->run();

expect(file_exists($path))->toBeTruthy();
expect(file_get_contents($path))->toBe('WorldHello');

unlink($path);
});

it('onSuccess', function () {
$task = new Task('closure', fn() => 'Hello');

expect(file_exists($path = __DIR__.'/test-hook.txt'))->toBeFalsy();

$task->onSuccess(function() use($path) {
file_put_contents($path, 'Success!');
});

$task->run();

expect(file_exists($path))->toBeTruthy();
expect(file_get_contents($path))->toBe('Success!');

unlink($path);
});

it('onFailure', function () {
$task = new Task('closure', function() {
throw new Exception('Error');
});

expect(file_exists($path = __DIR__.'/test-hook.txt'))->toBeFalsy();

$task->onFailure(function() use($path) {
file_put_contents($path, 'Failure!');
});

$task->run();

expect(file_exists($path))->toBeTruthy();
expect(file_get_contents($path))->toBe('Failure!');

unlink($path);
});

it('onFailure lorsqu\'il y\'a pas exception mais on renvoie un code d\'erreur', function () {
$task = new Task('closure', fn() => EXIT_ERROR);

expect(file_exists($path = __DIR__.'/test-hook.txt'))->toBeFalsy();

$task->onFailure(function() use($path) {
file_put_contents($path, 'Failure!');
});

$task->run();

expect(file_exists($path))->toBeTruthy();
expect(file_get_contents($path))->toBe('Failure!');

unlink($path);
});

it('onFailure avec recuperation de l\'exception', function () {
$task = new Task('closure', function() {
throw new Exception('Error');
});

expect(file_exists($path = __DIR__.'/test-hook.txt'))->toBeFalsy();

$task->onFailure(function(Throwable $e) use($path) {
file_put_contents($path, $e->getMessage());
});

$task->run();

expect(file_exists($path))->toBeTruthy();
expect(file_get_contents($path))->toBe('Error');

unlink($path);
});
});
17 changes: 17 additions & 0 deletions spec/Scheduler.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,22 @@ public function __invoke(Container $container)

expect($task)->toBeAnInstanceOf(Task::class);
expect('foo:bar')->toBe($task->getAction());
expect('shell')->toBe($task->getType());
});

it('Peut sauvegarder un evenement', function () {
$task = $this->scheduler->event('foo.bar');

expect($task)->toBeAnInstanceOf(Task::class);
expect('foo.bar')->toBe($task->getAction());
expect('event')->toBe($task->getType());
});

it('Peut sauvegarder un appel d\'URL', function () {
$task = $this->scheduler->url('http://localhost');

expect($task)->toBeAnInstanceOf(Task::class);
expect('http://localhost')->toBe($task->getAction());
expect('url')->toBe($task->getType());
});
});
57 changes: 57 additions & 0 deletions spec/Task.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use BlitzPHP\Spec\ReflectionHelper;
use BlitzPHP\Tasks\Task;
use BlitzPHP\Utilities\Date;
use BlitzPHP\Utilities\Helpers;

use function Kahlan\expect;

Expand Down Expand Up @@ -62,6 +63,17 @@
$task = new Task('command', 'foo:bar');

expect($task->getType())->toBe('command');
expect($task->type)->toBe('command');
});

it('__set', function () {
$task = new Task('command', 'foo:bar');

$task->fake = 'foo:bar';

$attributes = ReflectionHelper::getPrivateProperty($task, 'attributes');

expect($attributes)->toBe(['fake' => 'foo:bar']);
});

it("Execution d'une commande", function () {
Expand All @@ -79,6 +91,10 @@

expect($task->shouldRun('12:05am'))->toBeFalsy();
expect($task->shouldRun('12:00am'))->toBeTruthy();

$task = (new Task('command', 'tasks:test'))->hourly()->environments('production');

expect($task->shouldRun('12:00am'))->toBeFalsy();
});

it("Peut s'executer dans un environnement donné", function () {
Expand Down Expand Up @@ -120,4 +136,45 @@
expect($task->lastRun())->toBeAnInstanceOf(Date::class); // @phpstan-ignore-line
expect($task->lastRun()->format('Y-m-d H:i:s'))->toBe($date);
});

it('Peut executer une commande shell', function () {
expect(file_exists($path = __DIR__ . '/test.php'))->toBeFalsy();

$task = new Task('shell', 'cp ' . __FILE__ . ' ' . $path);
$task->run();

expect(file_exists($path))->toBeTruthy();

$task = new Task('shell', 'rm ' . $path);
$task->run();

expect(file_exists($path))->toBeFalsy();
});

it('Peut executer un evenement', function () {
expect(file_exists($path = __DIR__ . '/test.txt'))->toBeFalsy();

service('event')->on($event = 'test.event', function() use($path) {
file_put_contents($path, 'event.txt');
});

$task = new Task('event', $event);
$task->run();

expect(file_exists($path))->toBeTruthy();
expect(file_get_contents($path))->toBe('event.txt');

unlink($path);
});

it('Peut executer une URL', function () {
skipIf(! Helpers::isConnected());

$task = new Task('url', 'https://raw.githubusercontent.com/blitz-php/tasks/refs/heads/main/composer.json');
$result = $task->run();
$result = json_decode($result, true);

expect($result)->toContainKey('name');
expect($result['name'])->toBe('blitz-php/tasks');
});
});
2 changes: 1 addition & 1 deletion src/FrequenciesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function time(string $time): self
*/
public function hourly(?int $minute = null): self
{
return $this->everyHour(1, $minute);
return $this->everyHour(1, $minute ?? '00');
}

/**
Expand Down
Loading
Loading