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

Add functions to clear breadcrumbs and bulk set tags and extras in the scope #852

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
72 changes: 20 additions & 52 deletions src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
* @param array<string, string> $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;
}

/**
Expand All @@ -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<string, mixed>
* @param array<string, mixed> $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;
}

/**
Expand All @@ -137,18 +141,6 @@ public function setUser(array $data): self
return $this;
}

/**
* Gets the information contained in the user context.
*
* @return array<string, mixed>
*
* @internal
*/
public function getUser(): array
{
return $this->user->toArray();
}

/**
* Sets the list of strings used to dictate the deduplication of this event.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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;
}

/**
Expand Down
34 changes: 14 additions & 20 deletions tests/Monolog/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,36 @@
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;
use Sentry\State\Scope;

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);
}

Expand Down Expand Up @@ -313,7 +306,7 @@ public function handleDataProvider(): \Generator
[
'monolog.channel' => 'channel.foo',
'monolog.level' => Logger::getLevelName(Logger::INFO),
'1' => 'numeric key',
'0' => 'numeric key',
],
[],
];
Expand All @@ -332,6 +325,7 @@ public function handleDataProvider(): \Generator
[
'level' => Severity::debug(),
'message' => 'foo bar',
'logger' => 'monolog.channel.foo',
'transaction' => 'Foo transaction',
],
[
Expand Down
20 changes: 15 additions & 5 deletions tests/SdkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading