Skip to content

Commit

Permalink
ListCommand: sort jobs by keys instead of generated names
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Apr 7, 2024
1 parent 8703b0d commit 89dbd5a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `getExtendedExpression()` includes timezone if timezone is used
- `ListCommand`
- more compact render
- sort jobs by keys instead of generated names

### Fixed

Expand Down
14 changes: 2 additions & 12 deletions src/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use function is_bool;
use function is_int;
use function is_string;
use function ksort;
use function max;
use function mb_strlen;
use function preg_match;
use function sprintf;
use function str_repeat;
use function strnatcmp;
use function timezone_identifiers_list;
use function uasort;

Expand Down Expand Up @@ -274,17 +274,7 @@ private function sortJobs(array $jobSchedules, $next, DateTimeZone $timeZone): a
$jobSchedules = $slicedJobs;
}
} else {
/** @infection-ignore-all */
uasort($jobSchedules, static function (JobSchedule $a, JobSchedule $b): int {
$nameA = $a->getJob()->getName();
$nameB = $b->getJob()->getName();

if ($nameA === $nameB) {
return 0;
}

return strnatcmp($nameA, $nameB);
});
ksort($jobSchedules);
}

return $jobSchedules;
Expand Down
38 changes: 21 additions & 17 deletions tests/Unit/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ public function testList(): void
$scheduler->addJob(
new CallbackJob(Closure::fromCallable([$cbs, 'job1'])),
new CronExpression('* * * * *'),
'b',
);
$scheduler->addJob(
new CallbackJob(Closure::fromCallable([$cbs, 'job1'])),
new CronExpression('*/30 7-15 * * 1-5'),
'10',
);
$scheduler->addJob(
new CallbackJob(Closure::fromCallable($cbs)),
new CronExpression('* * * 4 *'),
'a',
);
$scheduler->addJob(
new CallbackJob($cbs->getClosure()),
new CronExpression('30 * 12 10 *'),
'1',
);

$command = new ListCommand($scheduler, $clock);
Expand All @@ -73,10 +77,10 @@ public function testList(): void

self::assertSame(
<<<'MSG'
* * * 4 * [2] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 2 months
* * * * * [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() Next Due: 59 seconds
*/30 7-15 * * 1-5 [1] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() Next Due: 5 hours
30 * 12 10 * [3] tests/Doubles/CallbackList.php:32... Next Due: 9 months
30 * 12 10 * [1] tests/Doubles/CallbackList.php:32... Next Due: 9 months
*/30 7-15 * * 1-5 [10] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() Next Due: 5 hours
* * * 4 * [a] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 2 months
* * * * * [b] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() Next Due: 59 seconds

MSG,
CommandOutputHelper::getCommandOutput($tester),

Check failure on line 86 in tests/Unit/Command/ListCommandTest.php

View workflow job for this annotation

GitHub Actions / Test for mutants (ubuntu-latest, 8.1)

Failed asserting that two strings are identical.

Check failure on line 86 in tests/Unit/Command/ListCommandTest.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.1, false)

Failed asserting that two strings are identical.
Expand All @@ -88,10 +92,10 @@ public function testList(): void

self::assertSame(
<<<'MSG'
* * * 4 * [2] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke()........... Next Due: 2 months
* * * * * [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()............. Next Due: 59 seconds
*/30 7-15 * * 1-5 [1] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()................ Next Due: 5 hours
30 * 12 10 * [3] tests/Doubles/CallbackList.php:32................................. Next Due: 9 months
30 * 12 10 * [1] tests/Doubles/CallbackList.php:32................................. Next Due: 9 months
*/30 7-15 * * 1-5 [10] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()............... Next Due: 5 hours
* * * 4 * [a] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke()........... Next Due: 2 months
* * * * * [b] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()............. Next Due: 59 seconds

MSG,
CommandOutputHelper::getCommandOutput($tester),
Expand All @@ -105,10 +109,10 @@ public function testList(): void

self::assertSame(
<<<'MSG'
* * * 4 * [2] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke().... Next Due: 1970-04-01 00:00:00 +01:00
* * * * * [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()........ Next Due: 1970-01-01 01:01:00 +01:00
*/30 7-15 * * 1-5 [1] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()........ Next Due: 1970-01-01 07:00:00 +01:00
30 * 12 10 * [3] tests/Doubles/CallbackList.php:32.......................... Next Due: 1970-10-12 00:30:00 +01:00
30 * 12 10 * [1] tests/Doubles/CallbackList.php:32.......................... Next Due: 1970-10-12 00:30:00 +01:00
*/30 7-15 * * 1-5 [10] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()....... Next Due: 1970-01-01 07:00:00 +01:00
* * * 4 * [a] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke().... Next Due: 1970-04-01 00:00:00 +01:00
* * * * * [b] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()........ Next Due: 1970-01-01 01:01:00 +01:00

MSG,
CommandOutputHelper::getCommandOutput($tester),
Expand Down Expand Up @@ -159,10 +163,10 @@ public function testListWithSeconds(): void

self::assertSame(
<<<'MSG'
* * * 4 * / 5 [3] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 1970-04-01 00:00:00 +01:00
* * * * * [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1().... Next Due: 1970-01-01 01:01:00 +01:00
*/30 7-15 * * 1-5 [1] Tests\Orisai\Scheduler\Doubles\CallbackList::job1().... Next Due: 1970-01-01 07:00:00 +01:00
*/30 7-15 * * 1-5 / 1 [2] Tests\Orisai\Scheduler\Doubles\CallbackList::job1().... Next Due: 1970-01-01 07:00:00 +01:00
* * * 4 * / 5 [3] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 1970-04-01 00:00:00 +01:00
30 * 12 10 * / 30 [4] tests/Doubles/CallbackList.php:32...................... Next Due: 1970-10-12 00:30:00 +01:00

MSG,
Expand Down Expand Up @@ -444,14 +448,14 @@ public function testExplain(): void

self::assertSame(
$explainDefault = <<<'MSG'
* * * 4 * / 10 [2] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 2 months
At every 10 seconds in April.
* * * 3 * / 15 (America/New_York) [3] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 1 month
At every 15 seconds in March in America/New_York time zone.
* * * * * [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() Next Due: 59 seconds
At every minute.
*/30 7-15 * * 1-5 (America/New_York) [1] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() Next Due: 5 hours
At every 30th minute past every hour from 7 through 15 on every day-of-week from Monday through Friday in America/New_York time zone.
* * * 4 * / 10 [2] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 2 months
At every 10 seconds in April.
* * * 3 * / 15 (America/New_York) [3] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 1 month
At every 15 seconds in March in America/New_York time zone.

MSG,
CommandOutputHelper::getCommandOutput($tester),
Expand Down

0 comments on commit 89dbd5a

Please sign in to comment.