Skip to content

Commit

Permalink
Merge pull request #261 from getsentry/fix-console-listener
Browse files Browse the repository at this point in the history
Fix console listener nullable command name
  • Loading branch information
Jean85 committed Oct 16, 2019
2 parents 9f7ec3d + 146208f commit 58050af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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

0 comments on commit 58050af

Please sign in to comment.