From 805af7774fbd5213927cbd08947f72332714ddc8 Mon Sep 17 00:00:00 2001 From: yousef kadah Date: Tue, 3 Jun 2025 02:25:48 +0300 Subject: [PATCH 1/2] Add CommandAttributeTest to validate command attributes and descriptions --- src/Illuminate/Console/Command.php | 5 ++- tests/Console/CommandAttributeTest.php | 45 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/Console/CommandAttributeTest.php diff --git a/src/Illuminate/Console/Command.php b/src/Illuminate/Console/Command.php index 5ef2132f8233..7b51a41da5f2 100755 --- a/src/Illuminate/Console/Command.php +++ b/src/Illuminate/Console/Command.php @@ -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; @@ -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)) { $this->setDescription((string) $this->description); } diff --git a/tests/Console/CommandAttributeTest.php b/tests/Console/CommandAttributeTest.php new file mode 100644 index 000000000000..c7a61d6d8623 --- /dev/null +++ b/tests/Console/CommandAttributeTest.php @@ -0,0 +1,45 @@ +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; + } +} From 229bf992e410f1cf1b05f4eded490bdbbb0d59f2 Mon Sep 17 00:00:00 2001 From: yousef kadah Date: Tue, 3 Jun 2025 02:28:32 +0300 Subject: [PATCH 2/2] Add ClosureCommandTest to validate ClosureCommand functionality --- .../Foundation/Console/ClosureCommand.php | 7 ++++++ .../Foundation/Console/ClosureCommandTest.php | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/Integration/Foundation/Console/ClosureCommandTest.php diff --git a/src/Illuminate/Foundation/Console/ClosureCommand.php b/src/Illuminate/Foundation/Console/ClosureCommand.php index a9817d4df22d..f781ba44d2ea 100644 --- a/src/Illuminate/Foundation/Console/ClosureCommand.php +++ b/src/Illuminate/Foundation/Console/ClosureCommand.php @@ -25,6 +25,13 @@ class ClosureCommand extends Command */ protected $callback; + /** + * The console command description. + * + * @var string + */ + protected $description = ''; + /** * Create a new command instance. * diff --git a/tests/Integration/Foundation/Console/ClosureCommandTest.php b/tests/Integration/Foundation/Console/ClosureCommandTest.php new file mode 100644 index 000000000000..c57243193b63 --- /dev/null +++ b/tests/Integration/Foundation/Console/ClosureCommandTest.php @@ -0,0 +1,23 @@ +comment('We must ship. - Taylor Otwell'); + })->purpose('Display an inspiring quote'); + } + + public function testItCanRunClosureCommand() + { + $this->artisan('inspire')->expectsOutput('We must ship. - Taylor Otwell'); + } +}