Skip to content

Commit 5ab0a15

Browse files
committed
refactor(commands): Replace output with components in TestCommand
- Update warning messages to use components instead of output - Change runtime exception message for clarity - Improve success message to indicate task completion These changes enhance the code's consistency and improve the user experience by standardizing the way messages are presented.
1 parent 64eebc4 commit 5ab0a15

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/Commands/TestCommand.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class TestCommand extends Command
3232
}
3333

3434
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
35-
protected $signature = 'exception-notify:test {--c|channel= : The channel of report exception}';
35+
protected $signature = <<<'SIGNATURE'
36+
exception-notify:test
37+
{--c|channel= : The channel of report exception}
38+
{--queue-connection= : The queue connection of report exception}
39+
SIGNATURE;
3640

3741
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
3842
protected $description = 'Testing';
@@ -43,17 +47,20 @@ class TestCommand extends Command
4347
public function handle(ExceptionNotifyManager $exceptionNotifyManager): int
4448
{
4549
if (!config($configurationKey = 'exception-notify.enabled')) {
46-
$this->output->warning("The value of this configuration [$configurationKey] is false, please configure it to true.");
50+
$this->components->warn(\sprintf(
51+
'The value of this configuration [%s] is false, please configure it to true.',
52+
$this->warned($configurationKey)
53+
));
4754

4855
return self::INVALID;
4956
}
5057

51-
$runtimeException = new RuntimeException('Testing');
58+
$runtimeException = new RuntimeException('This is a test.');
5259

5360
if (!$exceptionNotifyManager->shouldReport($runtimeException)) {
54-
$this->output->warning(\sprintf(
61+
$this->components->warn(\sprintf(
5562
'The exception [%s] should not be reported, please check the configuration.',
56-
$runtimeException::class
63+
$this->warned($runtimeException::class),
5764
));
5865

5966
return self::INVALID;
@@ -63,16 +70,19 @@ public function handle(ExceptionNotifyManager $exceptionNotifyManager): int
6370
throw $runtimeException;
6471
} finally {
6572
$this->laravel->terminating(function () use ($runtimeException): void {
66-
$this->output->warning(\sprintf(
67-
<<<'warning'
68-
The exception [%s] has been thrown.
69-
Please check whether the channel [%s] has received an exception report.
70-
If not, please find the reason in the default log channel.
71-
warning,
72-
$runtimeException::class,
73-
config('exception-notify.default'),
73+
$this->components->warn(\sprintf(
74+
'The exception [%s] has been thrown.',
75+
$this->warned($runtimeException::class)
76+
));
77+
$this->components->warn(\sprintf(
78+
'Please check whether the exception-notify channel [%s] has received an exception report.',
79+
$this->warned(config('exception-notify.default'))
80+
));
81+
$this->components->warn(\sprintf(
82+
'If not, please find the reason in the default log channel [%s].',
83+
$this->warned(config('logging.default'))
7484
));
75-
$this->output->success('Testing done.');
85+
$this->components->info('Testing done.');
7686
});
7787
}
7888
}
@@ -86,6 +96,7 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
8696
$this->configureableInitialize($input, $output);
8797

8898
$channel = $this->option('channel') and config()->set('exception-notify.default', $channel);
99+
$connection = $this->option('queue-connection') and config()->set('queue.default', $connection);
89100

90101
collect(config('exception-notify.channels'))->each(function (array $configuration, string $name): void {
91102
if ('notify' === ($configuration['driver'] ?? $name)) {
@@ -107,4 +118,9 @@ function (Client $client): Client {
107118
}
108119
});
109120
}
121+
122+
private function warned(string $string): string
123+
{
124+
return "<fg=yellow;options=bold>$string</>";
125+
}
110126
}

tests/Commands/TestCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
it('will throws RuntimeException', function (): void {
4646
artisan(TestCommand::class);
47-
})->group(__DIR__, __FILE__)->throws(RuntimeException::class, 'Testing');
47+
})->group(__DIR__, __FILE__)->throws(RuntimeException::class, 'This is a test.');
4848

4949
it('will catch RuntimeException and can report it', function (): void {
5050
try {

0 commit comments

Comments
 (0)