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