Skip to content

Commit 2863731

Browse files
authored
feat(functions): samples for typed function signature (#1866)
1 parent 16f7498 commit 2863731

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"php": ">= 7.4",
4+
"google/cloud-functions-framework": "^1.3"
5+
},
6+
"scripts": {
7+
"start": [
8+
"Composer\\Config::disableProcessTimeout",
9+
"FUNCTION_TARGET=helloHttp php -S localhost:${PORT:-8080} vendor/google/cloud-functions-framework/router.php"
10+
]
11+
}
12+
}

functions/typed_greeting/index.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 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+
// [START functions_typed_greeting]
20+
21+
use Google\CloudFunctions\FunctionsFramework;
22+
23+
class GreetingRequest
24+
{
25+
/** @var string */
26+
public $first_name;
27+
28+
/** @var string */
29+
public $last_name;
30+
31+
public function __construct(string $first_name = '', string $last_name = '')
32+
{
33+
$this->first_name = $first_name;
34+
$this->last_name = $last_name;
35+
}
36+
37+
public function serializeToJsonString(): string
38+
{
39+
return json_encode([
40+
'first_name' => $this->first_name,
41+
'last_name' => $this->last_name,
42+
]);
43+
}
44+
45+
public function mergeFromJsonString(string $body): void
46+
{
47+
$obj = json_decode($body);
48+
$this->first_name = $obj['first_name'];
49+
$this->last_name = $obj['last_name'];
50+
}
51+
}
52+
53+
class GreetingResponse
54+
{
55+
/** @var string */
56+
public $message;
57+
58+
public function __construct(string $message = '')
59+
{
60+
$this->message = $message;
61+
}
62+
63+
public function serializeToJsonString(): string
64+
{
65+
return json_encode([
66+
'message' => $message,
67+
]);
68+
}
69+
70+
public function mergeFromJsonString(string $body): void
71+
{
72+
$obj = json_decode($body);
73+
$this->message = $obj['message'];
74+
}
75+
};
76+
77+
function greeting(GreetingRequest $req): GreetingResponse
78+
{
79+
return new GreetingResponse("Hello $req->first_name $req->last_name!");
80+
};
81+
82+
FunctionsFramework::typed('greeting', 'greeting');
83+
84+
// [END functions_typed_greeting]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2023 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions Typed Greeting HTTP Test Suite">
20+
<directory>.</directory>
21+
<exclude>vendor</exclude>
22+
</testsuite>
23+
</testsuites>
24+
<logging>
25+
<log type="coverage-clover" target="build/logs/clover.xml"/>
26+
</logging>
27+
<filter>
28+
<whitelist>
29+
<directory suffix=".php">.</directory>
30+
<exclude>
31+
<directory>./vendor</directory>
32+
</exclude>
33+
</whitelist>
34+
</filter>
35+
</phpunit>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace Google\Cloud\Samples\Functions\TypedGreeting\Test;
21+
22+
use PHPUnit\Framework\TestCase;
23+
use GreetingRequest;
24+
use GreetingResponse;
25+
26+
/**
27+
* Unit tests for the Cloud Function.
28+
*/
29+
class UnitTest extends TestCase
30+
{
31+
private static $entryPoint = 'greeting';
32+
33+
public static function setUpBeforeClass(): void
34+
{
35+
require_once __DIR__ . '/../index.php';
36+
}
37+
38+
/**
39+
* @dataProvider cases
40+
*/
41+
public function testFunction(
42+
$label,
43+
$first_name,
44+
$last_name,
45+
$expected_message,
46+
): void {
47+
$actual = $this->runFunction(self::$entryPoint, [new GreetingRequest($first_name, $last_name)]);
48+
$this->assertEquals($expected_message, $actual->message, $label . ':');
49+
}
50+
51+
private static function runFunction($functionName, array $params = []): GreetingResponse
52+
{
53+
return call_user_func_array($functionName, $params);
54+
}
55+
56+
public static function cases(): array
57+
{
58+
return [
59+
[
60+
'label' => 'Default',
61+
'first_name' => 'Jane',
62+
'last_name' => 'Doe',
63+
'expected_message' => 'Hello Jane Doe!',
64+
],
65+
];
66+
}
67+
}

0 commit comments

Comments
 (0)