Skip to content

Commit 58050af

Browse files
authored
Merge pull request #261 from getsentry/fix-console-listener
Fix console listener nullable command name
2 parents 9f7ec3d + 146208f commit 58050af

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
8+
- Fix handling of command with no name on `ConsoleListener` (#261)
89

910
## 3.2.0 (2019-10-04)
1011

src/EventListener/ConsoleListener.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
3333
{
3434
$command = $event->getCommand();
3535

36+
$commandName = null;
37+
if ($command) {
38+
$commandName = $command->getName();
39+
}
40+
3641
SentryBundle::getCurrentHub()
37-
->configureScope(function (Scope $scope) use ($command): void {
38-
$scope->setTag('command', $command ? $command->getName() : 'N/A');
42+
->configureScope(static function (Scope $scope) use ($commandName): void {
43+
$scope->setTag('command', $commandName ?? 'N/A');
3944
});
4045
}
4146
}

test/EventListener/ConsoleListenerTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testOnConsoleCommandAddsCommandName(): void
5151
$this->assertSame(['command' => 'sf:command:name'], $this->getTagsContext($this->currentScope));
5252
}
5353

54-
public function testOnConsoleCommandAddsPlaceholderCommandName(): void
54+
public function testOnConsoleCommandWithNoCommandAddsPlaceholder(): void
5555
{
5656
$event = $this->prophesize(ConsoleCommandEvent::class);
5757
$event->getCommand()
@@ -64,6 +64,23 @@ public function testOnConsoleCommandAddsPlaceholderCommandName(): void
6464
$this->assertSame(['command' => 'N/A'], $this->getTagsContext($this->currentScope));
6565
}
6666

67+
public function testOnConsoleCommandWithNoCommandNameAddsPlaceholder(): void
68+
{
69+
$command = $this->prophesize(Command::class);
70+
$command->getName()
71+
->willReturn(null);
72+
73+
$event = $this->prophesize(ConsoleCommandEvent::class);
74+
$event->getCommand()
75+
->willReturn($command->reveal());
76+
77+
$listener = new ConsoleListener($this->currentHub->reveal());
78+
79+
$listener->onConsoleCommand($event->reveal());
80+
81+
$this->assertSame(['command' => 'N/A'], $this->getTagsContext($this->currentScope));
82+
}
83+
6784
private function getTagsContext(Scope $scope): array
6885
{
6986
$event = new Event();

0 commit comments

Comments
 (0)