Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\View\Components\Factory;
use Illuminate\Contracts\Console\Isolatable;
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -101,9 +102,7 @@ public function __construct()
// Once we have constructed the command, we'll set the description and other
// related properties of the command. If a signature wasn't used to build
// the command we'll set the arguments and the options on this command.
if (! isset($this->description)) {
$this->setDescription((string) static::getDefaultDescription());
} else {
if (isset($this->description)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is best to be pushed to master branch for Laravel 13

$this->setDescription((string) $this->description);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Illuminate/Foundation/Console/ClosureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class ClosureCommand extends Command
*/
protected $callback;

/**
* The console command description.
*
* @var string
*/
protected $description = '';

/**
* Create a new command instance.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Console/CommandAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Illuminate\Tests\Console;

use Illuminate\Console\Application;
use Illuminate\Console\Command;
use Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Attribute\AsCommand;

class CommandAttributeTest extends TestCase
{
public function testCommandWithAttribute()
{
$command = new TestCommandWithAttribute();
$this->assertEquals('test:command', $command->getName());
$this->assertEquals('This is a test command', $command->getDescription());
}

public function testCommandWithoutAttribute()
{
$command = new TestCommandWithoutAttribute();
$this->assertEquals('test:no-attribute', $command->getName());
$this->assertEquals('', $command->getDescription());
}
}

#[AsCommand(name: 'test:command', description: 'This is a test command')]
class TestCommandWithAttribute extends Command
{
public function handle()
{
return 0;
}
}

class TestCommandWithoutAttribute extends Command
{
protected $name = 'test:no-attribute';

public function handle()
{
return 0;
}
}
23 changes: 23 additions & 0 deletions tests/Integration/Foundation/Console/ClosureCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Console;

use Illuminate\Support\Facades\Artisan;
use Orchestra\Testbench\TestCase;

class ClosureCommandTest extends TestCase
{
/** {@inheritDoc} */
#[\Override]
protected function defineEnvironment($app)
{
Artisan::command('inspire', function () {
$this->comment('We must ship. - Taylor Otwell');
})->purpose('Display an inspiring quote');
}

public function testItCanRunClosureCommand()
{
$this->artisan('inspire')->expectsOutput('We must ship. - Taylor Otwell');
}
}