|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Winter\Storm\Tests\Scheduling; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Console\Scheduling\Schedule; |
| 7 | +use Illuminate\Console\Scheduling\ScheduleListCommand; |
| 8 | +use Illuminate\Support\Carbon; |
| 9 | +use Illuminate\Support\ProcessUtils; |
| 10 | + |
| 11 | +use Winter\Storm\Tests\TestCase; |
| 12 | + |
| 13 | +class ScheduleListCommandTest extends TestCase |
| 14 | +{ |
| 15 | + public $schedule; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + Carbon::setTestNow('2023-01-01'); |
| 22 | + ScheduleListCommand::resolveTerminalWidthUsing(fn () => 80); |
| 23 | + |
| 24 | + $this->schedule = $this->app->make(Schedule::class); |
| 25 | + } |
| 26 | + |
| 27 | + public function testDisplayEmptySchedule() |
| 28 | + { |
| 29 | + $this->artisan(ScheduleListCommand::class) |
| 30 | + ->assertSuccessful() |
| 31 | + ->expectsOutputToContain('No scheduled tasks have been defined.'); |
| 32 | + } |
| 33 | + |
| 34 | + public function testDisplaySchedule() |
| 35 | + { |
| 36 | + $this->schedule->command(FooCommand::class)->quarterly(); |
| 37 | + $this->schedule->command('inspire')->twiceDaily(14, 18); |
| 38 | + $this->schedule->command('foobar', ['a' => 'b'])->everyMinute(); |
| 39 | + $this->schedule->job(FooJob::class)->everyMinute(); |
| 40 | + $this->schedule->command('inspire')->cron('0 9,17 * * *'); |
| 41 | + $this->schedule->command('inspire')->cron("0 10\t* * *"); |
| 42 | + $this->schedule->call(FooCall::class)->everyMinute(); |
| 43 | + $this->schedule->call([FooCall::class, 'fooFunction'])->everyMinute(); |
| 44 | + |
| 45 | + $this->schedule->call(fn () => '')->everyMinute(); |
| 46 | + $closureLineNumber = __LINE__ - 1; |
| 47 | + $closureFilePath = __FILE__; |
| 48 | + |
| 49 | + $this->artisan(ScheduleListCommand::class) |
| 50 | + ->assertSuccessful() |
| 51 | + ->expectsOutput(' 0 0 1 1-12/3 * php artisan foo:command .... Next Due: 3 months from now') |
| 52 | + ->expectsOutput(' 0 14,18 * * * php artisan inspire ........ Next Due: 14 hours from now') |
| 53 | + ->expectsOutput(' * * * * * php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now') |
| 54 | + ->expectsOutput(' * * * * * Winter\Storm\Tests\Scheduling\FooJob Next Due: 1 minute from now') |
| 55 | + ->expectsOutput(' 0 9,17 * * * php artisan inspire ......... Next Due: 9 hours from now') |
| 56 | + ->expectsOutput(' 0 10 * * * php artisan inspire ........ Next Due: 10 hours from now') |
| 57 | + ->expectsOutput(' * * * * * Closure at: Winter\Storm\Tests\Scheduling\FooCall Next Due: 1 minute from now') |
| 58 | + ->expectsOutput(' * * * * * Closure at: Winter\Storm\Tests\Scheduling\FooCall::fooFunction Next Due: 1 minute from now') |
| 59 | + ->expectsOutput(' * * * * * Closure at: '.$closureFilePath.':'.$closureLineNumber.' Next Due: 1 minute from now'); |
| 60 | + } |
| 61 | + |
| 62 | + public function testDisplayScheduleWithSort() |
| 63 | + { |
| 64 | + $this->schedule->command(FooCommand::class)->quarterly(); |
| 65 | + $this->schedule->command('inspire')->twiceDaily(14, 18); |
| 66 | + $this->schedule->command('foobar', ['a' => 'b'])->everyMinute(); |
| 67 | + $this->schedule->job(FooJob::class)->everyMinute(); |
| 68 | + $this->schedule->command('inspire')->cron('0 9,17 * * *'); |
| 69 | + $this->schedule->command('inspire')->cron("0 10\t* * *"); |
| 70 | + $this->schedule->call(FooCall::class)->everyMinute(); |
| 71 | + $this->schedule->call([FooCall::class, 'fooFunction'])->everyMinute(); |
| 72 | + |
| 73 | + $this->schedule->call(fn () => '')->everyMinute(); |
| 74 | + $closureLineNumber = __LINE__ - 1; |
| 75 | + $closureFilePath = __FILE__; |
| 76 | + |
| 77 | + $this->artisan(ScheduleListCommand::class, ['--next' => true]) |
| 78 | + ->assertSuccessful() |
| 79 | + ->expectsOutput(' * * * * * php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now') |
| 80 | + ->expectsOutput(' * * * * * Winter\Storm\Tests\Scheduling\FooJob Next Due: 1 minute from now') |
| 81 | + ->expectsOutput(' * * * * * Closure at: Winter\Storm\Tests\Scheduling\FooCall Next Due: 1 minute from now') |
| 82 | + ->expectsOutput(' * * * * * Closure at: Winter\Storm\Tests\Scheduling\FooCall::fooFunction Next Due: 1 minute from now') |
| 83 | + ->expectsOutput(' * * * * * Closure at: '.$closureFilePath.':'.$closureLineNumber.' Next Due: 1 minute from now') |
| 84 | + ->expectsOutput(' 0 9,17 * * * php artisan inspire ......... Next Due: 9 hours from now') |
| 85 | + ->expectsOutput(' 0 10 * * * php artisan inspire ........ Next Due: 10 hours from now') |
| 86 | + ->expectsOutput(' 0 14,18 * * * php artisan inspire ........ Next Due: 14 hours from now') |
| 87 | + ->expectsOutput(' 0 0 1 1-12/3 * php artisan foo:command .... Next Due: 3 months from now'); |
| 88 | + } |
| 89 | + |
| 90 | + public function testDisplayScheduleInVerboseMode() |
| 91 | + { |
| 92 | + $this->schedule->command(FooCommand::class)->everyMinute(); |
| 93 | + |
| 94 | + $this->artisan(ScheduleListCommand::class, ['-v' => true]) |
| 95 | + ->assertSuccessful() |
| 96 | + ->expectsOutputToContain('Next Due: '.now()->setMinutes(1)->format('Y-m-d H:i:s P')) |
| 97 | + ->expectsOutput(' ⇁ This is the description of the command.'); |
| 98 | + } |
| 99 | + |
| 100 | + protected function tearDown(): void |
| 101 | + { |
| 102 | + putenv('SHELL_VERBOSITY'); |
| 103 | + |
| 104 | + parent::tearDown(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class FooCommand extends Command |
| 109 | +{ |
| 110 | + protected $signature = 'foo:command'; |
| 111 | + |
| 112 | + protected $description = 'This is the description of the command.'; |
| 113 | +} |
| 114 | + |
| 115 | +class FooJob |
| 116 | +{ |
| 117 | +} |
| 118 | + |
| 119 | +class FooParamJob |
| 120 | +{ |
| 121 | + public function __construct($param) |
| 122 | + { |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class FooCall |
| 127 | +{ |
| 128 | + public function __invoke(): void |
| 129 | + { |
| 130 | + } |
| 131 | + |
| 132 | + public function fooFunction(): void |
| 133 | + { |
| 134 | + } |
| 135 | +} |
0 commit comments