Skip to content

Commit 7ea14e3

Browse files
authored
refactor(functions): declarative function signature for helloworld_storage (GoogleCloudPlatform#1555)
1 parent 1b272af commit 7ea14e3

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

functions/helloworld_storage/SampleUnitTest.php

Lines changed: 12 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,9 @@
2122

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

24-
use Google\CloudFunctions\CloudEvent;
25+
use CloudEvents\V1\CloudEventImmutable;
26+
use CloudEvents\V1\CloudEventInterface;
27+
2528
use PHPUnit\Framework\TestCase;
2629

2730
/**
@@ -45,19 +48,18 @@ public function dataProvider()
4548
{
4649
return [
4750
[
48-
'cloudevent' => CloudEvent::fromArray([
49-
'id' => uniqid(),
50-
'source' => 'storage.googleapis.com',
51-
'specversion' => '1.0',
52-
'type' => 'google.cloud.storage.object.v1.finalized',
53-
'data' => [
51+
'cloudevent' => new CloudEventImmutable(
52+
uniqId(), // id
53+
'storage.googleapis.com', // source
54+
'google.cloud.storage.object.v1.finalized', // type
55+
[
5456
'bucket' => 'some-bucket',
5557
'metageneration' => '1',
5658
'name' => 'folder/friendly.txt',
5759
'timeCreated' => '2020-04-23T07:38:57.230Z',
5860
'updated' => '2020-04-23T07:38:57.230Z',
59-
],
60-
]),
61+
] // data
62+
),
6163
'statusCode' => '200',
6264
],
6365
];
@@ -66,7 +68,7 @@ public function dataProvider()
6668
/**
6769
* @dataProvider dataProvider
6870
*/
69-
public function testFunction(CloudEvent $cloudevent, string $statusCode): void
71+
public function testFunction(CloudEventInterface $cloudevent): void
7072
{
7173
// Capture function output by overriding the function's logging behavior.
7274
// The 'LOGGER_OUTPUT' environment variable must be used in your function:

functions/helloworld_storage/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"require": {
3-
"google/cloud-functions-framework": "^1.0.0"
3+
"php": ">= 7.4",
4+
"google/cloud-functions-framework": "^1.1"
45
},
56
"scripts": {
67
"start": [

functions/helloworld_storage/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 2020 Google LLC.
45
*
@@ -17,9 +18,16 @@
1718

1819
// [START functions_helloworld_storage]
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('helloGCS', 'helloGCS');
2129

22-
function helloGCS(CloudEvent $cloudevent)
30+
function helloGCS(CloudEventInterface $cloudevent)
2331
{
2432
$log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');
2533
$data = $cloudevent->getData();

0 commit comments

Comments
 (0)