Skip to content

Commit e47be73

Browse files
authored
[9.x] Add tests for schedule:test command (#41698)
* Use Carbon for date formatting. * Add test coverage of schedule:test command. * Move teardown. * Update method name.
1 parent 69bb480 commit e47be73

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/Illuminate/Console/Scheduling/ScheduleTestCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Application;
66
use Illuminate\Console\Command;
7+
use Illuminate\Support\Carbon;
78

89
class ScheduleTestCommand extends Command
910
{
@@ -70,7 +71,7 @@ public function handle(Schedule $schedule)
7071

7172
$event = $commands[$index];
7273

73-
$this->line('<info>['.date('c').'] Running scheduled command:</info> '.$event->getSummaryForDisplay());
74+
$this->line('<info>['.Carbon::now()->format('c').'] Running scheduled command:</info> '.$event->getSummaryForDisplay());
7475

7576
$event->run($this->laravel);
7677
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)