Skip to content

Commit 314faf3

Browse files
committed
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.
1 parent 494730f commit 314faf3

File tree

3 files changed

+215
-1
lines changed

3 files changed

+215
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"require": {
66
"php": ">=7.2",
77
"guzzlehttp/psr7": "^1.7|^2.0",
8-
"psr/http-message": "^1.0"
8+
"psr/http-message": "^1.0",
9+
"cloudevents/sdk-php": "^1.0"
910
},
1011
"suggest": {
1112
"google/cloud-storage": "Google Cloud Storage client library for storing and persisting objects. When included, the functions framework will register the gs:// stream wrapper."

src/CloudEventSdkCompliant.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2020 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Google\CloudFunctions;
20+
21+
use BadMethodCallException;
22+
use JsonSerializable;
23+
use CloudEvents\V1\CloudEventInterface;
24+
use DateTimeImmutable;
25+
use DateTimeInterface;
26+
27+
/**
28+
* @internal
29+
* Wraps a Google\CloudFunctions\CloudEvent to comply with
30+
* CloudEvents\V1\CloudEventInterface.
31+
*/
32+
class CloudEventSdkCompliant implements JsonSerializable, CloudEventInterface
33+
{
34+
private $cloudevent;
35+
36+
public function __construct(
37+
CloudEvent $cloudevent
38+
) {
39+
$this->cloudevent = $cloudevent;
40+
}
41+
42+
public function getId(): string
43+
{
44+
return $this->cloudevent->getId();
45+
}
46+
public function getSource(): string
47+
{
48+
return $this->cloudevent->getSource();
49+
}
50+
public function getSpecVersion(): string
51+
{
52+
return $this->cloudevent->getSpecVersion();
53+
}
54+
public function getType(): string
55+
{
56+
return $this->cloudevent->getType();
57+
}
58+
public function getDataContentType(): ?string
59+
{
60+
return $this->cloudevent->getDataContentType();
61+
}
62+
public function getDataSchema(): ?string
63+
{
64+
return $this->cloudevent->getDataSchema();
65+
}
66+
public function getSubject(): ?string
67+
{
68+
return $this->cloudevent->getSubject();
69+
}
70+
public function getTime(): ?DateTimeImmutable
71+
{
72+
return DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime());
73+
}
74+
public function getExtension(string $attribute)
75+
{
76+
throw new BadMethodCallException('getExtension() is not currently supported by Functions Framework PHP');
77+
}
78+
public function getExtensions(): array
79+
{
80+
throw new BadMethodCallException('getExtensions() is not currently supported by Functions Framework PHP');
81+
}
82+
/**
83+
* @return mixed
84+
*/
85+
public function getData()
86+
{
87+
return $this->cloudevent->getData();
88+
}
89+
90+
public function jsonSerialize()
91+
{
92+
return $this->cloudevent->jsonSerialize();
93+
}
94+
95+
public function __toString()
96+
{
97+
return $this->cloudevent->__toString();
98+
}
99+
}

tests/CloudEventSdkCompliantTest.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2021 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Google\CloudFunctions\Tests;
20+
21+
use BadMethodCallException;
22+
use Google\CloudFunctions\CloudEvent;
23+
use Google\CloudFunctions\CloudEventSdkCompliant;
24+
use PHPUnit\Framework\TestCase;
25+
use DateTimeImmutable;
26+
use DateTimeInterface;
27+
28+
/**
29+
* @group gcf-framework
30+
* @group newtest
31+
*/
32+
class CloudEventSdkCompliantTest extends TestCase
33+
{
34+
private CloudEvent $cloudevent;
35+
36+
function setUp(): void
37+
{
38+
$this->cloudevent = new CloudEvent(
39+
'1413058901901494',
40+
'//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC',
41+
'1.0',
42+
'com.google.cloud.pubsub.topic.publish',
43+
'application/json',
44+
'type.googleapis.com/google.logging.v2.LogEntry',
45+
'My Subject',
46+
'2020-12-08T20:03:19.162Z',
47+
[
48+
"message" => [
49+
"data" => "SGVsbG8gdGhlcmU=",
50+
"messageId" => "1408577928008405",
51+
"publishTime" => "2020-08-06T22:31:14.536Z"
52+
],
53+
"subscription" => "projects/MY-PROJECT/subscriptions/MY-SUB"
54+
]
55+
);
56+
}
57+
58+
public function testJsonSerialize(): void
59+
{
60+
61+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
62+
63+
$want = '{
64+
"id": "1413058901901494",
65+
"source": "\/\/pubsub.googleapis.com\/projects\/MY-PROJECT\/topics\/MY-TOPIC",
66+
"specversion": "1.0",
67+
"type": "com.google.cloud.pubsub.topic.publish",
68+
"datacontenttype": "application\/json",
69+
"dataschema": "type.googleapis.com\/google.logging.v2.LogEntry",
70+
"subject": "My Subject",
71+
"time": "2020-12-08T20:03:19.162Z",
72+
"data": {
73+
"message": {
74+
"data": "SGVsbG8gdGhlcmU=",
75+
"messageId": "1408577928008405",
76+
"publishTime": "2020-08-06T22:31:14.536Z"
77+
},
78+
"subscription": "projects\\/MY-PROJECT\\/subscriptions\\/MY-SUB"
79+
}
80+
}';
81+
82+
$this->assertSame($want, json_encode($wrappedEvent, JSON_PRETTY_PRINT));
83+
}
84+
85+
public function testWrapsCloudEvent(): void
86+
{
87+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
88+
89+
$this->assertSame($this->cloudevent->getId(), $wrappedEvent->getId());
90+
$this->assertSame($this->cloudevent->getSource(), $wrappedEvent->getSource());
91+
$this->assertSame($this->cloudevent->getType(), $wrappedEvent->getType());
92+
$this->assertSame($this->cloudevent->getData(), $wrappedEvent->getData());
93+
$this->assertSame($this->cloudevent->getDataContentType(), $wrappedEvent->getDataContentType());
94+
$this->assertSame($this->cloudevent->getDataSchema(), $wrappedEvent->getDataSchema());
95+
$this->assertSame($this->cloudevent->getSubject(), $wrappedEvent->getSubject());
96+
$this->assertEquals(DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime()), $wrappedEvent->getTime());
97+
}
98+
99+
public function testUnimplementedGetExtensionThrowsError(): void
100+
{
101+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
102+
$this->expectException(BadMethodCallException::class);
103+
104+
$wrappedEvent->getExtension('attribute');
105+
}
106+
107+
public function testUnimplementedGetExtensionsThrowsError(): void
108+
{
109+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
110+
$this->expectException(BadMethodCallException::class);
111+
112+
$wrappedEvent->getExtensions();
113+
}
114+
}

0 commit comments

Comments
 (0)