Skip to content

Fix console listener nullable command name #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

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

## 3.2.0 (2019-10-04)

Expand Down
9 changes: 7 additions & 2 deletions src/EventListener/ConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
{
$command = $event->getCommand();

$commandName = null;
if ($command) {
$commandName = $command->getName();
}

SentryBundle::getCurrentHub()
->configureScope(function (Scope $scope) use ($command): void {
$scope->setTag('command', $command ? $command->getName() : 'N/A');
->configureScope(static function (Scope $scope) use ($commandName): void {
$scope->setTag('command', $commandName ?? 'N/A');
});
}
}
19 changes: 18 additions & 1 deletion test/EventListener/ConsoleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testOnConsoleCommandAddsCommandName(): void
$this->assertSame(['command' => 'sf:command:name'], $this->getTagsContext($this->currentScope));
}

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

public function testOnConsoleCommandWithNoCommandNameAddsPlaceholder(): void
{
$command = $this->prophesize(Command::class);
$command->getName()
->willReturn(null);

$event = $this->prophesize(ConsoleCommandEvent::class);
$event->getCommand()
->willReturn($command->reveal());

$listener = new ConsoleListener($this->currentHub->reveal());

$listener->onConsoleCommand($event->reveal());

$this->assertSame(['command' => 'N/A'], $this->getTagsContext($this->currentScope));
}

private function getTagsContext(Scope $scope): array
{
$event = new Event();
Expand Down