Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry/sentry 2.2 compat #244

Merged
merged 6 commits into from Sep 21, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -31,6 +31,8 @@ jobs:
- composer remove --dev friendsofphp/php-cs-fixer phpstan/phpstan --no-update
- travis_retry travis_wait composer install --no-interaction --prefer-dist
- travis_retry travis_wait composer update --no-interaction --prefer-dist --prefer-stable --prefer-lowest
- name: sentry/sentry dev-develop
install: composer require sentry/sentry:dev-develop
- stage: Code style and static analysis
script:
- composer phpstan
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -9,6 +9,7 @@ parameters:
- '/Parameter \$.+ of method Sentry\\SentryBundle\\EventListener\\ErrorListener::onConsoleException\(\) has invalid typehint type Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent./'
- '/Call to method getException\(\) on an unknown class Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent./'
- '/Access to undefined constant Symfony\\Component\\Console\\ConsoleEvents::EXCEPTION./'
- '/Sentry\\SentrySdk/'

includes:
- vendor/jangregor/phpstan-prophecy/src/extension.neon
Expand Down
4 changes: 2 additions & 2 deletions src/Command/SentryTestCommand.php
Expand Up @@ -2,7 +2,7 @@

namespace Sentry\SentryBundle\Command;

use Sentry\State\Hub;
use Sentry\SentryBundle\SentryBundle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -16,7 +16,7 @@ public function __construct()

protected function execute(InputInterface $input, OutputInterface $output): int
{
$currentHub = Hub::getCurrent();
$currentHub = SentryBundle::getCurrentHub();
$client = $currentHub->getClient();

if (! $client) {
Expand Down
3 changes: 2 additions & 1 deletion src/EventListener/ConsoleListener.php
Expand Up @@ -2,6 +2,7 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\SentryBundle\SentryBundle;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
{
$command = $event->getCommand();

Hub::getCurrent()
SentryBundle::getCurrentHub()
->configureScope(function (Scope $scope) use ($command): void {
$scope->setTag('command', $command ? $command->getName() : 'N/A');
});
Expand Down
7 changes: 4 additions & 3 deletions src/EventListener/RequestListener.php
Expand Up @@ -2,6 +2,7 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\SentryBundle\SentryBundle;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function onKernelRequest(GetResponseEvent $event): void
return;
}

$currentClient = Hub::getCurrent()->getClient();
$currentClient = SentryBundle::getCurrentHub()->getClient();
if (null === $currentClient || ! $currentClient->getOptions()->shouldSendDefaultPii()) {
return;
}
Expand All @@ -76,7 +77,7 @@ public function onKernelRequest(GetResponseEvent $event): void

$userData['ip_address'] = $event->getRequest()->getClientIp();

Hub::getCurrent()
SentryBundle::getCurrentHub()
->configureScope(function (Scope $scope) use ($userData): void {
$scope->setUser($userData);
});
Expand All @@ -94,7 +95,7 @@ public function onKernelController(FilterControllerEvent $event): void

$matchedRoute = (string) $event->getRequest()->attributes->get('_route');

Hub::getCurrent()
SentryBundle::getCurrentHub()
->configureScope(function (Scope $scope) use ($matchedRoute): void {
$scope->setTag('route', $matchedRoute);
});
Expand Down
6 changes: 3 additions & 3 deletions src/EventListener/SubRequestListener.php
Expand Up @@ -2,7 +2,7 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\State\Hub;
use Sentry\SentryBundle\SentryBundle;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

Expand All @@ -19,7 +19,7 @@ public function onKernelRequest(GetResponseEvent $event): void
return;
}

Hub::getCurrent()->pushScope();
SentryBundle::getCurrentHub()->pushScope();
}

/**
Expand All @@ -33,6 +33,6 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void
return;
}

Hub::getCurrent()->popScope();
SentryBundle::getCurrentHub()->popScope();
}
}
3 changes: 0 additions & 3 deletions src/Resources/config/services.xml
Expand Up @@ -17,9 +17,6 @@

<service id="Sentry\State\HubInterface" class="Sentry\State\Hub" public="false">
<argument type="service" id="Sentry\ClientInterface" />
<call method="setCurrent">
<argument type="service" id="Sentry\State\HubInterface" />
</call>
</service>

<service id="Sentry\SentryBundle\EventListener\ConsoleListener" class="Sentry\SentryBundle\EventListener\ConsoleListener" public="false">
Expand Down
27 changes: 27 additions & 0 deletions src/SentryBundle.php
Expand Up @@ -3,6 +3,9 @@
namespace Sentry\SentryBundle;

use Jean85\PrettyVersions;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class SentryBundle extends Bundle
Expand All @@ -14,4 +17,28 @@ public static function getSdkVersion(): string
return PrettyVersions::getVersion('sentry/sentry-symfony')
->getPrettyVersion();
}

/**
* This method avoids deprecations with sentry/sentry:^2.2
*/
public static function getCurrentHub(): HubInterface
{
if (class_exists(SentrySdk::class)) {
return SentrySdk::getCurrentHub();
}

return Hub::getCurrent();
}

/**
* This method avoids deprecations with sentry/sentry:^2.2
*/
public static function setCurrentHub(HubInterface $hub): void
{
if (class_exists(SentrySdk::class)) {
SentrySdk::setCurrentHub($hub);
}

Hub::setCurrent($hub);
}
}
13 changes: 11 additions & 2 deletions test/EventListener/ConsoleListenerTest.php
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sentry\Event;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function testOnConsoleCommandAddsCommandName(): void

$listener->onConsoleCommand($event->reveal());

$this->assertSame(['command' => 'sf:command:name'], $this->currentScope->getTags());
$this->assertSame(['command' => 'sf:command:name'], $this->getTagsContext($this->currentScope));
}

public function testOnConsoleCommandAddsPlaceholderCommandName(): void
Expand All @@ -60,6 +61,14 @@ public function testOnConsoleCommandAddsPlaceholderCommandName(): void

$listener->onConsoleCommand($event->reveal());

$this->assertSame(['command' => 'N/A'], $this->currentScope->getTags());
$this->assertSame(['command' => 'N/A'], $this->getTagsContext($this->currentScope));
}

private function getTagsContext(Scope $scope): array
{
$event = new Event();
$scope->applyToEvent($event, []);

return $event->getTagsContext()->toArray();
}
}
37 changes: 27 additions & 10 deletions test/EventListener/RequestListenerTest.php
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\Options;
use Sentry\SentryBundle\EventListener\RequestListener;
use Sentry\State\Hub;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function testOnKernelRequestUserDataIsSetToScope($user): void
'ip_address' => '1.2.3.4',
'username' => 'john-doe',
];
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
}

public function userDataProvider(): \Generator
Expand Down Expand Up @@ -131,7 +132,7 @@ public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void

$listener->onKernelRequest($event->reveal());

$this->assertEquals([], $this->currentScope->getUser());
$this->assertEquals([], $this->getUserContext($this->currentScope));
}

public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
Expand All @@ -156,7 +157,7 @@ public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void

$listener->onKernelRequest($event->reveal());

$this->assertEquals([], $this->currentScope->getUser());
$this->assertEquals([], $this->getUserContext($this->currentScope));
}

public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): void
Expand Down Expand Up @@ -187,7 +188,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): voi
$expectedUserData = [
'ip_address' => '1.2.3.4',
];
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
}

public function testOnKernelRequestUsernameIsNotSetIfAuthorizationCheckerIsAbsent(): void
Expand Down Expand Up @@ -218,7 +219,7 @@ public function testOnKernelRequestUsernameIsNotSetIfAuthorizationCheckerIsAbsen
$expectedUserData = [
'ip_address' => '1.2.3.4',
];
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
}

public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
Expand Down Expand Up @@ -253,7 +254,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
$expectedUserData = [
'ip_address' => '1.2.3.4',
];
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
}

/**
Expand Down Expand Up @@ -295,7 +296,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsNotAuthenticated():
$expectedUserData = [
'ip_address' => '1.2.3.4',
];
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
}

public function testOnKernelRequestUsernameIsNotSetIfUserIsNotRemembered(): void
Expand Down Expand Up @@ -330,7 +331,7 @@ public function testOnKernelRequestUsernameIsNotSetIfUserIsNotRemembered(): void
$expectedUserData = [
'ip_address' => '1.2.3.4',
];
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
}

public function testOnKernelControllerAddsRouteTag(): void
Expand Down Expand Up @@ -403,8 +404,24 @@ public function testOnKernelRequestUserDataAndTagsAreNotSetInSubRequest(): void

$listener->onKernelRequest($event->reveal());

$this->assertEmpty($this->currentScope->getUser());
$this->assertEmpty($this->currentScope->getTags());
$this->assertEmpty($this->getUserContext($this->currentScope));
$this->assertEmpty($this->getTagsContext($this->currentScope));
}

private function getUserContext(Scope $scope): array
{
$event = new Event();
$scope->applyToEvent($event, []);

return $event->getUserContext()->toArray();
}

private function getTagsContext(Scope $scope): array
{
$event = new Event();
$scope->applyToEvent($event, []);

return $event->getTagsContext()->toArray();
}
}

Expand Down