diff --git a/.php_cs b/.php_cs index 2ed598fb7..04238d23c 100644 --- a/.php_cs +++ b/.php_cs @@ -13,6 +13,7 @@ return PhpCsFixer\Config::create() 'psr4' => true, 'random_api_migration' => true, 'yoda_style' => true, + 'self_accessor' => false, 'phpdoc_no_useless_inheritdoc' => false, 'phpdoc_align' => [ 'tags' => ['param', 'return', 'throws', 'type', 'var'], diff --git a/CHANGELOG.md b/CHANGELOG.md index 8306df2d2..2eea33be2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fix `TypeError` in `Sentry\Monolog\Handler` when the extra data array has numeric keys (#833). - Changed type hint for both parameter and return value of `HubInterface::getCurrentHub` and `HubInterface::setCurrentHub()` methods (#849) +- Add the `setTags`, `setExtras` and `clearBreadcrumbs` methods to the `Scope` class (#852) ## 2.1.1 (2019-06-13) diff --git a/src/State/Hub.php b/src/State/Hub.php index a37cb5f3a..f11c7279b 100644 --- a/src/State/Hub.php +++ b/src/State/Hub.php @@ -227,6 +227,7 @@ public static function setCurrent(HubInterface $hub): HubInterface public function getIntegration(string $className): ?IntegrationInterface { $client = $this->getClient(); + if (null !== $client) { return $client->getIntegration($className); } diff --git a/src/State/Scope.php b/src/State/Scope.php index d0e58b71e..dcbf6b112 100644 --- a/src/State/Scope.php +++ b/src/State/Scope.php @@ -85,15 +85,17 @@ public function setTag(string $key, string $value): self } /** - * Gets the tags contained in the tags context. + * Merges the given tags into the current tags context. * - * @return array + * @param array $tags The tags to merge into the current context * - * @internal + * @return $this */ - public function getTags(): array + public function setTags(array $tags): self { - return $this->tags->toArray(); + $this->tags->merge($tags); + + return $this; } /** @@ -112,15 +114,17 @@ public function setExtra(string $key, $value): self } /** - * Gets the information contained in the extra context. + * Merges the given data into the current extras context. * - * @return array + * @param array $extras Data to merge into the current context * - * @internal + * @return $this */ - public function getExtra(): array + public function setExtras(array $extras): self { - return $this->extra->toArray(); + $this->extra->merge($extras); + + return $this; } /** @@ -137,18 +141,6 @@ public function setUser(array $data): self return $this; } - /** - * Gets the information contained in the user context. - * - * @return array - * - * @internal - */ - public function getUser(): array - { - return $this->user->toArray(); - } - /** * Sets the list of strings used to dictate the deduplication of this event. * @@ -163,18 +155,6 @@ public function setFingerprint(array $fingerprint): self return $this; } - /** - * Gets the list of strings used to dictate the deduplication of this event. - * - * @return string[] - * - * @internal - */ - public function getFingerprint(): array - { - return $this->fingerprint; - } - /** * Sets the severity to apply to all events captured in this scope. * @@ -189,18 +169,6 @@ public function setLevel(?Severity $level): self return $this; } - /** - * Gets the severity to apply to all events captured in this scope. - * - * @return Severity|null - * - * @internal - */ - public function getLevel(): ?Severity - { - return $this->level; - } - /** * Add the given breadcrumb to the scope. * @@ -218,15 +186,15 @@ public function addBreadcrumb(Breadcrumb $breadcrumb, int $maxBreadcrumbs = 100) } /** - * Gets the breadcrumbs. - * - * @return Breadcrumb[] + * Clears all the breadcrumbs. * - * @internal + * @return $this */ - public function getBreadcrumbs(): array + public function clearBreadcrumbs(): self { - return $this->breadcrumbs; + $this->breadcrumbs = []; + + return $this; } /** diff --git a/tests/Monolog/HandlerTest.php b/tests/Monolog/HandlerTest.php index 430daaa2d..2bd4715bb 100644 --- a/tests/Monolog/HandlerTest.php +++ b/tests/Monolog/HandlerTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Sentry\ClientInterface; +use Sentry\Event; use Sentry\Monolog\Handler; use Sentry\Severity; use Sentry\State\Hub; @@ -15,36 +16,28 @@ final class HandlerTest extends TestCase { - /** - * @var MockObject|ClientInterface - */ - private $client; - - protected function setUp(): void - { - $this->client = $this->createMock(ClientInterface::class); - } - /** * @dataProvider handleDataProvider */ public function testHandle(array $record, array $expectedPayload, array $expectedExtra, array $expectedTags): void { - $this->client->expects($this->once()) + $scope = new Scope(); + + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) ->method('captureEvent') - ->with($expectedPayload, $this->callback(static function (Scope $scope) use ($expectedExtra, $expectedTags): bool { - if ($expectedExtra !== $scope->getExtra()) { - return false; - } + ->with($expectedPayload, $this->callback(function (Scope $scopeArg) use ($expectedExtra, $expectedTags): bool { + $event = $scopeArg->applyToEvent(new Event(), []); - if ($expectedTags !== $scope->getTags()) { - return false; - } + $this->assertNotNull($event); + $this->assertSame($expectedExtra, $event->getExtraContext()->toArray()); + $this->assertSame($expectedTags, $event->getTagsContext()->toArray()); return true; })); - $handler = new Handler(new Hub($this->client)); + $handler = new Handler(new Hub($client, $scope)); $handler->handle($record); } @@ -313,7 +306,7 @@ public function handleDataProvider(): \Generator [ 'monolog.channel' => 'channel.foo', 'monolog.level' => Logger::getLevelName(Logger::INFO), - '1' => 'numeric key', + '0' => 'numeric key', ], [], ]; @@ -332,6 +325,7 @@ public function handleDataProvider(): \Generator [ 'level' => Severity::debug(), 'message' => 'foo bar', + 'logger' => 'monolog.channel.foo', 'transaction' => 'Foo transaction', ], [ diff --git a/tests/SdkTest.php b/tests/SdkTest.php index 6d0f55e37..fabc1c8d1 100644 --- a/tests/SdkTest.php +++ b/tests/SdkTest.php @@ -14,8 +14,11 @@ use function Sentry\captureMessage; use Sentry\ClientInterface; use function Sentry\configureScope; +use Sentry\Event; use function Sentry\init; +use Sentry\Options; use Sentry\State\Hub; +use Sentry\State\Scope; use function Sentry\withScope; class SdkTest extends TestCase @@ -92,14 +95,21 @@ public function testAddBreadcrumb(): void { $breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); - addBreadcrumb($breadcrumb); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('getOptions') + ->willReturn(new Options()); - $hub = Hub::getCurrent(); + Hub::getCurrent()->bindClient($client); - $method = new \ReflectionMethod($hub, 'getScope'); - $method->setAccessible(true); + addBreadcrumb($breadcrumb); + configureScope(function (Scope $scope) use ($breadcrumb): void { + $event = $scope->applyToEvent(new Event(), []); - $this->assertSame([$breadcrumb], $method->invoke($hub)->getBreadcrumbs()); + $this->assertNotNull($event); + $this->assertSame([$breadcrumb], $event->getBreadcrumbs()); + }); } public function testWithScope(): void diff --git a/tests/State/HubTest.php b/tests/State/HubTest.php index a95e73caf..f6b1c82cc 100644 --- a/tests/State/HubTest.php +++ b/tests/State/HubTest.php @@ -7,25 +7,19 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Sentry\Breadcrumb; -use Sentry\ClientBuilder; use Sentry\ClientInterface; +use Sentry\Event; +use Sentry\Integration\IntegrationInterface; +use Sentry\Options; use Sentry\Severity; use Sentry\State\Hub; -use Sentry\State\HubInterface; use Sentry\State\Scope; final class HubTest extends TestCase { - public function testConstructorCreatesScopeAutomatically(): void - { - $hub = new Hub(null, null); - - $this->assertNotNull($this->getScope($hub)); - } - public function testGetClient(): void { - /** @var ClientInterface|MockObject $client */ + /** @var ClientInterface&MockObject $client */ $client = $this->createMock(ClientInterface::class); $hub = new Hub($client); @@ -34,15 +28,22 @@ public function testGetClient(): void public function testGetScope(): void { + $callbackInvoked = false; $scope = new Scope(); $hub = new Hub($this->createMock(ClientInterface::class), $scope); - $this->assertSame($scope, $this->getScope($hub)); + $hub->configureScope(function (Scope $scopeArg) use (&$callbackInvoked, $scope) { + $this->assertSame($scope, $scopeArg); + + $callbackInvoked = true; + }); + + $this->assertTrue($callbackInvoked); } public function testGetLastEventId(): void { - /** @var ClientInterface|MockObject $client */ + /** @var ClientInterface&MockObject $client */ $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('captureMessage') @@ -56,40 +57,73 @@ public function testGetLastEventId(): void public function testPushScope(): void { - $hub = new Hub($this->createMock(ClientInterface::class)); + /** @var ClientInterface&MockObject $client1 */ + $client1 = $this->createMock(ClientInterface::class); + $scope1 = new Scope(); + $hub = new Hub($client1, $scope1); - $scope1 = $this->getScope($hub); - $client1 = $hub->getClient(); + $this->assertSame($client1, $hub->getClient()); $scope2 = $hub->pushScope(); - $client2 = $hub->getClient(); - $this->assertNotSame($scope1, $scope2); - $this->assertSame($scope2, $this->getScope($hub)); - $this->assertSame($client1, $client2); $this->assertSame($client1, $hub->getClient()); + $this->assertNotSame($scope1, $scope2); + + $hub->configureScope(function (Scope $scopeArg) use (&$callbackInvoked, $scope2): void { + $this->assertSame($scope2, $scopeArg); + + $callbackInvoked = true; + }); + + $this->assertTrue($callbackInvoked); } public function testPopScope(): void { - $hub = new Hub($this->createMock(ClientInterface::class)); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $scope1 = new Scope(); + $hub = new Hub($client, $scope1); - $scope1 = $this->getScope($hub); - $client = $hub->getClient(); + $this->assertFalse($hub->popScope()); $scope2 = $hub->pushScope(); - $this->assertSame($scope2, $this->getScope($hub)); + $callbackInvoked = false; + + $hub->configureScope(function (Scope $scopeArg) use ($scope2, &$callbackInvoked): void { + $this->assertSame($scope2, $scopeArg); + + $callbackInvoked = true; + }); + + $this->assertTrue($callbackInvoked); $this->assertSame($client, $hub->getClient()); $this->assertTrue($hub->popScope()); - $this->assertSame($scope1, $this->getScope($hub)); + $callbackInvoked = false; + + $hub->configureScope(function (Scope $scopeArg) use ($scope1, &$callbackInvoked): void { + $this->assertSame($scope1, $scopeArg); + + $callbackInvoked = true; + }); + + $this->assertTrue($callbackInvoked); $this->assertSame($client, $hub->getClient()); $this->assertFalse($hub->popScope()); - $this->assertSame($scope1, $this->getScope($hub)); + $callbackInvoked = false; + + $hub->configureScope(function (Scope $scopeArg) use ($scope1, &$callbackInvoked): void { + $this->assertSame($scope1, $scopeArg); + + $callbackInvoked = true; + }); + + $this->assertTrue($callbackInvoked); $this->assertSame($client, $hub->getClient()); } @@ -97,9 +131,6 @@ public function testWithScope(): void { $scope = new Scope(); $hub = new Hub($this->createMock(ClientInterface::class), $scope); - - $this->assertSame($scope, $this->getScope($hub)); - $callbackInvoked = false; try { @@ -117,16 +148,22 @@ public function testWithScope(): void } $this->assertTrue($callbackInvoked); - $this->assertSame($scope, $this->getScope($hub)); + + $callbackInvoked = false; + + $hub->configureScope(function (Scope $scopeArg) use (&$callbackInvoked, $scope): void { + $this->assertSame($scope, $scopeArg); + + $callbackInvoked = true; + }); + + $this->assertTrue($callbackInvoked); } public function testConfigureScope(): void { - /** @var ClientInterface|MockObject $client */ - $client = $this->createMock(ClientInterface::class); - - $hub = new Hub($client); - $scope = $hub->pushScope(); + $scope = new Scope(); + $hub = new Hub(null, $scope); $hub->configureScope(function (Scope $scopeArg) use ($scope, &$callbackInvoked): void { $this->assertSame($scope, $scopeArg); @@ -135,15 +172,14 @@ public function testConfigureScope(): void }); $this->assertTrue($callbackInvoked); - $this->assertSame($scope, $this->getScope($hub)); } public function testBindClient(): void { - /** @var ClientInterface|MockObject $client1 */ + /** @var ClientInterface&MockObject $client1 */ $client1 = $this->createMock(ClientInterface::class); - /** @var ClientInterface|MockObject $client2 */ + /** @var ClientInterface&MockObject $client2 */ $client2 = $this->createMock(ClientInterface::class); $hub = new Hub($client1); @@ -155,88 +191,123 @@ public function testBindClient(): void $this->assertSame($client2, $hub->getClient()); } - public function testCaptureMessageDoesNothingIfClientIsNotBinded() - { - $hub = new Hub(); - - $this->assertNull($hub->captureMessage('foo')); - } - public function testCaptureMessage(): void { - /** @var ClientInterface|MockObject $client */ + /** @var ClientInterface&MockObject $client */ $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('captureMessage') ->with('foo', Severity::debug()) ->willReturn('2b867534eead412cbdb882fd5d441690'); - $hub = new Hub($client); + $hub = new Hub(); - $this->assertEquals('2b867534eead412cbdb882fd5d441690', $hub->captureMessage('foo', Severity::debug())); - } + $this->assertNull($hub->captureMessage('foo')); - public function testCaptureExceptionDoesNothingIfClientIsNotBinded() - { - $hub = new Hub(); + $hub->bindClient($client); - $this->assertNull($hub->captureException(new \RuntimeException())); + $this->assertEquals('2b867534eead412cbdb882fd5d441690', $hub->captureMessage('foo', Severity::debug())); } public function testCaptureException(): void { $exception = new \RuntimeException('foo'); - /** @var ClientInterface|MockObject $client */ + /** @var ClientInterface&MockObject $client */ $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('captureException') ->with($exception) ->willReturn('2b867534eead412cbdb882fd5d441690'); - $hub = new Hub($client); + $hub = new Hub(); + + $this->assertNull($hub->captureException(new \RuntimeException())); + + $hub->bindClient($client); $this->assertEquals('2b867534eead412cbdb882fd5d441690', $hub->captureException($exception)); } - public function testAddBreadcrumbDoesNothingIfClientIsNotBinded(): void + public function testCaptureLastError(): void { - $scope = new Scope(); - $breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('captureLastError') + ->willReturn('7565e130d1d14e639442110a6dd1cbab'); - $hub = new Hub(null, $scope); - $hub->addBreadcrumb($breadcrumb); + $hub = new Hub(); + + $this->assertNull($hub->captureLastError()); - $this->assertEmpty($scope->getBreadcrumbs()); + $hub->bindClient($client); + + $this->assertEquals('7565e130d1d14e639442110a6dd1cbab', $hub->captureLastError()); } public function testAddBreadcrumb(): void { - $client = ClientBuilder::create()->getClient(); - $hub = new Hub($client); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('getOptions') + ->willReturn(new Options()); + + $callbackInvoked = false; + $hub = new Hub(); $breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); $hub->addBreadcrumb($breadcrumb); + $hub->configureScope(function (Scope $scope): void { + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertEmpty($event->getBreadcrumbs()); + }); - $this->assertSame([$breadcrumb], $this->getScope($hub)->getBreadcrumbs()); + $hub->bindClient($client); + $hub->addBreadcrumb($breadcrumb); + $hub->configureScope(function (Scope $scope) use (&$callbackInvoked, $breadcrumb): void { + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame([$breadcrumb], $event->getBreadcrumbs()); + + $callbackInvoked = true; + }); + + $this->assertTrue($callbackInvoked); } public function testAddBreadcrumbDoesNothingIfMaxBreadcrumbsLimitIsZero(): void { - $client = ClientBuilder::create(['max_breadcrumbs' => 0])->getClient(); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('getOptions') + ->willReturn(new Options(['max_breadcrumbs' => 0])); + $hub = new Hub($client); $hub->addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting')); + $hub->configureScope(function (Scope $scope): void { + $event = $scope->applyToEvent(new Event(), []); - $this->assertEmpty($this->getScope($hub)->getBreadcrumbs()); + $this->assertNotNull($event); + $this->assertEmpty($event->getBreadcrumbs()); + }); } public function testAddBreadcrumbRespectsMaxBreadcrumbsLimit(): void { - $client = ClientBuilder::create(['max_breadcrumbs' => 2])->getClient(); - $hub = new Hub($client); - $scope = $this->getScope($hub); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->any()) + ->method('getOptions') + ->willReturn(new Options(['max_breadcrumbs' => 2])); + $hub = new Hub($client); $breadcrumb1 = new Breadcrumb(Breadcrumb::LEVEL_WARNING, Breadcrumb::TYPE_ERROR, 'error_reporting', 'foo'); $breadcrumb2 = new Breadcrumb(Breadcrumb::LEVEL_WARNING, Breadcrumb::TYPE_ERROR, 'error_reporting', 'bar'); $breadcrumb3 = new Breadcrumb(Breadcrumb::LEVEL_WARNING, Breadcrumb::TYPE_ERROR, 'error_reporting', 'baz'); @@ -244,61 +315,113 @@ public function testAddBreadcrumbRespectsMaxBreadcrumbsLimit(): void $hub->addBreadcrumb($breadcrumb1); $hub->addBreadcrumb($breadcrumb2); - $this->assertSame([$breadcrumb1, $breadcrumb2], $scope->getBreadcrumbs()); + $hub->configureScope(function (Scope $scope) use ($breadcrumb1, $breadcrumb2): void { + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame([$breadcrumb1, $breadcrumb2], $event->getBreadcrumbs()); + }); $hub->addBreadcrumb($breadcrumb3); - $this->assertSame([$breadcrumb2, $breadcrumb3], $scope->getBreadcrumbs()); + $hub->configureScope(function (Scope $scope) use ($breadcrumb2, $breadcrumb3): void { + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame([$breadcrumb2, $breadcrumb3], $event->getBreadcrumbs()); + }); } public function testAddBreadcrumbDoesNothingWhenBeforeBreadcrumbCallbackReturnsNull(): void { - $callback = function (): ?Breadcrumb { - return null; - }; - $client = ClientBuilder::create(['before_breadcrumb' => $callback])->getClient(); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('getOptions') + ->willReturn(new Options([ + 'before_breadcrumb' => static function () { + return null; + }, + ])); + $hub = new Hub($client); $hub->addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting')); + $hub->configureScope(function (Scope $scope): void { + $event = $scope->applyToEvent(new Event(), []); - $this->assertEmpty($this->getScope($hub)->getBreadcrumbs()); + $this->assertNotNull($event); + $this->assertEmpty($event->getBreadcrumbs()); + }); } public function testAddBreadcrumbStoresBreadcrumbReturnedByBeforeBreadcrumbCallback(): void { + $callbackInvoked = false; $breadcrumb1 = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); $breadcrumb2 = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); - $callback = function () use ($breadcrumb2): ?Breadcrumb { - return $breadcrumb2; - }; - $client = ClientBuilder::create(['before_breadcrumb' => $callback])->getClient(); + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('getOptions') + ->willReturn(new Options([ + 'before_breadcrumb' => static function () use ($breadcrumb2): Breadcrumb { + return $breadcrumb2; + }, + ])); + $hub = new Hub($client); $hub->addBreadcrumb($breadcrumb1); + $hub->configureScope(function (Scope $scope) use (&$callbackInvoked, $breadcrumb2): void { + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame([$breadcrumb2], $event->getBreadcrumbs()); + + $callbackInvoked = true; + }); - $this->assertSame([$breadcrumb2], $this->getScope($hub)->getBreadcrumbs()); + $this->assertTrue($callbackInvoked); } public function testCaptureEvent(): void { - /** @var ClientInterface|MockObject $client */ + /** @var ClientInterface&MockObject $client */ $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('captureEvent') ->with(['message' => 'test']) ->willReturn('2b867534eead412cbdb882fd5d441690'); - $hub = new Hub($client); + $hub = new Hub(); + + $this->assertNull($hub->captureEvent([])); + + $hub->bindClient($client); $this->assertEquals('2b867534eead412cbdb882fd5d441690', $hub->captureEvent(['message' => 'test'])); } - private function getScope(HubInterface $hub): Scope + public function testGetIntegration(): void { - $method = new \ReflectionMethod($hub, 'getScope'); - $method->setAccessible(true); + /** @var MockObject $integration */ + $integration = $this->createMock(IntegrationInterface::class); + + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('getIntegration') + ->with('Foo\\Bar') + ->willReturn($integration); + + $hub = new Hub(); + + $this->assertNull($hub->getIntegration('Foo\\Bar')); + + $hub->bindClient($client); - return $method->invoke($hub); + $this->assertSame($integration, $hub->getIntegration('Foo\\Bar')); } } diff --git a/tests/State/ScopeTest.php b/tests/State/ScopeTest.php index 33a9908d4..51a9ace52 100644 --- a/tests/State/ScopeTest.php +++ b/tests/State/ScopeTest.php @@ -15,62 +15,127 @@ final class ScopeTest extends TestCase public function testSetTag(): void { $scope = new Scope(); + $event = $scope->applyToEvent(new Event(), []); - $this->assertEquals([], $scope->getTags()); + $this->assertNotNull($event); + $this->assertTrue($event->getTagsContext()->isEmpty()); $scope->setTag('foo', 'bar'); $scope->setTag('bar', 'baz'); - $this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $scope->getTags()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $event->getTagsContext()->toArray()); + } + + public function setTags(): void + { + $scope = new Scope(); + $scope->setTags(['foo' => 'bar']); + + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo' => 'bar'], $event->getTagsContext()->toArray()); + + $scope->setTags(['bar' => 'baz']); + + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $event->getTagsContext()->toArray()); } public function testSetExtra(): void { $scope = new Scope(); + $event = $scope->applyToEvent(new Event(), []); - $this->assertEquals([], $scope->getExtra()); + $this->assertNotNull($event); + $this->assertTrue($event->getExtraContext()->isEmpty()); $scope->setExtra('foo', 'bar'); $scope->setExtra('bar', 'baz'); - $this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $scope->getExtra()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $event->getExtraContext()->toArray()); + } + + public function testSetExtras(): void + { + $scope = new Scope(); + $scope->setExtras(['foo' => 'bar']); + + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo' => 'bar'], $event->getExtraContext()->toArray()); + + $scope->setExtras(['bar' => 'baz']); + + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $event->getExtraContext()->toArray()); } public function testSetUser(): void { $scope = new Scope(); - $this->assertEquals([], $scope->getUser()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame([], $event->getUserContext()->toArray()); $scope->setUser(['foo' => 'bar']); - $this->assertEquals(['foo' => 'bar'], $scope->getUser()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo' => 'bar'], $event->getUserContext()->toArray()); $scope->setUser(['bar' => 'baz']); - $this->assertEquals(['bar' => 'baz'], $scope->getUser()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['bar' => 'baz'], $event->getUserContext()->toArray()); } public function testSetFingerprint(): void { $scope = new Scope(); + $event = $scope->applyToEvent(new Event(), []); - $this->assertEmpty($scope->getFingerprint()); + $this->assertNotNull($event); + $this->assertEmpty($event->getFingerprint()); $scope->setFingerprint(['foo', 'bar']); - $this->assertEquals(['foo', 'bar'], $scope->getFingerprint()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame(['foo', 'bar'], $event->getFingerprint()); } public function testSetLevel(): void { $scope = new Scope(); + $event = $scope->applyToEvent(new Event(), []); - $this->assertNull($scope->getLevel()); + $this->assertNotNull($event); + $this->assertEquals(Severity::error(), $event->getLevel()); $scope->setLevel(Severity::debug()); - $this->assertEquals(Breadcrumb::LEVEL_DEBUG, $scope->getLevel()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertEquals(Severity::debug(), $event->getLevel()); } public function testAddBreadcrumb(): void @@ -80,16 +145,45 @@ public function testAddBreadcrumb(): void $breadcrumb2 = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); $breadcrumb3 = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); - $this->assertEmpty($scope->getBreadcrumbs()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertEmpty($event->getBreadcrumbs()); $scope->addBreadcrumb($breadcrumb1); $scope->addBreadcrumb($breadcrumb2); - $this->assertSame([$breadcrumb1, $breadcrumb2], $scope->getBreadcrumbs()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame([$breadcrumb1, $breadcrumb2], $event->getBreadcrumbs()); $scope->addBreadcrumb($breadcrumb3, 2); - $this->assertSame([$breadcrumb2, $breadcrumb3], $scope->getBreadcrumbs()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertSame([$breadcrumb2, $breadcrumb3], $event->getBreadcrumbs()); + } + + public function testClearBreadcrumbs(): void + { + $scope = new Scope(); + + $scope->addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting')); + $scope->addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting')); + + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertNotEmpty($event->getBreadcrumbs()); + + $scope->clearBreadcrumbs(); + + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertEmpty($event->getBreadcrumbs()); } public function testAddEventProcessor(): void @@ -136,19 +230,36 @@ public function testAddEventProcessor(): void public function testClear(): void { $scope = new Scope(); - $scope->setLevel(Severity::error()); - $scope->addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting')); + $breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); + + $scope->setLevel(Severity::info()); + $scope->addBreadcrumb($breadcrumb); $scope->setFingerprint(['foo']); + $scope->setExtras(['foo' => 'bar']); + $scope->setTags(['bar' => 'foo']); + $scope->setUser(['foobar' => 'barfoo']); - $this->assertNotNull($scope->getLevel()); - $this->assertNotEmpty($scope->getBreadcrumbs()); - $this->assertNotEmpty($scope->getFingerprint()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertEquals(Severity::info(), $event->getLevel()); + $this->assertSame([$breadcrumb], $event->getBreadcrumbs()); + $this->assertSame(['foo'], $event->getFingerprint()); + $this->assertSame(['foo' => 'bar'], $event->getExtraContext()->toArray()); + $this->assertSame(['bar' => 'foo'], $event->getTagsContext()->toArray()); + $this->assertSame(['foobar' => 'barfoo'], $event->getUserContext()->toArray()); $scope->clear(); - $this->assertNull($scope->getLevel()); - $this->assertEmpty($scope->getBreadcrumbs()); - $this->assertEmpty($scope->getFingerprint()); + $event = $scope->applyToEvent(new Event(), []); + + $this->assertNotNull($event); + $this->assertEquals(Severity::error(), $event->getLevel()); + $this->assertEmpty($event->getBreadcrumbs()); + $this->assertEmpty($event->getFingerprint()); + $this->assertEmpty($event->getExtraContext()->toArray()); + $this->assertEmpty($event->getTagsContext()->toArray()); + $this->assertEmpty($event->getUserContext()->toArray()); } public function testApplyToEvent(): void