Skip to content

Commit 7fc7a2a

Browse files
authored
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
1 parent 015260c commit 7fc7a2a

File tree

5 files changed

+217
-5
lines changed

5 files changed

+217
-5
lines changed

.github/workflows/conformance.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
php-version: [ '7.2', '7.3', '7.4','8.0' ]
15+
php-version: [ '7.4','8.0' ]
1616
name: PHP ${{ matrix.php-version }} Conformance Test
1717
steps:
1818
- name: Checkout code

.github/workflows/unit.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
operating-system: [ubuntu-latest]
13-
php-versions: [ '7.2', '7.3', '7.4','8.0' ]
13+
php-versions: [ '7.4','8.0' ]
1414
name: PHP ${{ matrix.php-versions }} Unit Test
1515
steps:
1616
- name: Checkout

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"description": "Google Cloud Functions Framework for PHP",
44
"license": "Apache-2.0",
55
"require": {
6-
"php": ">=7.2",
6+
"php": ">=7.4",
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."
@@ -16,7 +17,7 @@
1617
}
1718
},
1819
"require-dev": {
19-
"phpunit/phpunit": "^7.0|^8.0",
20+
"phpunit/phpunit": "^7.5|^8.0",
2021
"guzzlehttp/guzzle": "^7.2"
2122
},
2223
"bin": [

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

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
*/
31+
class CloudEventSdkCompliantTest extends TestCase
32+
{
33+
private CloudEvent $cloudevent;
34+
35+
public function setUp(): void
36+
{
37+
$this->cloudevent = new CloudEvent(
38+
'1413058901901494',
39+
'//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC',
40+
'1.0',
41+
'com.google.cloud.pubsub.topic.publish',
42+
'application/json',
43+
'type.googleapis.com/google.logging.v2.LogEntry',
44+
'My Subject',
45+
'2020-12-08T20:03:19.162Z',
46+
[
47+
"message" => [
48+
"data" => "SGVsbG8gdGhlcmU=",
49+
"messageId" => "1408577928008405",
50+
"publishTime" => "2020-08-06T22:31:14.536Z"
51+
],
52+
"subscription" => "projects/MY-PROJECT/subscriptions/MY-SUB"
53+
]
54+
);
55+
}
56+
57+
public function testJsonSerialize(): void
58+
{
59+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
60+
61+
$want = '{
62+
"id": "1413058901901494",
63+
"source": "\/\/pubsub.googleapis.com\/projects\/MY-PROJECT\/topics\/MY-TOPIC",
64+
"specversion": "1.0",
65+
"type": "com.google.cloud.pubsub.topic.publish",
66+
"datacontenttype": "application\/json",
67+
"dataschema": "type.googleapis.com\/google.logging.v2.LogEntry",
68+
"subject": "My Subject",
69+
"time": "2020-12-08T20:03:19.162Z",
70+
"data": {
71+
"message": {
72+
"data": "SGVsbG8gdGhlcmU=",
73+
"messageId": "1408577928008405",
74+
"publishTime": "2020-08-06T22:31:14.536Z"
75+
},
76+
"subscription": "projects\\/MY-PROJECT\\/subscriptions\\/MY-SUB"
77+
}
78+
}';
79+
80+
$this->assertSame($want, json_encode($wrappedEvent, JSON_PRETTY_PRINT));
81+
}
82+
83+
public function testWrapsCloudEvent(): void
84+
{
85+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
86+
87+
$this->assertSame($this->cloudevent->getId(), $wrappedEvent->getId());
88+
$this->assertSame($this->cloudevent->getSource(), $wrappedEvent->getSource());
89+
$this->assertSame($this->cloudevent->getType(), $wrappedEvent->getType());
90+
$this->assertSame($this->cloudevent->getData(), $wrappedEvent->getData());
91+
$this->assertSame($this->cloudevent->getDataContentType(), $wrappedEvent->getDataContentType());
92+
$this->assertSame($this->cloudevent->getDataSchema(), $wrappedEvent->getDataSchema());
93+
$this->assertSame($this->cloudevent->getSubject(), $wrappedEvent->getSubject());
94+
$this->assertEquals(DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime()), $wrappedEvent->getTime());
95+
}
96+
97+
public function testUnimplementedGetExtensionThrowsError(): void
98+
{
99+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
100+
$this->expectException(BadMethodCallException::class);
101+
102+
$wrappedEvent->getExtension('attribute');
103+
}
104+
105+
public function testUnimplementedGetExtensionsThrowsError(): void
106+
{
107+
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent);
108+
$this->expectException(BadMethodCallException::class);
109+
110+
$wrappedEvent->getExtensions();
111+
}
112+
}

0 commit comments

Comments
 (0)