-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add class that wraps CloudEvent into CloudEventInterface (#113
) * refactor: add class that wraps CloudEvent into CloudEventInterface This is groundwork to enabling function signatures that expect the official PHP CloudEvent SDKs [CloudEventInterface](https://github.com/cloudevents/sdk-php/blob/master/src/V1/CloudEventInterface.php) instead of the Function Framework's hand-rolled class. * Remove PHP 7.2 and 7.3 testing * Remove PHP 7.2 and 7.3 from unit testing workflow * Require PHP >=7.4 for Functions Framework
- Loading branch information
Showing
5 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2020 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\CloudFunctions; | ||
|
||
use BadMethodCallException; | ||
use JsonSerializable; | ||
use CloudEvents\V1\CloudEventInterface; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
|
||
/** | ||
* @internal | ||
* Wraps a Google\CloudFunctions\CloudEvent to comply with | ||
* CloudEvents\V1\CloudEventInterface. | ||
*/ | ||
class CloudEventSdkCompliant implements JsonSerializable, CloudEventInterface | ||
{ | ||
private $cloudevent; | ||
|
||
public function __construct( | ||
CloudEvent $cloudevent | ||
) { | ||
$this->cloudevent = $cloudevent; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->cloudevent->getId(); | ||
} | ||
public function getSource(): string | ||
{ | ||
return $this->cloudevent->getSource(); | ||
} | ||
public function getSpecVersion(): string | ||
{ | ||
return $this->cloudevent->getSpecVersion(); | ||
} | ||
public function getType(): string | ||
{ | ||
return $this->cloudevent->getType(); | ||
} | ||
public function getDataContentType(): ?string | ||
{ | ||
return $this->cloudevent->getDataContentType(); | ||
} | ||
public function getDataSchema(): ?string | ||
{ | ||
return $this->cloudevent->getDataSchema(); | ||
} | ||
public function getSubject(): ?string | ||
{ | ||
return $this->cloudevent->getSubject(); | ||
} | ||
public function getTime(): ?DateTimeImmutable | ||
{ | ||
return DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime()); | ||
} | ||
public function getExtension(string $attribute) | ||
{ | ||
throw new BadMethodCallException('getExtension() is not currently supported by Functions Framework PHP'); | ||
} | ||
public function getExtensions(): array | ||
{ | ||
throw new BadMethodCallException('getExtensions() is not currently supported by Functions Framework PHP'); | ||
} | ||
/** | ||
* @return mixed | ||
*/ | ||
public function getData() | ||
{ | ||
return $this->cloudevent->getData(); | ||
} | ||
|
||
public function jsonSerialize() | ||
{ | ||
return $this->cloudevent->jsonSerialize(); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->cloudevent->__toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2021 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\CloudFunctions\Tests; | ||
|
||
use BadMethodCallException; | ||
use Google\CloudFunctions\CloudEvent; | ||
use Google\CloudFunctions\CloudEventSdkCompliant; | ||
use PHPUnit\Framework\TestCase; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
|
||
/** | ||
* @group gcf-framework | ||
*/ | ||
class CloudEventSdkCompliantTest extends TestCase | ||
{ | ||
private CloudEvent $cloudevent; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->cloudevent = new CloudEvent( | ||
'1413058901901494', | ||
'//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC', | ||
'1.0', | ||
'com.google.cloud.pubsub.topic.publish', | ||
'application/json', | ||
'type.googleapis.com/google.logging.v2.LogEntry', | ||
'My Subject', | ||
'2020-12-08T20:03:19.162Z', | ||
[ | ||
"message" => [ | ||
"data" => "SGVsbG8gdGhlcmU=", | ||
"messageId" => "1408577928008405", | ||
"publishTime" => "2020-08-06T22:31:14.536Z" | ||
], | ||
"subscription" => "projects/MY-PROJECT/subscriptions/MY-SUB" | ||
] | ||
); | ||
} | ||
|
||
public function testJsonSerialize(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
|
||
$want = '{ | ||
"id": "1413058901901494", | ||
"source": "\/\/pubsub.googleapis.com\/projects\/MY-PROJECT\/topics\/MY-TOPIC", | ||
"specversion": "1.0", | ||
"type": "com.google.cloud.pubsub.topic.publish", | ||
"datacontenttype": "application\/json", | ||
"dataschema": "type.googleapis.com\/google.logging.v2.LogEntry", | ||
"subject": "My Subject", | ||
"time": "2020-12-08T20:03:19.162Z", | ||
"data": { | ||
"message": { | ||
"data": "SGVsbG8gdGhlcmU=", | ||
"messageId": "1408577928008405", | ||
"publishTime": "2020-08-06T22:31:14.536Z" | ||
}, | ||
"subscription": "projects\\/MY-PROJECT\\/subscriptions\\/MY-SUB" | ||
} | ||
}'; | ||
|
||
$this->assertSame($want, json_encode($wrappedEvent, JSON_PRETTY_PRINT)); | ||
} | ||
|
||
public function testWrapsCloudEvent(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
|
||
$this->assertSame($this->cloudevent->getId(), $wrappedEvent->getId()); | ||
$this->assertSame($this->cloudevent->getSource(), $wrappedEvent->getSource()); | ||
$this->assertSame($this->cloudevent->getType(), $wrappedEvent->getType()); | ||
$this->assertSame($this->cloudevent->getData(), $wrappedEvent->getData()); | ||
$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()); | ||
} | ||
|
||
public function testUnimplementedGetExtensionThrowsError(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
$this->expectException(BadMethodCallException::class); | ||
|
||
$wrappedEvent->getExtension('attribute'); | ||
} | ||
|
||
public function testUnimplementedGetExtensionsThrowsError(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
$this->expectException(BadMethodCallException::class); | ||
|
||
$wrappedEvent->getExtensions(); | ||
} | ||
} |