Skip to content

feat: change CloudEvent time property from string to object (#66) #169

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions src/CloudEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace Google\CloudFunctions;

use DateTimeImmutable;
use DateTimeInterface;
use JsonSerializable;

class CloudEvent implements JsonSerializable
Expand All @@ -32,6 +34,9 @@ class CloudEvent implements JsonSerializable
private $datacontenttype;
private $dataschema;
private $subject;
/**
* @var DateTimeImmutable|null
*/
private $time;
/**
* @var mixed $data
Expand All @@ -56,7 +61,7 @@ public function __construct(
$this->datacontenttype = $datacontenttype;
$this->dataschema = $dataschema;
$this->subject = $subject;
$this->time = $time;
$this->time = DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $time) ?: null;
$this->data = $data;
}

Expand Down Expand Up @@ -88,7 +93,7 @@ public function getSubject(): ?string
{
return $this->subject;
}
public function getTime(): ?string
public function getTime(): ?DateTimeImmutable
{
return $this->time;
}
Expand Down Expand Up @@ -132,7 +137,7 @@ public function jsonSerialize()
'datacontenttype' => $this->datacontenttype,
'dataschema' => $this->dataschema,
'subject' => $this->subject,
'time' => $this->time,
'time' => $this->time->format(DateTimeInterface::RFC3339_EXTENDED),
'data' => $this->data,
];
}
Expand All @@ -148,7 +153,7 @@ public function __toString()
"- datacontenttype: $this->datacontenttype",
"- dataschema: $this->dataschema",
"- subject: $this->subject",
"- time: $this->time",
"- time: {$this->time->format(DateTimeInterface::RFC3339_EXTENDED)}",
]);
return $output;
}
Expand Down
6 changes: 1 addition & 5 deletions src/CloudEventSdkCompliant.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ public function getSubject(): ?string
}
public function getTime(): ?DateTimeImmutable
{
$time = DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime());
if ($time === false) {
return null;
}
return $time;
return $this->cloudevent->getTime();
}
public function getExtension(string $attribute)
{
Expand Down
4 changes: 3 additions & 1 deletion src/LegacyEventMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace Google\CloudFunctions;

use DateTime;
use DateTimeInterface;
use RuntimeException;

class LegacyEventMapper
Expand Down Expand Up @@ -193,7 +195,7 @@ private function convertRawPubsubPayload(array $jsonData, string $requestUriPath
if (array_key_exists('publishTime', $jsonData['message'])) {
$timestamp = $jsonData['message']['publishTime'];
} else {
$timestamp = gmdate('%Y-%m-%dT%H:%M:%S.%6NZ');
$timestamp = gmdate('Y-m-d\TH:i:s.v\Z');
}

return [
Expand Down
5 changes: 3 additions & 2 deletions tests/CloudEventFunctionsWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Google\CloudFunctions\Tests;

use DateTimeInterface;
use Google\CloudFunctions\CloudEventFunctionWrapper;
use Google\CloudFunctions\CloudEvent;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -239,7 +240,7 @@ public function invokeThis(CloudEvent $cloudevent): void
$this->assertSame('application/json', $cloudevent->getDataContentType());
$this->assertSame('type.googleapis.com/google.logging.v2.LogEntry', $cloudevent->getDataSchema());
$this->assertSame('My Subject', $cloudevent->getSubject());
$this->assertSame('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
$this->assertSame('2020-12-08T20:03:19.162+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));
}

public function testWithNotFullButValidCloudEvent(): void
Expand Down Expand Up @@ -306,7 +307,7 @@ public function invokeThisLegacy(CloudEvent $cloudevent): void
$this->assertSame('application/json', $cloudevent->getDataContentType());
$this->assertNull($cloudevent->getDataSchema());
$this->assertNull($cloudevent->getSubject());
$this->assertSame('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
$this->assertSame('2020-12-08T20:03:19.162+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));
}

public function testFromStructuredEventRequest(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/CloudEventSdkCompliantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testJsonSerialize(): void
"datacontenttype": "application\/json",
"dataschema": "type.googleapis.com\/google.logging.v2.LogEntry",
"subject": "My Subject",
"time": "2020-12-08T20:03:19.162Z",
"time": "2020-12-08T20:03:19.162+00:00",
"data": {
"message": {
"data": "SGVsbG8gdGhlcmU=",
Expand All @@ -91,7 +91,7 @@ public function testWrapsCloudEvent(): void
$this->assertSame($this->cloudevent->getDataContentType(), $wrappedEvent->getDataContentType());
$this->assertSame($this->cloudevent->getDataSchema(), $wrappedEvent->getDataSchema());
$this->assertSame($this->cloudevent->getSubject(), $wrappedEvent->getSubject());
$this->assertEquals(DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime()), $wrappedEvent->getTime());
$this->assertSame($this->cloudevent->getTime(), $wrappedEvent->getTime());
}

public function testUnimplementedGetExtensionThrowsError(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/CloudEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testJsonSerialize(): void
"datacontenttype": "application\/json",
"dataschema": "type.googleapis.com\/google.logging.v2.LogEntry",
"subject": "My Subject",
"time": "2020-12-08T20:03:19.162Z",
"time": "2020-12-08T20:03:19.162+00:00",
"data": {
"message": {
"data": "SGVsbG8gdGhlcmU=",
Expand Down
24 changes: 13 additions & 11 deletions tests/LegacyEventMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Google\CloudFunctions\Tests;

use DateTimeInterface;
use Google\CloudFunctions\LegacyEventMapper;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -93,7 +94,7 @@ public function testWithoutContextProperty(): void
$this->assertSame('application/json', $cloudevent->getDataContentType());
$this->assertNull($cloudevent->getDataSchema());
$this->assertNull($cloudevent->getSubject());
$this->assertSame('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
$this->assertSame('2020-12-08T20:03:19.162+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));

// Verify Pub/Sub-specific data transformation.
$message = $cloudevent->getData()['message'];
Expand Down Expand Up @@ -128,8 +129,8 @@ public function testRawPubsubNoPath(): void
);
$this->assertNull($cloudevent->getSubject());
$this->assertEqualsWithDelta(
strtotime(gmdate('%Y-%m-%dT%H:%M:%S.%6NZ')),
strtotime($cloudevent->getTime()),
strtotime(gmdate(DateTimeInterface::RFC3339_EXTENDED)),
strtotime($cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED)),
1
);
$this->assertSame(
Expand Down Expand Up @@ -168,8 +169,8 @@ public function testRawPubsubWithPath(): void
);
$this->assertNull($cloudevent->getSubject());
$this->assertEqualsWithDelta(
strtotime(gmdate('%Y-%m-%dT%H:%M:%S.%6NZ')),
strtotime($cloudevent->getTime()),
strtotime(gmdate(DateTimeInterface::RFC3339_EXTENDED)),
strtotime($cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED)),
1
);
$this->assertSame(
Expand Down Expand Up @@ -207,7 +208,7 @@ public function testResourceAsString(): void
$this->assertSame('application/json', $cloudevent->getDataContentType());
$this->assertNull($cloudevent->getDataSchema());
$this->assertNull($cloudevent->getSubject());
$this->assertSame('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
$this->assertSame('2020-12-08T20:03:19.162+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));

// Verify Pub/Sub-specific data transformation.
$message = $cloudevent->getData()['message'];
Expand Down Expand Up @@ -249,7 +250,7 @@ public function testCloudStorage(): void
'objects/MyFile#1588778055917163',
$cloudevent->getSubject()
);
$this->assertSame('2020-12-08T20:03:19.162Z', $cloudevent->getTime());
$this->assertSame('2020-12-08T20:03:19.162+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));
$this->assertSame('foo', $cloudevent->getData());
}

Expand Down Expand Up @@ -296,7 +297,8 @@ public function testFirebaseAuth(): void
'users/UUpby3s4spZre6kHsgVSPetzQ8l2',
$cloudevent->getSubject()
);
$this->assertSame('2020-09-29T11:32:00.000Z', $cloudevent->getTime());
var_dump($cloudevent->getTime());
$this->assertSame('2020-09-29T11:32:00.000+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));
$this->assertSame('2020-05-26T10:42:27Z', $cloudevent->getData()['metadata']['createTime']);
$this->assertSame('2020-10-24T11:00:00Z', $cloudevent->getData()['metadata']['lastSignInTime']);
}
Expand Down Expand Up @@ -341,7 +343,7 @@ public function testFirebaseAuthDbDelete(): void
'refs/gcf-test/xyz',
$cloudevent->getSubject()
);
$this->assertSame('2020-05-21T11:53:45.337Z', $cloudevent->getTime());
$this->assertSame('2020-05-21T11:53:45.337+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));
}

public function testFirebaseAuthDbDeleteWithAlternateDomain(): void
Expand Down Expand Up @@ -384,7 +386,7 @@ public function testFirebaseAuthDbDeleteWithAlternateDomain(): void
'refs/gcf-test/xyz',
$cloudevent->getSubject()
);
$this->assertSame('2020-05-21T11:53:45.337Z', $cloudevent->getTime());
$this->assertSame('2020-05-21T11:53:45.337+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));
}

public function testFirebaseAuthDbDeleteWithInvalidDomain(): void
Expand Down Expand Up @@ -424,6 +426,6 @@ public function testFirebaseAuthDbDeleteWithInvalidDomain(): void
$this->assertSame('application/json', $cloudevent->getDataContentType());
$this->assertNull($cloudevent->getDataSchema());
$this->assertNull($cloudevent->getSubject());
$this->assertSame('2020-05-21T11:53:45.337Z', $cloudevent->getTime());
$this->assertSame('2020-05-21T11:53:45.337+00:00', $cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED));
}
}
2 changes: 1 addition & 1 deletion tests/conformance/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function fixCloudEventData(CloudEvent $cloudevent): CloudEvent
$cloudevent->getDatacontenttype(),
$cloudevent->getDataschema(),
$cloudevent->getSubject(),
$cloudevent->getTime(),
$cloudevent->getTime()->format(DateTimeInterface::RFC3339_EXTENDED),
$data
);
}
Expand Down