Skip to content
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

refactor: add class that wraps CloudEvent into CloudEventInterface #113

Merged
merged 4 commits into from
Nov 2, 2021
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/conformance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: [ '7.2', '7.3', '7.4','8.0' ]
php-version: [ '7.4','8.0' ]
name: PHP ${{ matrix.php-version }} Conformance Test
steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '7.2', '7.3', '7.4','8.0' ]
php-versions: [ '7.4','8.0' ]
name: PHP ${{ matrix.php-versions }} Unit Test
steps:
- name: Checkout
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"description": "Google Cloud Functions Framework for PHP",
"license": "Apache-2.0",
"require": {
"php": ">=7.2",
"php": ">=7.4",
"guzzlehttp/psr7": "^1.7|^2.0",
"psr/http-message": "^1.0"
"psr/http-message": "^1.0",
"cloudevents/sdk-php": "^1.0"
},
"suggest": {
"google/cloud-storage": "Google Cloud Storage client library for storing and persisting objects. When included, the functions framework will register the gs:// stream wrapper."
Expand All @@ -16,7 +17,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^7.0|^8.0",
"phpunit/phpunit": "^7.5|^8.0",
"guzzlehttp/guzzle": "^7.2"
},
"bin": [
Expand Down
99 changes: 99 additions & 0 deletions src/CloudEventSdkCompliant.php
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline spacing is inconsistent with these functions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this is going to be used.

Do we want to implement CloudEventImmutable in addition to CloudEventInterface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the getters don't have newlines in between them, but other functions do.

CloudEventInterface is going to be what's passed to CloudEvent Functions with the new declarative function signatures. This is similar to how ServerRequestInterface is passed to HTTP functions today.

CloudEventImmutable is not an interface, so we can't implement it. I don't think we would benefit from inheriting or using it within this class, since we are not using any other part of the CloudEvents SDK today that can serialize/deserialize to CloudEvent or CloudEventImmutable.

Using the CloudEventInterface will put us a step in the right direction, because it means we can swap our implementation to use the official CloudEvent SDK in the future, without rewiring everything all at once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the next PR queue'd up which makes use of this new class, you can see the new function stub in this test:
https://github.com/anniefu/functions-framework-php/pull/1/files#diff-479b688739465688c5b60552045cb322a656e9bd47a2b9b12a05f8c4f76d3bd6R74

{
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();
}
}
112 changes: 112 additions & 0 deletions tests/CloudEventSdkCompliantTest.php
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"
]
);
Comment on lines +37 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using the constructor, which is pretty fragile given the number of arguments (and is not copy-pastable), could we deserialized a JSON payload?

JsonDeserializer::create()->deserializeStructured($payload);

Copy link
Contributor Author

@anniefu anniefu Nov 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CloudEvents SDK can't parse this payload due to a bug. I filed a bug on them: cloudevents/sdk-php#35

Until they fix it, the best we can do is provide the same interface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a TODO, or do you think using this constructor is fine?

}

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();
}
}