Skip to content

Commit

Permalink
Fix broken build
Browse files Browse the repository at this point in the history
  • Loading branch information
ste93cry committed Feb 1, 2021
1 parent 3d0d943 commit 79265d9
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 102 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ parameters:
count: 1
path: tests/End2End/End2EndTest.php

-
message: "#^Comparison operation \"\\<\" between 50201 and 40300 is always false\\.$#"
count: 1
path: tests/End2End/End2EndTest.php

-
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\Tests\\\\EventListener\\\\ErrorListenerTest\\:\\:testHandleExceptionEvent\\(\\) has invalid typehint type Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
count: 1
Expand Down
11 changes: 11 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.4.1@9fd7a7d885b3a216cff8dec9d8c21a132f275224">
<file src="src/EventListener/ConsoleCommandListener.php">
<InvalidExtendClass occurrences="1">
<code>ConsoleListener</code>
</InvalidExtendClass>
<MethodSignatureMismatch occurrences="1">
<code>public function __construct(HubInterface $hub)</code>
</MethodSignatureMismatch>
</file>
</files>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
memoizeMethodCallResults="true"
errorBaseline="psalm-baseline.xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
8 changes: 7 additions & 1 deletion src/EventListener/ConsoleCommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

namespace Sentry\SentryBundle\EventListener;

@trigger_error(sprintf('The "%s" class is deprecated since version 4.1 and will be removed in 5.0. Use "%s" instead.', ConsoleCommandListener::class, ConsoleListener::class), \E_USER_DEPRECATED);
use Sentry\State\HubInterface;

/**
* @deprecated since version 4.1, to be removed in 5.0
*/
final class ConsoleCommandListener extends ConsoleListener
{
public function __construct(HubInterface $hub)
{
parent::__construct($hub);

@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);
}
}
2 changes: 1 addition & 1 deletion tests/End2End/End2EndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private function assertLastEventIdIsNull(KernelBrowser $client): void

private function skipIfMessengerIsMissing(): void
{
if (!interface_exists(MessageBusInterface::class) || Kernel::VERSION_ID < 40300) {
if (!interface_exists(MessageBusInterface::class)) {
$this->markTestSkipped('Messenger missing');
}
}
Expand Down
115 changes: 115 additions & 0 deletions tests/EventListener/AbstractConsoleListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\EventListener;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\Event;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class AbstractConsoleListenerTest extends TestCase
{
/**
* @var MockObject&HubInterface
*/
private $hub;

/**
* @var ConsoleListener
*/
private $listener;

protected function setUp(): void
{
$listenerClass = static::getListenerClass();

$this->hub = $this->createMock(HubInterface::class);
$this->listener = new $listenerClass($this->hub);
}

/**
* @dataProvider handleConsoleCommmandEventDataProvider
*
* @param array<string, string> $expectedTags
*/
public function testHandleConsoleCommandEvent(ConsoleCommandEvent $consoleEvent, array $expectedTags): void
{
$scope = new Scope();

$this->hub->expects($this->once())
->method('pushScope')
->willReturn($scope);

$this->listener->handleConsoleCommandEvent($consoleEvent);

$event = $scope->applyToEvent(Event::createEvent());

$this->assertSame($expectedTags, $event->getTags());
}

/**
* @return \Generator<mixed>
*/
public function handleConsoleCommmandEventDataProvider(): \Generator
{
yield [
new ConsoleCommandEvent(null, $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
[],
];

yield [
new ConsoleCommandEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
[],
];

yield [
new ConsoleCommandEvent(new Command('foo:bar'), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
['console.command' => 'foo:bar'],
];
}

public function testHandleConsoleTerminateEvent(): void
{
$this->hub->expects($this->once())
->method('popScope');

$this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), 0));
}

public function testHandleConsoleErrorEvent(): void
{
$scope = new Scope();
$consoleEvent = new ConsoleErrorEvent($this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), new \Exception());

$this->hub->expects($this->once())
->method('configureScope')
->willReturnCallback(static function (callable $callback) use ($scope): void {
$callback($scope);
});

$this->hub->expects($this->once())
->method('captureException')
->with($consoleEvent->getError());

$this->listener->handleConsoleErrorEvent($consoleEvent);

$event = $scope->applyToEvent(Event::createEvent());

$this->assertSame(['console.command.exit_code' => '1'], $event->getTags());
}

/**
* @return class-string<ConsoleListener>
*/
abstract protected static function getListenerClass(): string;
}
23 changes: 23 additions & 0 deletions tests/EventListener/ConsoleCommandListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\EventListener;

use Sentry\SentryBundle\EventListener\ConsoleCommandListener;

/**
* @group legacy
*
* @deprecated since version 4.1, to be removed in 5.0
*/
final class ConsoleCommandListenerTest extends AbstractConsoleListenerTest
{
/**
* {@inheritdoc}
*/
protected static function getListenerClass(): string
{
return ConsoleCommandListener::class;
}
}
98 changes: 4 additions & 94 deletions tests/EventListener/ConsoleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,15 @@

namespace Sentry\SentryBundle\Tests\EventListener;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\Event;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ConsoleListenerTest extends TestCase
final class ConsoleListenerTest extends AbstractConsoleListenerTest
{
/**
* @var MockObject&HubInterface
* {@inheritdoc}
*/
private $hub;

/**
* @var ConsoleListener
*/
private $listener;

protected function setUp(): void
{
$this->hub = $this->createMock(HubInterface::class);
$this->listener = new ConsoleListener($this->hub);
}

/**
* @dataProvider handleConsoleCommmandEventDataProvider
*
* @param array<string, string> $expectedTags
*/
public function testHandleConsoleCommandEvent(ConsoleCommandEvent $consoleEvent, array $expectedTags): void
{
$scope = new Scope();

$this->hub->expects($this->once())
->method('pushScope')
->willReturn($scope);

$this->listener->handleConsoleCommandEvent($consoleEvent);

$event = $scope->applyToEvent(Event::createEvent());

$this->assertSame($expectedTags, $event->getTags());
}

/**
* @return \Generator<mixed>
*/
public function handleConsoleCommmandEventDataProvider(): \Generator
{
yield [
new ConsoleCommandEvent(null, $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
[],
];

yield [
new ConsoleCommandEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
[],
];

yield [
new ConsoleCommandEvent(new Command('foo:bar'), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)),
['console.command' => 'foo:bar'],
];
}

public function testHandleConsoleTerminateEvent(): void
{
$this->hub->expects($this->once())
->method('popScope');

$this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), 0));
}

public function testHandleConsoleErrorEvent(): void
protected static function getListenerClass(): string
{
$scope = new Scope();
$consoleEvent = new ConsoleErrorEvent($this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), new \Exception());

$this->hub->expects($this->once())
->method('configureScope')
->willReturnCallback(static function (callable $callback) use ($scope): void {
$callback($scope);
});

$this->hub->expects($this->once())
->method('captureException')
->with($consoleEvent->getError());

$this->listener->handleConsoleErrorEvent($consoleEvent);

$event = $scope->applyToEvent(Event::createEvent());

$this->assertSame(['console.command.exit_code' => '1'], $event->getTags());
return ConsoleListener::class;
}
}

0 comments on commit 79265d9

Please sign in to comment.