Skip to content

Commit 4fa71c5

Browse files
authored
refactor(functions): declarative function signature for helloworld_pubsub (GoogleCloudPlatform#1554)
1 parent bc94c2a commit 4fa71c5

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

functions/helloworld_pubsub/SampleUnitTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
2+
23
/**
34
* Copyright 2020 Google LLC.
45
*
@@ -21,7 +22,8 @@
2122

2223
namespace Google\Cloud\Samples\Functions\HelloworldPubsub\Test;
2324

24-
use Google\CloudFunctions\CloudEvent;
25+
use CloudEvents\V1\CloudEventImmutable;
26+
use CloudEvents\V1\CloudEventInterface;
2527
use PHPUnit\Framework\TestCase;
2628

2729
/**
@@ -45,15 +47,14 @@ public function dataProvider()
4547
{
4648
return [
4749
[
48-
'cloudevent' => CloudEvent::fromArray([
49-
'id' => uniqid(),
50-
'source' => 'pubsub.googleapis.com',
51-
'specversion' => '1.0',
52-
'type' => 'google.cloud.pubsub.topic.v1.messagePublished',
53-
'data' => [
50+
'cloudevent' => new CloudEventImmutable(
51+
uniqId(), // id
52+
'pubsub.googleapis.com', // source
53+
'google.cloud.pubsub.topic.v1.messagePublished', // type
54+
[
5455
'data' => base64_encode('John')
55-
],
56-
]),
56+
]
57+
),
5758
'expected' => 'Hello, John!'
5859
],
5960
];
@@ -63,7 +64,7 @@ public function dataProvider()
6364
* @dataProvider dataProvider
6465
*/
6566
public function testFunction(
66-
CloudEvent $cloudevent,
67+
CloudEventInterface $cloudevent,
6768
string $expected
6869
): void {
6970
// Capture function output by overriding the function's logging behavior.

functions/helloworld_pubsub/index.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
2+
23
/**
34
* Copyright 2021 Google LLC.
45
*
@@ -17,9 +18,16 @@
1718

1819
// [START functions_helloworld_pubsub]
1920

20-
use Google\CloudFunctions\CloudEvent;
21+
use CloudEvents\V1\CloudEventInterface;
22+
use Google\CloudFunctions\FunctionsFramework;
23+
24+
// Register the function with Functions Framework.
25+
// This enables omitting the `FUNCTIONS_SIGNATURE_TYPE=cloudevent` environment
26+
// variable when deploying. The `FUNCTION_TARGET` environment variable should
27+
// match the first parameter.
28+
FunctionsFramework::cloudEvent('helloworldPubsub', 'helloworldPubsub');
2129

22-
function helloworldPubsub(CloudEvent $event): void
30+
function helloworldPubsub(CloudEventInterface $event): void
2331
{
2432
$log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');
2533

0 commit comments

Comments
 (0)