Skip to content

Commit

Permalink
Remove all the getters of the Scope class
Browse files Browse the repository at this point in the history
  • Loading branch information
ste93cry committed Jul 19, 2019
1 parent 01a1c93 commit ffa26c0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 114 deletions.
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
72 changes: 0 additions & 72 deletions src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ public function setTags(array $tags): self
return $this;
}

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

/**
* Sets a new information in the extra context.
*
Expand Down Expand Up @@ -139,18 +127,6 @@ public function setExtras(array $extras): self
return $this;
}

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

/**
* Sets the given data in the user context.
*
Expand All @@ -165,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 @@ -191,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 @@ -217,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 @@ -245,18 +185,6 @@ public function addBreadcrumb(Breadcrumb $breadcrumb, int $maxBreadcrumbs = 100)
return $this;
}

/**
* Gets the breadcrumbs.
*
* @return Breadcrumb[]
*
* @internal
*/
public function getBreadcrumbs(): array
{
return $this->breadcrumbs;
}

/**
* Clears all the breadcrumbs.
*
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
73 changes: 56 additions & 17 deletions tests/State/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,88 +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']);

$this->assertEquals(['foo' => 'bar'], $scope->getTags());
$event = $scope->applyToEvent(new Event(), []);

$this->assertNotNull($event);
$this->assertSame(['foo' => 'bar'], $event->getTagsContext()->toArray());

$scope->setTags(['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 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 setExtras(): void
{
$scope = new Scope();

$scope->setExtras(['foo' => 'bar']);

$this->assertEquals(['foo' => 'bar'], $scope->getExtra());
$event = $scope->applyToEvent(new Event(), []);

$this->assertNotNull($event);
$this->assertSame(['foo' => 'bar'], $event->getExtraContext()->toArray());

$scope->setExtras(['bar' => 'baz']);

$this->assertEquals(['bar' => 'baz'], $scope->getExtra());
$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
Expand Down

0 comments on commit ffa26c0

Please sign in to comment.