|
14 | 14 | namespace Guanguans\LaravelExceptionNotify\Commands;
|
15 | 15 |
|
16 | 16 | use Guanguans\LaravelExceptionNotify\DefaultNotifyClientExtender;
|
| 17 | +use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager; |
17 | 18 | use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
|
18 | 19 | use Illuminate\Console\Command;
|
| 20 | +use Symfony\Component\Console\Input\InputInterface; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
19 | 22 |
|
20 | 23 | class TestCommand extends Command
|
21 | 24 | {
|
22 | 25 | protected $signature = 'exception-notify:test {--c|channels=* : Specify channels to test}';
|
23 | 26 | protected $description = 'Test for exception-notify';
|
24 | 27 |
|
25 |
| - public function handle(): void |
| 28 | + public function handle(ExceptionNotifyManager $exceptionNotifyManager) |
26 | 29 | {
|
27 |
| - if ($channels = $this->option('channels')) { |
28 |
| - config()->set('exception-notify.defaults', $channels); |
| 30 | + $this->output->info('Test for exception-notify start.'); |
| 31 | + |
| 32 | + if (!config('exception-notify.enabled')) { |
| 33 | + $this->output->warning('The exception-notify is not enabled. Please enable it first.'); |
| 34 | + |
| 35 | + return self::INVALID; |
29 | 36 | }
|
30 | 37 |
|
31 |
| - collect(config('exception-notify.channels')) |
32 |
| - ->only($defaults = config('exception-notify.defaults')) |
33 |
| - ->each(static function (array $config, string $name): void { |
34 |
| - if ('notify' === $config['driver']) { |
35 |
| - config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class); |
36 |
| - } |
37 |
| - }); |
| 38 | + if (empty(config('exception-notify.defaults'))) { |
| 39 | + $this->output->warning('The exception-notify default channels is empty. Please configure it first.'); |
38 | 40 |
|
39 |
| - $this->output->note('Test for exception-notify start.'); |
40 |
| - $this->output->section('Current default channels:'); |
41 |
| - $this->output->listing($defaults); |
| 41 | + return self::INVALID; |
| 42 | + } |
42 | 43 |
|
43 |
| - try { |
| 44 | + $runtimeException = new RuntimeException('Test for exception-notify.'); |
| 45 | + |
| 46 | + if (!$exceptionNotifyManager->shouldReport($runtimeException)) { |
44 | 47 | $this->output->warning(sprintf(
|
45 | 48 | <<<'warning'
|
46 |
| - Let's throw an exception to trigger exception notification monitoring. |
47 |
| - Please check whether your channels(%s) received the exception notification reports. |
48 |
| - If not, please find reason in the default log. |
| 49 | + The exception [%s] should not be reported. |
| 50 | + Please check the configuration. |
49 | 51 | warning,
|
50 |
| - implode('、', $defaults) |
| 52 | + \get_class($runtimeException) |
51 | 53 | ));
|
52 | 54 |
|
53 |
| - throw new RuntimeException('Test for exception-notify.'); |
| 55 | + return self::INVALID; |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + throw $runtimeException; |
54 | 60 | } finally {
|
55 |
| - $this->output->note('Test for exception-notify done.'); |
| 61 | + $this->laravel->terminating(function (): void { |
| 62 | + $this->output->section('Current default channels:'); |
| 63 | + $this->output->listing($defaults = config('exception-notify.defaults')); |
| 64 | + $this->output->warning(sprintf( |
| 65 | + <<<'warning' |
| 66 | + An exception has been thrown to trigger the exception notification monitor. |
| 67 | + Please check whether your channels(%s) received the exception notification reports. |
| 68 | + If not, please find reason in the default log. |
| 69 | + warning, |
| 70 | + implode('、', $defaults) |
| 71 | + )); |
| 72 | + $this->output->success('Test for exception-notify done.'); |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @noinspection MethodVisibilityInspection |
| 79 | + */ |
| 80 | + protected function initialize(InputInterface $input, OutputInterface $output): void |
| 81 | + { |
| 82 | + if ($channels = $this->option('channels')) { |
| 83 | + config()->set('exception-notify.defaults', $channels); |
56 | 84 | }
|
| 85 | + |
| 86 | + collect(config('exception-notify.channels')) |
| 87 | + ->only(config('exception-notify.defaults')) |
| 88 | + ->each(static function (array $config, string $name): void { |
| 89 | + if ('notify' === $config['driver']) { |
| 90 | + config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class); |
| 91 | + } |
| 92 | + }); |
57 | 93 | }
|
58 | 94 | }
|
0 commit comments