|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Runtime\GoogleCloud\Tests; |
| 4 | + |
| 5 | +use Google\CloudFunctions\CloudEvent; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Runtime\GoogleCloud\Runtime; |
| 8 | +use Symfony\Component\Runtime\RuntimeInterface; |
| 9 | + |
| 10 | +class RuntimeTest extends TestCase |
| 11 | +{ |
| 12 | + public function testStructuredType() |
| 13 | + { |
| 14 | + $input = [ |
| 15 | + 'id' => '1234567890', |
| 16 | + 'source' => '//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC', |
| 17 | + 'specversion' => '1.0', |
| 18 | + 'type' => 'com.google.cloud.pubsub.topic.publish', |
| 19 | + ]; |
| 20 | + |
| 21 | + $runtime = $this->getRuntimeMock(); |
| 22 | + $runtime->method('getBody')->willReturn(json_encode($input)); |
| 23 | + $runtime->method('getHeaders')->willReturn(['content-type' => 'application/cloudevents+json']); |
| 24 | + |
| 25 | + $output = $this->invokeCreateCloudEvent($runtime); |
| 26 | + $this->assertInstanceOf(CloudEvent::class, $output); |
| 27 | + $this->assertSame('1234567890', $output->getId()); |
| 28 | + $this->assertSame('com.google.cloud.pubsub.topic.publish', $output->getType()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testLegacyType() |
| 32 | + { |
| 33 | + $input = [ |
| 34 | + 'data' => 'foo', |
| 35 | + 'context' => [ |
| 36 | + 'eventId' => '1413058901901494', |
| 37 | + 'timestamp' => '2020-12-08T20:03:19.162Z', |
| 38 | + 'eventType' => 'providers/cloud.pubsub/eventTypes/topic.publish', |
| 39 | + 'resource' => [ |
| 40 | + 'name' => 'projects/MY-PROJECT/topics/MY-TOPIC', |
| 41 | + 'service' => 'pubsub.googleapis.com', |
| 42 | + ], |
| 43 | + ], |
| 44 | + ]; |
| 45 | + |
| 46 | + $runtime = $this->getRuntimeMock(); |
| 47 | + $runtime->method('getBody')->willReturn(json_encode($input)); |
| 48 | + $runtime->method('getHeaders')->willReturn([]); |
| 49 | + |
| 50 | + $output = $this->invokeCreateCloudEvent($runtime); |
| 51 | + $this->assertInstanceOf(CloudEvent::class, $output); |
| 52 | + $this->assertSame(['message' => 'foo'], $output->getData()); |
| 53 | + $this->assertSame('1413058901901494', $output->getId()); |
| 54 | + $this->assertSame('google.cloud.pubsub.topic.v1.messagePublished', $output->getType()); |
| 55 | + } |
| 56 | + |
| 57 | + public function testValidateJsonWithJsonContentType() |
| 58 | + { |
| 59 | + $runtime = $this->getRuntimeMock(); |
| 60 | + $runtime->method('getBody')->willReturn('not json'); |
| 61 | + $runtime->method('getHeaders')->willReturn(['content-type' => 'application/json']); |
| 62 | + |
| 63 | + $this->expectException(ExecutionStopped::class); |
| 64 | + $this->invokeCreateCloudEvent($runtime); |
| 65 | + } |
| 66 | + |
| 67 | + public function testValidateJsonWithStructuredType() |
| 68 | + { |
| 69 | + $runtime = $this->getRuntimeMock(); |
| 70 | + $runtime->method('getBody')->willReturn('not json'); |
| 71 | + $runtime->method('getHeaders')->willReturn(['content-type' => 'application/cloudevents+json']); |
| 72 | + |
| 73 | + $this->expectException(ExecutionStopped::class); |
| 74 | + $this->invokeCreateCloudEvent($runtime); |
| 75 | + } |
| 76 | + |
| 77 | + public function testValidateJsonWithLegacyType() |
| 78 | + { |
| 79 | + $runtime = $this->getRuntimeMock(); |
| 80 | + $runtime->method('getBody')->willReturn('not json'); |
| 81 | + $runtime->method('getHeaders')->willReturn([]); |
| 82 | + |
| 83 | + $this->expectException(ExecutionStopped::class); |
| 84 | + $this->invokeCreateCloudEvent($runtime); |
| 85 | + } |
| 86 | + |
| 87 | + public function testNoValidateJsonWithBinaryType() |
| 88 | + { |
| 89 | + $runtime = $this->getRuntimeMock(); |
| 90 | + $runtime->method('getBody')->willReturn('not json'); |
| 91 | + $runtime->method('getHeaders')->willReturn([ |
| 92 | + 'ce-id' => '1234567890', |
| 93 | + 'ce-source' => '//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC', |
| 94 | + 'ce-specversion' => '1.0', |
| 95 | + 'ce-type' => 'com.google.cloud.pubsub.topic.publish', |
| 96 | + ]); |
| 97 | + |
| 98 | + $output = $this->invokeCreateCloudEvent($runtime); |
| 99 | + $this->assertInstanceOf(CloudEvent::class, $output); |
| 100 | + $this->assertSame('not json', $output->getData()); |
| 101 | + } |
| 102 | + |
| 103 | + private function invokeCreateCloudEvent(RuntimeInterface $runtime): ?CloudEvent |
| 104 | + { |
| 105 | + $reflection = new \ReflectionObject($runtime); |
| 106 | + $method = $reflection->getMethod('createCloudEvent'); |
| 107 | + $method->setAccessible(true); |
| 108 | + |
| 109 | + return $method->invoke($runtime); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return \PHPUnit\Framework\MockObject\MockObject|Runtime |
| 114 | + */ |
| 115 | + private function getRuntimeMock() |
| 116 | + { |
| 117 | + $runtime = $this->getMockBuilder(Runtime::class) |
| 118 | + ->disableOriginalConstructor() |
| 119 | + ->onlyMethods(['getBody', 'getHeaders', 'sendHttpResponseAndExit']) |
| 120 | + ->getMock(); |
| 121 | + $runtime->method('sendHttpResponseAndExit')->willThrowException(new ExecutionStopped()); |
| 122 | + |
| 123 | + return $runtime; |
| 124 | + } |
| 125 | +} |
0 commit comments