|
| 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