diff --git a/CHANGELOG.md b/CHANGELOG.md index d3c0ec1e..b902aa75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/EventListener/ConsoleListener.php b/src/EventListener/ConsoleListener.php index 4d47dc8c..f7e1eca3 100644 --- a/src/EventListener/ConsoleListener.php +++ b/src/EventListener/ConsoleListener.php @@ -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'); }); } } diff --git a/test/EventListener/ConsoleListenerTest.php b/test/EventListener/ConsoleListenerTest.php index 2c7b361a..d6c174f6 100644 --- a/test/EventListener/ConsoleListenerTest.php +++ b/test/EventListener/ConsoleListenerTest.php @@ -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() @@ -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();