Skip to content

Commit 021fc91

Browse files
authored
Updates for sentry/sentry 2.2 (#273)
* Updates for sentry/sentry 2.2 * Test against the latest version of sentry/sentry
1 parent c92416a commit 021fc91

11 files changed

+104
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ package.xml
44
.idea
55
.php_cs.cache
66
docs/_build
7+
.phpunit.result.cache

.travis.yml

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,34 @@ php:
88

99
env:
1010
matrix:
11-
- LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
12-
- LARAVEL=5.1.* TESTBENCH=3.1.* PHPUNIT=5.7.*
13-
- LARAVEL=5.2.* TESTBENCH=3.2.* PHPUNIT=5.7.*
14-
- LARAVEL=5.3.* TESTBENCH=3.3.* PHPUNIT=5.7.*
15-
- LARAVEL=5.4.* TESTBENCH=3.4.* PHPUNIT=5.7.*
16-
- LARAVEL=5.5.* TESTBENCH=3.5.* PHPUNIT=6.5.*
17-
- LARAVEL=5.6.* TESTBENCH=3.6.* PHPUNIT=7.5.*
18-
- LARAVEL=5.7.* TESTBENCH=3.7.* PHPUNIT=7.5.*
19-
- LARAVEL=5.8.* TESTBENCH=3.8.* PHPUNIT=7.5.*
20-
- LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.*
11+
- LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0
12+
- LARAVEL=5.1.* TESTBENCH=3.1.* PHPUNIT=5.7.* SENTRY=^2.0
13+
- LARAVEL=5.2.* TESTBENCH=3.2.* PHPUNIT=5.7.* SENTRY=^2.0
14+
- LARAVEL=5.3.* TESTBENCH=3.3.* PHPUNIT=5.7.* SENTRY=^2.0
15+
- LARAVEL=5.4.* TESTBENCH=3.4.* PHPUNIT=5.7.* SENTRY=^2.0
16+
- LARAVEL=5.5.* TESTBENCH=3.5.* PHPUNIT=6.5.* SENTRY=^2.0
17+
- LARAVEL=5.6.* TESTBENCH=3.6.* PHPUNIT=7.5.* SENTRY=^2.0
18+
- LARAVEL=5.7.* TESTBENCH=3.7.* PHPUNIT=7.5.* SENTRY=^2.0
19+
- LARAVEL=5.8.* TESTBENCH=3.8.* PHPUNIT=7.5.* SENTRY=^2.0
20+
- LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=^2.0
21+
# We add one more test using the bleading edge of the sentry/sentry package
22+
- LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=dev-develop@dev
2123

2224
matrix:
2325
fast_finish: true
2426
allow_failures:
2527
- php: 7.4snapshot
2628
exclude:
2729
- php: 7.1
28-
env: LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.*
30+
env: LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=^2.0
31+
- php: 7.1
32+
env: LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=dev-develop@dev
2933
- php: 7.2
30-
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
34+
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0
3135
- php: 7.3
32-
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
36+
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0
3337
- php: 7.4snapshot
34-
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
38+
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0
3539

3640
cache:
3741
directories:
@@ -51,7 +55,7 @@ jobs:
5155

5256
before_install:
5357
- if [ "$USE_COMPOSER_JSON" != "1" ]; then composer remove friendsofphp/php-cs-fixer --dev --no-update; fi;
54-
- if [ "$USE_COMPOSER_JSON" != "1" ]; then composer require laravel/framework:$LARAVEL illuminate/support:$LARAVEL orchestra/testbench:$TESTBENCH phpunit/phpunit:$PHPUNIT --no-update --no-interaction --dev; fi;
58+
- if [ "$USE_COMPOSER_JSON" != "1" ]; then composer require laravel/framework:$LARAVEL illuminate/support:$LARAVEL orchestra/testbench:$TESTBENCH phpunit/phpunit:$PHPUNIT sentry/sentry:$SENTRY --no-update --no-interaction --dev; fi;
5559

5660
install:
5761
- travis_retry composer install --no-suggest --no-interaction --prefer-dist --no-progress

src/Sentry/Laravel/EventHandler.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use RuntimeException;
1717
use Sentry\State\Scope;
1818
use Sentry\Breadcrumb;
19-
use Sentry\State\Hub;
2019

2120
class EventHandler
2221
{
@@ -115,7 +114,7 @@ public function subscribeQueueEvents(QueueManager $queue)
115114
Integration::flushEvents();
116115

117116
// We have added a scope when a job starts processing
118-
Hub::getCurrent()->popScope();
117+
Integration::getCurrentHub()->popScope();
119118
});
120119

121120
foreach (static::$queueEventHandlerMap as $eventName => $handler) {
@@ -283,7 +282,7 @@ protected function authenticatedHandler(Authenticated $event)
283282
protected function queueJobProcessingHandler(JobProcessing $event)
284283
{
285284
// When a job starts, we want to push a new scope
286-
Hub::getCurrent()->pushScope();
285+
Integration::getCurrentHub()->pushScope();
287286

288287
$job = [
289288
'job' => $event->job->getName(),

src/Sentry/Laravel/Integration.php

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace Sentry\Laravel;
44

5+
use Sentry\FlushableClientInterface;
6+
use Sentry\SentrySdk;
7+
use Sentry\State\Hub;
8+
use Sentry\State\HubInterface;
59
use function Sentry\addBreadcrumb;
610
use function Sentry\configureScope;
711
use Sentry\Breadcrumb;
812
use Sentry\Event;
9-
use Sentry\Client;
1013
use Sentry\Integration\IntegrationInterface;
11-
use Sentry\State\Hub;
1214
use Sentry\State\Scope;
13-
use Sentry\Transport\HttpTransport;
1415

1516
class Integration implements IntegrationInterface
1617
{
@@ -25,7 +26,7 @@ class Integration implements IntegrationInterface
2526
public function setupOnce(): void
2627
{
2728
Scope::addGlobalEventProcessor(function (Event $event): Event {
28-
$self = Hub::getCurrent()->getIntegration(self::class);
29+
$self = static::getCurrentHub()->getIntegration(self::class);
2930

3031
if (!$self instanceof self) {
3132
return $event;
@@ -44,7 +45,7 @@ public function setupOnce(): void
4445
*/
4546
public static function addBreadcrumb(Breadcrumb $breadcrumb): void
4647
{
47-
$self = Hub::getCurrent()->getIntegration(self::class);
48+
$self = static::getCurrentHub()->getIntegration(self::class);
4849

4950
if (!$self instanceof self) {
5051
return;
@@ -60,7 +61,7 @@ public static function addBreadcrumb(Breadcrumb $breadcrumb): void
6061
*/
6162
public static function configureScope(callable $callback): void
6263
{
63-
$self = Hub::getCurrent()->getIntegration(self::class);
64+
$self = static::getCurrentHub()->getIntegration(self::class);
6465

6566
if (!$self instanceof self) {
6667
return;
@@ -93,21 +94,57 @@ public static function setTransaction($transaction): void
9394
*/
9495
public static function flushEvents(): void
9596
{
96-
$client = Hub::getCurrent()->getClient();
97+
$client = static::getCurrentHub()->getClient();
9798

98-
if ($client instanceof Client) {
99-
$transportProperty = new \ReflectionProperty(Client::class, 'transport');
100-
$transportProperty->setAccessible(true);
99+
if ($client instanceof FlushableClientInterface) {
100+
$client->flush();
101+
}
102+
}
101103

102-
$transport = $transportProperty->getValue($client);
104+
/**
105+
* Gets the current hub. If it's not initialized then creates a new instance
106+
* and sets it as current hub.
107+
*
108+
* The is here for legacy reasons where we used the Hub directly as a singleton.
109+
*
110+
* @TODO: This method should be removed and replaced with calls to `SentrySdk::getCurrentHub()` directly once
111+
* `sentry/sentry` 3.0 is released and pinned as an dependency.
112+
*
113+
* @internal This is not part of the public API and is here temporarily.
114+
*
115+
* @return \Sentry\State\HubInterface
116+
*/
117+
public static function getCurrentHub(): HubInterface
118+
{
119+
if (class_exists(SentrySdk::class)) {
120+
SentrySdk::getCurrentHub();
121+
}
103122

104-
if ($transport instanceof HttpTransport) {
105-
$closure = \Closure::bind(function () {
106-
$this->cleanupPendingRequests();
107-
}, $transport, $transport);
123+
return Hub::getCurrent();
124+
}
108125

109-
$closure();
110-
}
126+
/**
127+
* Sets the current hub.
128+
*
129+
* The is here for legacy reasons where we used the Hub directly as a singleton.
130+
*
131+
* @TODO: This method should be removed and replaced with calls to `SentrySdk::getCurrentHub()` directly once
132+
* `sentry/sentry` 3.0 is released and pinned as an dependency.
133+
*
134+
* @internal This is not part of the public API and is here temporarily.
135+
*
136+
* @param \Sentry\State\HubInterface $hub
137+
*
138+
* @return void
139+
*/
140+
public static function setCurrentHub(HubInterface $hub): void
141+
{
142+
if (class_exists(SentrySdk::class)) {
143+
SentrySdk::setCurrentHub($hub);
144+
145+
return;
111146
}
147+
148+
Hub::setCurrent($hub);
112149
}
113150
}

src/Sentry/Laravel/SentryHandler.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,17 @@ protected function getLogLevel($logLevel)
123123
switch ($logLevel) {
124124
case Logger::DEBUG:
125125
return Severity::debug();
126-
case Logger::INFO:
127-
return Severity::info();
128126
case Logger::NOTICE:
127+
case Logger::INFO:
129128
return Severity::info();
130129
case Logger::WARNING:
131130
return Severity::warning();
132131
case Logger::ERROR:
133132
return Severity::error();
134-
case Logger::CRITICAL:
135-
return Severity::fatal();
136133
case Logger::ALERT:
137-
return Severity::fatal();
138134
case Logger::EMERGENCY:
135+
case Logger::CRITICAL:
139136
return Severity::fatal();
140-
141137
}
142138
}
143139

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ protected function configureAndRegisterClient(): void
123123
$clientBuilder->setSdkIdentifier(Version::SDK_IDENTIFIER);
124124
$clientBuilder->setSdkVersion(Version::SDK_VERSION);
125125

126-
Hub::setCurrent(new Hub($clientBuilder->getClient()));
126+
$hub = new Hub($clientBuilder->getClient());
127127

128-
return Hub::getCurrent();
128+
Integration::setCurrentHub($hub);
129+
130+
return $hub;
129131
});
130132

131133
$this->app->alias(static::$abstract, HubInterface::class);

test/Sentry/IntegrationsOptionTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Sentry\Laravel\Tests;
44

5-
use Sentry\State\Hub;
65
use Sentry\Integration\IntegrationInterface;
76

87
class IntegrationsOptionTest extends SentryLaravelTestCase
@@ -24,7 +23,7 @@ public function testCustomIntegrationIsResolvedFromContainerByAlias()
2423
],
2524
]);
2625

27-
$this->assertNotNull(Hub::getCurrent()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
26+
$this->assertNotNull($this->getHubFromContainer()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
2827
}
2928

3029
public function testCustomIntegrationIsResolvedFromContainerByClass()
@@ -35,7 +34,7 @@ public function testCustomIntegrationIsResolvedFromContainerByClass()
3534
],
3635
]);
3736

38-
$this->assertNotNull(Hub::getCurrent()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
37+
$this->assertNotNull($this->getHubFromContainer()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
3938
}
4039

4140
public function testCustomIntegrationByInstance()
@@ -46,7 +45,7 @@ public function testCustomIntegrationByInstance()
4645
],
4746
]);
4847

49-
$this->assertNotNull(Hub::getCurrent()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
48+
$this->assertNotNull($this->getHubFromContainer()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
5049
}
5150

5251
/**

test/Sentry/SentryLaravelTestCase.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Sentry\Laravel\Tests;
44

5-
use Sentry\State\Scope;
65
use Sentry\State\HubInterface;
76
use Sentry\Laravel\ServiceProvider;
87
use Orchestra\Testbench\TestCase as LaravelTestCase;
@@ -47,12 +46,24 @@ protected function dispatchLaravelEvent($event, array $payload = [])
4746
: $dispatcher->fire($event, $payload);
4847
}
4948

50-
protected function getScope(HubInterface $hub): Scope
49+
protected function getHubFromContainer(): HubInterface
5150
{
52-
$method = new \ReflectionMethod($hub, 'getScope');
51+
return $this->app->make('sentry');
52+
}
53+
54+
protected function getCurrentBreadcrumbs(): array
55+
{
56+
$hub = $this->getHubFromContainer();
5357

58+
$method = new \ReflectionMethod($hub, 'getScope');
5459
$method->setAccessible(true);
5560

56-
return $method->invoke($hub);
61+
/** @var \Sentry\State\Scope $scope */
62+
$scope = $method->invoke($hub);
63+
64+
$property = new \ReflectionProperty($scope, 'breadcrumbs');
65+
$property->setAccessible(true);
66+
67+
return $property->getValue($scope);
5768
}
5869
}

test/Sentry/SqlBindingsInBreadcrumbsTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Sentry\Laravel\Tests;
44

5-
use Sentry\State\Hub;
6-
75
class SqlBindingsInBreadcrumbsTest extends SentryLaravelTestCase
86
{
97
public function testSqlBindingsAreRecordedWhenEnabled()
@@ -21,7 +19,7 @@ public function testSqlBindingsAreRecordedWhenEnabled()
2119
'test',
2220
]);
2321

24-
$breadcrumbs = $this->getScope(Hub::getCurrent())->getBreadcrumbs();
22+
$breadcrumbs = $this->getCurrentBreadcrumbs();
2523

2624
/** @var \Sentry\Breadcrumb $lastBreadcrumb */
2725
$lastBreadcrumb = end($breadcrumbs);
@@ -45,7 +43,7 @@ public function testSqlBindingsAreRecordedWhenDisabled()
4543
'test',
4644
]);
4745

48-
$breadcrumbs = $this->getScope(Hub::getCurrent())->getBreadcrumbs();
46+
$breadcrumbs = $this->getCurrentBreadcrumbs();
4947

5048
/** @var \Sentry\Breadcrumb $lastBreadcrumb */
5149
$lastBreadcrumb = end($breadcrumbs);

test/Sentry/SqlBindingsInBreadcrumbsWithOldConfigKeyDisabledTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Sentry\Laravel\Tests;
44

5-
use Sentry\State\Hub;
65
use Illuminate\Config\Repository;
76

87
class SqlBindingsInBreadcrumbsWithOldConfigKeyDisabledTest extends SentryLaravelTestCase
@@ -29,7 +28,7 @@ public function testSqlBindingsAreRecordedWhenDisabledByOldConfigKey()
2928
'test',
3029
]);
3130

32-
$breadcrumbs = $this->getScope(Hub::getCurrent())->getBreadcrumbs();
31+
$breadcrumbs = $this->getCurrentBreadcrumbs();
3332

3433
/** @var \Sentry\Breadcrumb $lastBreadcrumb */
3534
$lastBreadcrumb = end($breadcrumbs);

0 commit comments

Comments
 (0)