diff --git a/.travis.yml b/.travis.yml index 24126437..e7453b73 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e50afaa..c1dc023f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] - Add forward compatibility with Symfony 5 (#235, thanks to @garak) + - Fix compatibility with sentry/sentry 2.2+ (#244) ## 3.1.0 - 2019-07-02 - Add support for Symfony 2.8 (#233, thanks to @nocive) diff --git a/phpstan.neon b/phpstan.neon index 7fdac1a5..b1cacf0f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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 diff --git a/src/Command/SentryTestCommand.php b/src/Command/SentryTestCommand.php index 445bbb90..7634adad 100644 --- a/src/Command/SentryTestCommand.php +++ b/src/Command/SentryTestCommand.php @@ -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; @@ -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) { diff --git a/src/EventListener/ConsoleListener.php b/src/EventListener/ConsoleListener.php index 788074ba..71972214 100644 --- a/src/EventListener/ConsoleListener.php +++ b/src/EventListener/ConsoleListener.php @@ -2,6 +2,7 @@ namespace Sentry\SentryBundle\EventListener; +use Sentry\SentryBundle\SentryBundle; use Sentry\State\Hub; use Sentry\State\HubInterface; use Sentry\State\Scope; @@ -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'); }); diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index e3b110e1..06a2a2be 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -2,6 +2,7 @@ namespace Sentry\SentryBundle\EventListener; +use Sentry\SentryBundle\SentryBundle; use Sentry\State\Hub; use Sentry\State\HubInterface; use Sentry\State\Scope; @@ -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; } @@ -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); }); @@ -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); }); diff --git a/src/EventListener/SubRequestListener.php b/src/EventListener/SubRequestListener.php index 80c473c8..5ffa0704 100644 --- a/src/EventListener/SubRequestListener.php +++ b/src/EventListener/SubRequestListener.php @@ -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; @@ -19,7 +19,7 @@ public function onKernelRequest(GetResponseEvent $event): void return; } - Hub::getCurrent()->pushScope(); + SentryBundle::getCurrentHub()->pushScope(); } /** @@ -33,6 +33,6 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void return; } - Hub::getCurrent()->popScope(); + SentryBundle::getCurrentHub()->popScope(); } } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index e45c32ed..d768e84f 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -17,9 +17,6 @@ - - - diff --git a/src/SentryBundle.php b/src/SentryBundle.php index acf470c9..15f0fe4c 100644 --- a/src/SentryBundle.php +++ b/src/SentryBundle.php @@ -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 @@ -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); + } } diff --git a/test/EventListener/ConsoleListenerTest.php b/test/EventListener/ConsoleListenerTest.php index 7671e6dc..2c7b361a 100644 --- a/test/EventListener/ConsoleListenerTest.php +++ b/test/EventListener/ConsoleListenerTest.php @@ -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; @@ -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 @@ -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(); } } diff --git a/test/EventListener/RequestListenerTest.php b/test/EventListener/RequestListenerTest.php index 568a2605..5f013974 100644 --- a/test/EventListener/RequestListenerTest.php +++ b/test/EventListener/RequestListenerTest.php @@ -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; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)); } /** @@ -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 @@ -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 @@ -352,7 +353,7 @@ public function testOnKernelControllerAddsRouteTag(): void $listener->onKernelController($event->reveal()); - $this->assertSame(['route' => 'sf-route'], $this->currentScope->getTags()); + $this->assertSame(['route' => 'sf-route'], $this->getTagsContext($this->currentScope)); } public function testOnKernelControllerRouteTagIsNotSetIfRequestDoesNotHaveARoute(): void @@ -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(); } }