|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Console\Scheduling; |
| 4 | + |
| 5 | +use Illuminate\Console\Application; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Console\Scheduling\Schedule; |
| 8 | +use Illuminate\Console\Scheduling\ScheduleTestCommand; |
| 9 | +use Illuminate\Support\Carbon; |
| 10 | +use Orchestra\Testbench\TestCase; |
| 11 | + |
| 12 | +class ScheduleTestCommandTest extends TestCase |
| 13 | +{ |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + Carbon::setTestNow(now()->startOfYear()); |
| 19 | + |
| 20 | + $this->timestamp = now()->startOfYear()->format('c'); |
| 21 | + $this->schedule = $this->app->make(Schedule::class); |
| 22 | + } |
| 23 | + |
| 24 | + public function testRunNoDefinedCommands() |
| 25 | + { |
| 26 | + $this->artisan(ScheduleTestCommand::class) |
| 27 | + ->assertSuccessful() |
| 28 | + ->expectsOutput('No scheduled commands have been defined.'); |
| 29 | + } |
| 30 | + |
| 31 | + public function testRunNoMatchingCommand() |
| 32 | + { |
| 33 | + $this->schedule->command(BarCommandStub::class); |
| 34 | + |
| 35 | + $this->artisan(ScheduleTestCommand::class, ['--name' => 'missing:command']) |
| 36 | + ->assertSuccessful() |
| 37 | + ->expectsOutput('No matching scheduled command found.'); |
| 38 | + } |
| 39 | + |
| 40 | + public function testRunUsingNameOption() |
| 41 | + { |
| 42 | + $this->schedule->command(BarCommandStub::class)->name('bar-command'); |
| 43 | + $this->schedule->job(BarJobStub::class); |
| 44 | + $this->schedule->call(fn () => true)->name('callback'); |
| 45 | + |
| 46 | + $this->artisan(ScheduleTestCommand::class, ['--name' => 'bar:command']) |
| 47 | + ->assertSuccessful() |
| 48 | + ->expectsOutput(sprintf('[%s] Running scheduled command: bar-command', $this->timestamp)); |
| 49 | + |
| 50 | + $this->artisan(ScheduleTestCommand::class, ['--name' => BarJobStub::class]) |
| 51 | + ->assertSuccessful() |
| 52 | + ->expectsOutput(sprintf('[%s] Running scheduled command: %s', $this->timestamp, BarJobStub::class)); |
| 53 | + |
| 54 | + $this->artisan(ScheduleTestCommand::class, ['--name' => 'callback']) |
| 55 | + ->assertSuccessful() |
| 56 | + ->expectsOutput(sprintf('[%s] Running scheduled command: callback', $this->timestamp)); |
| 57 | + } |
| 58 | + |
| 59 | + public function testRunUsingChoices() |
| 60 | + { |
| 61 | + $this->schedule->command(BarCommandStub::class)->name('bar-command'); |
| 62 | + $this->schedule->job(BarJobStub::class); |
| 63 | + $this->schedule->call(fn () => true)->name('callback'); |
| 64 | + |
| 65 | + $this->artisan(ScheduleTestCommand::class) |
| 66 | + ->assertSuccessful() |
| 67 | + ->expectsChoice( |
| 68 | + 'Which command would you like to run?', |
| 69 | + 'callback', |
| 70 | + [Application::formatCommandString('bar:command'), BarJobStub::class, 'callback'], |
| 71 | + true |
| 72 | + ) |
| 73 | + ->expectsOutput(sprintf('[%s] Running scheduled command: callback', $this->timestamp)); |
| 74 | + } |
| 75 | + |
| 76 | + protected function tearDown(): void |
| 77 | + { |
| 78 | + parent::tearDown(); |
| 79 | + |
| 80 | + Carbon::setTestNow(null); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +class BarCommandStub extends Command |
| 85 | +{ |
| 86 | + protected $signature = 'bar:command'; |
| 87 | + |
| 88 | + protected $description = 'This is the description of the command.'; |
| 89 | +} |
| 90 | + |
| 91 | +class BarJobStub |
| 92 | +{ |
| 93 | + public function __invoke() |
| 94 | + { |
| 95 | + // .. |
| 96 | + } |
| 97 | +} |
0 commit comments