Skip to content

Commit 06dffde

Browse files
authored
Rename the ConsoleCommandListener class to ConsoleListener (getsentry#429)
1 parent ed64c02 commit 06dffde

File tree

13 files changed

+241
-162
lines changed

13 files changed

+241
-162
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Added missing `capture-soft-fails` config schema option (#417)
6+
- Deprecate the `Sentry\SentryBundle\EventListener\ConsoleCommandListener` class in favor of its parent class `Sentry\SentryBundle\EventListener\ConsoleListener` (#429)
67

78
## 4.0.0 (2021-01-19)
89

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ parameters:
3535
count: 1
3636
path: tests/End2End/End2EndTest.php
3737

38-
-
39-
message: "#^Comparison operation \"\\<\" between 50201 and 40300 is always false\\.$#"
40-
count: 1
41-
path: tests/End2End/End2EndTest.php
42-
4338
-
4439
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\Tests\\\\EventListener\\\\ErrorListenerTest\\:\\:testHandleExceptionEvent\\(\\) has invalid typehint type Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
4540
count: 1

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
beStrictAboutOutputDuringTests="true"
88
>
99
<php>
10-
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
10+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
1111
</php>
1212

1313
<testsuites>

psalm-baseline.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="4.4.1@9fd7a7d885b3a216cff8dec9d8c21a132f275224">
3+
<file src="src/EventListener/ConsoleCommandListener.php">
4+
<InvalidExtendClass occurrences="1">
5+
<code>ConsoleListener</code>
6+
</InvalidExtendClass>
7+
<MethodSignatureMismatch occurrences="1">
8+
<code>public function __construct(HubInterface $hub)</code>
9+
</MethodSignatureMismatch>
10+
</file>
11+
</files>

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<psalm
33
errorLevel="4"
4-
resolveFromConfigFile="true"
54
memoizeMethodCallResults="true"
5+
errorBaseline="psalm-baseline.xml"
66
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
77
xmlns="https://getpsalm.org/schema/config"
88
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"

src/EventListener/ConsoleCommandListener.php

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,16 @@
55
namespace Sentry\SentryBundle\EventListener;
66

77
use Sentry\State\HubInterface;
8-
use Sentry\State\Scope;
9-
use Symfony\Component\Console\Event\ConsoleCommandEvent;
10-
use Symfony\Component\Console\Event\ConsoleErrorEvent;
11-
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
128

139
/**
14-
* This listener handles all errors thrown while running a console command and
15-
* logs them to Sentry.
10+
* @deprecated since version 4.1, to be removed in 5.0
1611
*/
17-
final class ConsoleCommandListener
12+
final class ConsoleCommandListener extends ConsoleListener
1813
{
19-
/**
20-
* @var HubInterface The current hub
21-
*/
22-
private $hub;
23-
24-
/**
25-
* Constructor.
26-
*
27-
* @param HubInterface $hub The current hub
28-
*/
2914
public function __construct(HubInterface $hub)
3015
{
31-
$this->hub = $hub;
32-
}
33-
34-
/**
35-
* Handles the execution of a console command by pushing a new {@see Scope}.
36-
*
37-
* @param ConsoleCommandEvent $event The event
38-
*/
39-
public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
40-
{
41-
$scope = $this->hub->pushScope();
42-
$command = $event->getCommand();
43-
44-
if (null !== $command && null !== $command->getName()) {
45-
$scope->setTag('console.command', $command->getName());
46-
}
47-
}
48-
49-
/**
50-
* Handles the termination of a console command by popping the {@see Scope}.
51-
*
52-
* @param ConsoleTerminateEvent $event The event
53-
*/
54-
public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
55-
{
56-
$this->hub->popScope();
57-
}
58-
59-
/**
60-
* Handles an error that happened while running a console command.
61-
*
62-
* @param ConsoleErrorEvent $event The event
63-
*/
64-
public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void
65-
{
66-
$this->hub->configureScope(function (Scope $scope) use ($event): void {
67-
$scope->setTag('console.command.exit_code', (string) $event->getExitCode());
16+
parent::__construct($hub);
6817

69-
$this->hub->captureException($event->getError());
70-
});
18+
@trigger_error(sprintf('The "%s" class is deprecated since version 4.1 and will be removed in 5.0. Use "%s" instead.', self::class, ConsoleListener::class), \E_USER_DEPRECATED);
7119
}
7220
}

src/EventListener/ConsoleListener.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\EventListener;
6+
7+
use Sentry\State\HubInterface;
8+
use Sentry\State\Scope;
9+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
10+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
11+
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
12+
13+
/**
14+
* This listener handles all errors thrown while running a console command and
15+
* logs them to Sentry.
16+
*
17+
* @final since version 4.1
18+
*/
19+
class ConsoleListener
20+
{
21+
/**
22+
* @var HubInterface The current hub
23+
*/
24+
private $hub;
25+
26+
/**
27+
* Constructor.
28+
*
29+
* @param HubInterface $hub The current hub
30+
*/
31+
public function __construct(HubInterface $hub)
32+
{
33+
$this->hub = $hub;
34+
}
35+
36+
/**
37+
* Handles the execution of a console command by pushing a new {@see Scope}.
38+
*
39+
* @param ConsoleCommandEvent $event The event
40+
*/
41+
public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
42+
{
43+
$scope = $this->hub->pushScope();
44+
$command = $event->getCommand();
45+
46+
if (null !== $command && null !== $command->getName()) {
47+
$scope->setTag('console.command', $command->getName());
48+
}
49+
}
50+
51+
/**
52+
* Handles the termination of a console command by popping the {@see Scope}.
53+
*
54+
* @param ConsoleTerminateEvent $event The event
55+
*/
56+
public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
57+
{
58+
$this->hub->popScope();
59+
}
60+
61+
/**
62+
* Handles an error that happened while running a console command.
63+
*
64+
* @param ConsoleErrorEvent $event The event
65+
*/
66+
public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void
67+
{
68+
$this->hub->configureScope(function (Scope $scope) use ($event): void {
69+
$scope->setTag('console.command.exit_code', (string) $event->getExitCode());
70+
71+
$this->hub->captureException($event->getError());
72+
});
73+
}
74+
}

src/Resources/config/services.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
</call>
2424
</service>
2525

26-
<service id="Sentry\SentryBundle\EventListener\ConsoleCommandListener" class="Sentry\SentryBundle\EventListener\ConsoleCommandListener">
26+
<service id="Sentry\SentryBundle\EventListener\ConsoleCommandListener" alias="Sentry\SentryBundle\EventListener\ConsoleListener" />
27+
28+
<service id="Sentry\SentryBundle\EventListener\ConsoleListener" class="Sentry\SentryBundle\EventListener\ConsoleListener">
2729
<argument type="service" id="Sentry\State\HubInterface" />
2830

2931
<tag name="kernel.event_listener" event="console.command" method="handleConsoleCommandEvent" priority="128" />

tests/DependencyInjection/SentryExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Sentry\Integration\IgnoreErrorsIntegration;
1111
use Sentry\Options;
1212
use Sentry\SentryBundle\DependencyInjection\SentryExtension;
13-
use Sentry\SentryBundle\EventListener\ConsoleCommandListener;
13+
use Sentry\SentryBundle\EventListener\ConsoleListener;
1414
use Sentry\SentryBundle\EventListener\ErrorListener;
1515
use Sentry\SentryBundle\EventListener\MessengerListener;
1616
use Sentry\SentryBundle\EventListener\RequestListener;
@@ -57,9 +57,9 @@ public function testErrorListenerIsRemovedWhenDisabled(): void
5757
public function testConsoleCommandListener(): void
5858
{
5959
$container = $this->createContainerFromFixture('full');
60-
$definition = $container->getDefinition(ConsoleCommandListener::class);
60+
$definition = $container->findDefinition(ConsoleListener::class);
6161

62-
$this->assertSame(ConsoleCommandListener::class, $definition->getClass());
62+
$this->assertSame(ConsoleListener::class, $definition->getClass());
6363
$this->assertSame([
6464
'kernel.event_listener' => [
6565
[

tests/End2End/End2EndTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ private function assertLastEventIdIsNull(KernelBrowser $client): void
255255

256256
private function skipIfMessengerIsMissing(): void
257257
{
258-
if (!interface_exists(MessageBusInterface::class) || Kernel::VERSION_ID < 40300) {
258+
if (!interface_exists(MessageBusInterface::class)) {
259259
$this->markTestSkipped('Messenger missing');
260260
}
261261
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\EventListener;
6+
7+
use PHPUnit\Framework\MockObject\MockObject;
8+
use PHPUnit\Framework\TestCase;
9+
use Sentry\Event;
10+
use Sentry\SentryBundle\EventListener\ConsoleListener;
11+
use Sentry\State\HubInterface;
12+
use Sentry\State\Scope;
13+
use Symfony\Component\Console\Command\Command;
14+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
15+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
16+
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
20+
abstract class AbstractConsoleListenerTest extends TestCase
21+
{
22+
/**
23+
* @var MockObject&HubInterface
24+
*/
25+
private $hub;
26+
27+
/**
28+
* @var ConsoleListener
29+
*/
30+
private $listener;
31+
32+
protected function setUp(): void
33+
{
34+
$listenerClass = static::getListenerClass();
35+
36+
$this->hub = $this->createMock(HubInterface::class);
37+
$this->listener = new $listenerClass($this->hub);
38+
}
39+
40+
/**
41+
* @dataProvider handleConsoleCommmandEventDataProvider
42+
*
43+
* @param array<string, string> $expectedTags
44+
*/
45+
public function testHandleConsoleCommandEvent(ConsoleCommandEvent $consoleEvent, array $expectedTags): void
46+
{
47+
$scope = new Scope();
48+
49+
$this->hub->expects($this->once())
50+
->method('pushScope')
51+
->willReturn($scope);
52+
53+
$this->listener->handleConsoleCommandEvent($consoleEvent);
54+
55+
$event = $scope->applyToEvent(Event::createEvent());
56+
57+
$this->assertSame($expectedTags, $event->getTags());
58+
}
59+
60+
/**
61+
* @return \Generator<mixed>
62+
*/
63+
public function handleConsoleCommmandEventDataProvider(): \Generator
64+
{
65+
yield [
66+
new ConsoleCommandEvent(null, $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
67+
[],
68+
];
69+
70+
yield [
71+
new ConsoleCommandEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
72+
[],
73+
];
74+
75+
yield [
76+
new ConsoleCommandEvent(new Command('foo:bar'), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
77+
['console.command' => 'foo:bar'],
78+
];
79+
}
80+
81+
public function testHandleConsoleTerminateEvent(): void
82+
{
83+
$this->hub->expects($this->once())
84+
->method('popScope');
85+
86+
$this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), 0));
87+
}
88+
89+
public function testHandleConsoleErrorEvent(): void
90+
{
91+
$scope = new Scope();
92+
$consoleEvent = new ConsoleErrorEvent($this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), new \Exception());
93+
94+
$this->hub->expects($this->once())
95+
->method('configureScope')
96+
->willReturnCallback(static function (callable $callback) use ($scope): void {
97+
$callback($scope);
98+
});
99+
100+
$this->hub->expects($this->once())
101+
->method('captureException')
102+
->with($consoleEvent->getError());
103+
104+
$this->listener->handleConsoleErrorEvent($consoleEvent);
105+
106+
$event = $scope->applyToEvent(Event::createEvent());
107+
108+
$this->assertSame(['console.command.exit_code' => '1'], $event->getTags());
109+
}
110+
111+
/**
112+
* @return class-string<ConsoleListener>
113+
*/
114+
abstract protected static function getListenerClass(): string;
115+
}

0 commit comments

Comments
 (0)