Skip to content

Commit 191cd91

Browse files
[12.x] Add --json option to EventListCommand (#55207)
* Add --json option to EventListCommand with tests * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent e9f18fc commit 191cd91

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

src/Illuminate/Foundation/Console/EventListCommand.php

+41-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class EventListCommand extends Command
1818
*
1919
* @var string
2020
*/
21-
protected $signature = 'event:list {--event= : Filter the events by name}';
21+
protected $signature = 'event:list
22+
{--event= : Filter the events by name}
23+
{--json : Output the events and listeners as JSON}';
2224

2325
/**
2426
* The console command description.
@@ -44,11 +46,48 @@ public function handle()
4446
$events = $this->getEvents()->sortKeys();
4547

4648
if ($events->isEmpty()) {
47-
$this->components->info("Your application doesn't have any events matching the given criteria.");
49+
if ($this->option('json')) {
50+
$this->output->writeln('[]');
51+
} else {
52+
$this->components->info("Your application doesn't have any events matching the given criteria.");
53+
}
4854

4955
return;
5056
}
5157

58+
if ($this->option('json')) {
59+
$this->displayJson($events);
60+
} else {
61+
$this->displayForCli($events);
62+
}
63+
}
64+
65+
/**
66+
* Display events and their listeners in JSON.
67+
*
68+
* @param \Illuminate\Support\Collection $events
69+
* @return void
70+
*/
71+
protected function displayJson(Collection $events)
72+
{
73+
$data = $events->map(function ($listeners, $event) {
74+
return [
75+
'event' => strip_tags($this->appendEventInterfaces($event)),
76+
'listeners' => collect($listeners)->map(fn ($listener) => strip_tags($listener))->values()->all(),
77+
];
78+
})->values();
79+
80+
$this->output->writeln($data->toJson());
81+
}
82+
83+
/**
84+
* Display the events and their listeners for the CLI.
85+
*
86+
* @param \Illuminate\Support\Collection $events
87+
* @return void
88+
*/
89+
protected function displayForCli(Collection $events)
90+
{
5291
$this->newLine();
5392

5493
$events->each(function ($listeners, $event) {

tests/Integration/Console/Events/EventListCommandTest.php

+49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Queue\ShouldQueue;
77
use Illuminate\Events\Dispatcher;
88
use Illuminate\Foundation\Console\EventListCommand;
9+
use Illuminate\Support\Facades\Artisan;
910
use Orchestra\Testbench\TestCase;
1011

1112
class EventListCommandTest extends TestCase
@@ -58,6 +59,54 @@ public function testDisplayFilteredEvent()
5859
->expectsOutputToContain('ExampleEvent');
5960
}
6061

62+
public function testDisplayEmptyListAsJson()
63+
{
64+
$this->withoutMockingConsoleOutput()->artisan(EventListCommand::class, ['--json' => true]);
65+
$output = Artisan::output();
66+
67+
$this->assertJson($output);
68+
$this->assertJsonStringEqualsJsonString('[]', $output);
69+
}
70+
71+
public function testDisplayEventsAsJson()
72+
{
73+
$this->dispatcher->subscribe(ExampleSubscriber::class);
74+
$this->dispatcher->listen(ExampleEvent::class, ExampleListener::class);
75+
$this->dispatcher->listen(ExampleEvent::class, ExampleQueueListener::class);
76+
$this->dispatcher->listen(ExampleBroadcastEvent::class, ExampleBroadcastListener::class);
77+
$this->dispatcher->listen(ExampleEvent::class, fn () => '');
78+
$closureLineNumber = __LINE__ - 1;
79+
$unixFilePath = str_replace('\\', '/', __FILE__);
80+
81+
$this->withoutMockingConsoleOutput()->artisan(EventListCommand::class, ['--json' => true]);
82+
$output = Artisan::output();
83+
84+
$this->assertJson($output);
85+
$this->assertStringContainsString('ExampleSubscriberEventName', $output);
86+
$this->assertStringContainsString(json_encode('Illuminate\Tests\Integration\Console\Events\ExampleSubscriber@a'), $output);
87+
$this->assertStringContainsString(json_encode('Illuminate\Tests\Integration\Console\Events\ExampleBroadcastEvent (ShouldBroadcast)'), $output);
88+
$this->assertStringContainsString(json_encode('Illuminate\Tests\Integration\Console\Events\ExampleBroadcastListener'), $output);
89+
$this->assertStringContainsString(json_encode('Illuminate\Tests\Integration\Console\Events\ExampleEvent'), $output);
90+
$this->assertStringContainsString(json_encode('Closure at: '.$unixFilePath.':'.$closureLineNumber), $output);
91+
}
92+
93+
public function testDisplayFilteredEventAsJson()
94+
{
95+
$this->dispatcher->subscribe(ExampleSubscriber::class);
96+
$this->dispatcher->listen(ExampleEvent::class, ExampleListener::class);
97+
98+
$this->withoutMockingConsoleOutput()->artisan(EventListCommand::class, [
99+
'--event' => 'ExampleEvent',
100+
'--json' => true,
101+
]);
102+
$output = Artisan::output();
103+
104+
$this->assertJson($output);
105+
$this->assertStringContainsString('ExampleEvent', $output);
106+
$this->assertStringContainsString('ExampleListener', $output);
107+
$this->assertStringNotContainsString('ExampleSubscriberEventName', $output);
108+
}
109+
61110
protected function tearDown(): void
62111
{
63112
parent::tearDown();

0 commit comments

Comments
 (0)