From 1f5f0b5ffcff3f260391bf926c7427278c09fedb Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Wed, 16 Oct 2019 11:34:53 +0200 Subject: [PATCH 1/3] Fix console listener nullable command name --- src/EventListener/ConsoleListener.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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'); }); } } From b6b00f8548f23310996c8481fd1672314153b1db Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Wed, 16 Oct 2019 11:38:13 +0200 Subject: [PATCH 2/3] Add test --- test/EventListener/ConsoleListenerTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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(); From 146208f38b32db0edf2077b80261d011dbc4c457 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Wed, 16 Oct 2019 11:39:31 +0200 Subject: [PATCH 3/3] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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)