Skip to content

Commit

Permalink
Add named constructor to the Event class
Browse files Browse the repository at this point in the history
  • Loading branch information
ste93cry committed Sep 10, 2020
1 parent 53e8aee commit d65ef99
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 81 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### Unreleased

- [BC BREAK] Add named constructors to the `Event` class (#1085)

## 3.0.0-beta1 (2020-09-03)

**Tracing API**
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@
- Removed the `Frame::toArray()` and `Frame::jsonSerialize()` methods
- Removed the `Stacktrace::toArray()` and `Stacktrace::jsonSerialize()` methods
- Removed the `SpoolTransport` class and the `SpoolInterface` interface with related implementation
- Made the `Event::__construct()` method `private`, use the named constructors instead
27 changes: 21 additions & 6 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,32 @@ final class Event
*/
private $type;

private function __construct(?EventId $eventId, EventType $eventType)
{
$this->id = $eventId ?? EventId::generate();
$this->timestamp = gmdate('Y-m-d\TH:i:s\Z');
$this->sdkVersion = PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion();
$this->type = $eventType;
}

/**
* Class constructor.
* Creates a new event.
*
* @param EventId|null $eventId The ID of the event
*/
public function __construct(?EventId $eventId = null)
public static function createEvent(?EventId $eventId = null): self
{
$this->id = $eventId ?? EventId::generate();
$this->timestamp = gmdate('Y-m-d\TH:i:s\Z');
$this->sdkVersion = PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion();
$this->type = EventType::default();
return new self($eventId, EventType::default());
}

/**
* Creates a new transaction event.
*
* @param EventId|null $eventId The ID of the event
*/
public static function createTransaction(EventId $eventId = null): self
{
return new self($eventId, EventType::transaction());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function create($payload): Event
if ($payload instanceof Event) {
$event = $payload;
} else {
$event = new Event();
$event = Event::createEvent();

if (isset($payload['logger'])) {
$event->setLogger($payload['logger']);
Expand Down
4 changes: 1 addition & 3 deletions src/Tracing/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Sentry\Event;
use Sentry\EventId;
use Sentry\EventType;
use Sentry\Severity;
use Sentry\State\HubInterface;

Expand Down Expand Up @@ -90,8 +89,7 @@ public function finish(?float $endTimestamp = null): ?EventId
*/
public function toEvent(): Event
{
$event = new Event();
$event->setType(EventType::transaction());
$event = Event::createTransaction();
$event->setTags(array_merge($event->getTags(), $this->tags));
$event->setTransaction($this->name);
$event->setStartTimestamp($this->startTimestamp);
Expand Down
8 changes: 4 additions & 4 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ final class EventTest extends TestCase
{
public function testEventIsGeneratedWithUniqueIdentifier(): void
{
$event1 = new Event();
$event2 = new Event();
$event1 = Event::createEvent();
$event2 = Event::createEvent();

$this->assertNotEquals($event1->getId(), $event2->getId());
}
Expand All @@ -26,7 +26,7 @@ public function testEventIsGeneratedWithUniqueIdentifier(): void
*/
public function testGetMessage(array $setMessageArguments, array $expectedValue): void
{
$event = new Event();
$event = Event::createEvent();

\call_user_func_array([$event, 'setMessage'], $setMessageArguments);

Expand Down Expand Up @@ -80,7 +80,7 @@ public function testGettersAndSetters(string $propertyName, $propertyValue): voi
$getterMethod = 'get' . ucfirst($propertyName);
$setterMethod = 'set' . ucfirst($propertyName);

$event = new Event();
$event = Event::createEvent();
$event->$setterMethod($propertyValue);

$this->assertEquals($event->$getterMethod(), $propertyValue);
Expand Down
2 changes: 1 addition & 1 deletion tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function testAddBreadcrumb(): void

addBreadcrumb($breadcrumb);
configureScope(function (Scope $scope) use ($breadcrumb): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertSame([$breadcrumb], $event->getBreadcrumbs());
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/EnvironmentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testInvoke(bool $isIntegrationEnabled, ?RuntimeContext $initialR
SentrySdk::getCurrentHub()->bindClient($client);

withScope(function (Scope $scope) use ($expectedRuntimeContext, $expectedOsContext, $initialRuntimeContext, $initialOsContext): void {
$event = new Event();
$event = Event::createEvent();
$event->setRuntimeContext($initialRuntimeContext);
$event->setOsContext($initialOsContext);

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/FrameContextifierIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testInvoke(string $fixtureFilePath, int $lineNumber, int $contex
new Frame('[unknown]', $fixtureFilePath, $lineNumber, null, $fixtureFilePath),
]);

$event = new Event();
$event = Event::createEvent();
$event->setStacktrace($stacktrace);

withScope(static function (Scope $scope) use (&$event): void {
Expand Down Expand Up @@ -157,7 +157,7 @@ public function testInvokeLogsWarningMessageIfSourceCodeExcerptCannotBeRetrieved
new Frame(null, 'file.ext', 10, null, 'file.ext'),
]);

$event = new Event();
$event = Event::createEvent();
$event->setStacktrace($stacktrace);

withScope(static function (Scope $scope) use (&$event): void {
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/IgnoreErrorsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,31 @@ public function testInvoke(Event $event, bool $isIntegrationEnabled, array $inte

public function invokeDataProvider(): \Generator
{
$event = new Event();
$event = Event::createEvent();
$event->setExceptions([new ExceptionDataBag(new \RuntimeException())]);

yield 'Integration disabled' => [
new Event(),
Event::createEvent(),
false,
[
'ignore_exceptions' => [],
],
false,
];

$event = new Event();
$event = Event::createEvent();
$event->setExceptions([new ExceptionDataBag(new \RuntimeException())]);

yield 'No exceptions to check' => [
new Event(),
Event::createEvent(),
true,
[
'ignore_exceptions' => [],
],
false,
];

$event = new Event();
$event = Event::createEvent();
$event->setExceptions([new ExceptionDataBag(new \RuntimeException())]);

yield 'The exception is matching exactly the "ignore_exceptions" option' => [
Expand All @@ -83,7 +83,7 @@ public function invokeDataProvider(): \Generator
true,
];

$event = new Event();
$event = Event::createEvent();
$event->setExceptions([new ExceptionDataBag(new \RuntimeException())]);

yield 'The exception is matching the "ignore_exceptions" option' => [
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/ModulesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testInvoke(bool $isIntegrationEnabled, bool $expectedEmptyModule
SentrySdk::getCurrentHub()->bindClient($client);

withScope(function (Scope $scope) use ($expectedEmptyModules): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/RequestIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class RequestIntegrationTest extends TestCase
*/
public function testInvoke(array $options, ServerRequestInterface $request, array $expectedRequestContextData, ?UserDataBag $initialUser, ?UserDataBag $expectedUser): void
{
$event = new Event();
$event = Event::createEvent();
$event->setUser($initialUser);

$integration = new RequestIntegration($this->createRequestFetcher($request));
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/TransactionIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,30 @@ public function testSetupOnce(Event $event, bool $isIntegrationEnabled, array $p
public function setupOnceDataProvider(): \Generator
{
yield [
new Event(),
Event::createEvent(),
true,
[],
[],
null,
];

yield [
new Event(),
Event::createEvent(),
true,
['transaction' => '/foo/bar'],
[],
'/foo/bar',
];

yield [
new Event(),
Event::createEvent(),
true,
[],
['PATH_INFO' => '/foo/bar'],
'/foo/bar',
];

$event = new Event();
$event = Event::createEvent();
$event->setTransaction('/foo/bar');

yield [
Expand All @@ -80,7 +80,7 @@ public function setupOnceDataProvider(): \Generator
'/foo/bar',
];

$event = new Event();
$event = Event::createEvent();
$event->setTransaction('/foo/bar');

yield [
Expand All @@ -92,7 +92,7 @@ public function setupOnceDataProvider(): \Generator
];

yield [
new Event(),
Event::createEvent(),
false,
['transaction' => '/foo/bar'],
[],
Expand Down
2 changes: 1 addition & 1 deletion tests/Monolog/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testHandle(array $record, array $expectedPayload, array $expecte
$client->expects($this->once())
->method('captureEvent')
->with($expectedPayload, $this->callback(function (Scope $scopeArg) use ($expectedExtra): bool {
$event = $scopeArg->applyToEvent(new Event(), []);
$event = $scopeArg->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertSame($expectedExtra, $event->getExtra());
Expand Down
14 changes: 6 additions & 8 deletions tests/Serializer/PayloadSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Sentry\Context\RuntimeContext;
use Sentry\Event;
use Sentry\EventId;
use Sentry\EventType;
use Sentry\ExceptionDataBag;
use Sentry\ExceptionMechanism;
use Sentry\Frame;
Expand Down Expand Up @@ -62,7 +61,7 @@ public function serializeDataProvider(): iterable
$sdkVersion = PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion();

yield [
new Event(new EventId('fc9442f5aef34234bb22b9a615e30ccd')),
Event::createEvent(new EventId('fc9442f5aef34234bb22b9a615e30ccd')),
<<<JSON
{
"event_id": "fc9442f5aef34234bb22b9a615e30ccd",
Expand All @@ -78,7 +77,7 @@ public function serializeDataProvider(): iterable
true,
];

$event = new Event(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event = Event::createEvent(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event->setLevel(Severity::error());
$event->setLogger('app.php');
$event->setTransaction('/users/<username>/');
Expand Down Expand Up @@ -316,7 +315,7 @@ public function serializeDataProvider(): iterable
true,
];

$event = new Event(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event = Event::createEvent(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event->setMessage('My raw message with interpreted strings like this', []);

yield [
Expand All @@ -337,7 +336,7 @@ public function serializeDataProvider(): iterable
true,
];

$event = new Event(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event = Event::createEvent(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event->setMessage('My raw message with interpreted strings like %s', ['this']);

yield [
Expand All @@ -362,7 +361,7 @@ public function serializeDataProvider(): iterable
true,
];

$event = new Event(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event = Event::createEvent(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event->setMessage('My raw message with interpreted strings like %s', ['this'], 'My raw message with interpreted strings like that');

yield [
Expand Down Expand Up @@ -409,8 +408,7 @@ public function serializeDataProvider(): iterable

$span2->finish(1598659060);

$event = new Event(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event->setType(EventType::transaction());
$event = Event::createTransaction(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event->setSpans([$span1, $span2]);

yield [
Expand Down
14 changes: 7 additions & 7 deletions tests/State/HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function testAddBreadcrumb(): void

$hub->addBreadcrumb($breadcrumb);
$hub->configureScope(function (Scope $scope): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertEmpty($event->getBreadcrumbs());
Expand All @@ -298,7 +298,7 @@ public function testAddBreadcrumb(): void
$hub->bindClient($client);
$hub->addBreadcrumb($breadcrumb);
$hub->configureScope(function (Scope $scope) use (&$callbackInvoked, $breadcrumb): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertSame([$breadcrumb], $event->getBreadcrumbs());
Expand All @@ -321,7 +321,7 @@ public function testAddBreadcrumbDoesNothingIfMaxBreadcrumbsLimitIsZero(): void

$hub->addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'));
$hub->configureScope(function (Scope $scope): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertEmpty($event->getBreadcrumbs());
Expand All @@ -345,7 +345,7 @@ public function testAddBreadcrumbRespectsMaxBreadcrumbsLimit(): void
$hub->addBreadcrumb($breadcrumb2);

$hub->configureScope(function (Scope $scope) use ($breadcrumb1, $breadcrumb2): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertSame([$breadcrumb1, $breadcrumb2], $event->getBreadcrumbs());
Expand All @@ -354,7 +354,7 @@ public function testAddBreadcrumbRespectsMaxBreadcrumbsLimit(): void
$hub->addBreadcrumb($breadcrumb3);

$hub->configureScope(function (Scope $scope) use ($breadcrumb2, $breadcrumb3): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertSame([$breadcrumb2, $breadcrumb3], $event->getBreadcrumbs());
Expand All @@ -377,7 +377,7 @@ public function testAddBreadcrumbDoesNothingWhenBeforeBreadcrumbCallbackReturnsN

$hub->addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'));
$hub->configureScope(function (Scope $scope): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertEmpty($event->getBreadcrumbs());
Expand All @@ -404,7 +404,7 @@ public function testAddBreadcrumbStoresBreadcrumbReturnedByBeforeBreadcrumbCallb

$hub->addBreadcrumb($breadcrumb1);
$hub->configureScope(function (Scope $scope) use (&$callbackInvoked, $breadcrumb2): void {
$event = $scope->applyToEvent(new Event(), []);
$event = $scope->applyToEvent(Event::createEvent(), []);

$this->assertNotNull($event);
$this->assertSame([$breadcrumb2], $event->getBreadcrumbs());
Expand Down

0 comments on commit d65ef99

Please sign in to comment.