Skip to content

Commit 231aa2c

Browse files
authored
feat(functions): add functions_helloworld_get, functions_helloworld_http (GoogleCloudPlatform#1187)
1 parent 0f250a4 commit 231aa2c

File tree

13 files changed

+521
-0
lines changed

13 files changed

+521
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.6"
4+
}
5+
}

functions/helloworld_get/index.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
/**
3+
* Copyright 2020 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+
// [START functions_helloworld_get]
19+
20+
use Psr\Http\Message\ServerRequestInterface;
21+
22+
function helloGet(ServerRequestInterface $request): string
23+
{
24+
return 'Hello, World!' . PHP_EOL;
25+
}
26+
27+
// [END functions_helloworld_get]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions Hello World (Get) Test Suite">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">.directory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
phpunit>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2020 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\HelloworldGet\Test;
21+
22+
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Class DeployTest.
27+
*
28+
* This test is not run by the CI system.
29+
*
30+
* To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true".
31+
* To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true".
32+
*/
33+
class DeployTest extends TestCase
34+
{
35+
use CloudFunctionDeploymentTrait;
36+
37+
private static $name = 'helloGet';
38+
39+
public function testFunction() : void
40+
{
41+
// Send a request to the function.
42+
$resp = $this->client->get('', [
43+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
44+
// 'debug' => true
45+
]);
46+
47+
// Assert status code.
48+
$this->assertEquals('200', $resp->getStatusCode());
49+
50+
// Assert function output.
51+
$expected = 'Hello, World!';
52+
$actual = trim((string) $resp->getBody());
53+
// Failures often lead to a large HTML page in the response body.
54+
$this->assertEquals($expected, $actual);
55+
}
56+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/**
3+
* Copyright 2020 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+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HelloworldGet\Test;
20+
21+
use PHPUnit\Framework\TestCase;
22+
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
23+
24+
/**
25+
* Class SystemTest.
26+
*/
27+
class SystemTest extends TestCase
28+
{
29+
use CloudFunctionLocalTestTrait;
30+
31+
private static $name = 'helloGet';
32+
33+
public function testFunction() : void
34+
{
35+
// Send a request to the function.
36+
$resp = $this->client->get('/');
37+
38+
// Assert status code.
39+
$this->assertEquals('200', $resp->getStatusCode());
40+
41+
// Assert function output.
42+
$expected = trim('Hello, World!');
43+
$actual = trim((string) $resp->getBody());
44+
$this->assertEquals($expected, $actual);
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2020 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+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HelloworldGet\Test;
20+
21+
use GuzzleHttp\Psr7\ServerRequest;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Unit tests for the Cloud Function.
26+
*/
27+
class UnitTest extends TestCase
28+
{
29+
private static $name = 'helloGet';
30+
31+
public static function setUpBeforeClass() : void
32+
{
33+
require_once __DIR__ . '/../index.php';
34+
}
35+
36+
public function testFunction() : void
37+
{
38+
$request = new ServerRequest('GET', '/');
39+
$output = $this->runFunction(self::$name, [$request]);
40+
$this->assertContains('Hello, World!', $output);
41+
}
42+
43+
private static function runFunction($functionName, array $params = []) : string
44+
{
45+
return call_user_func_array($functionName, $params);
46+
}
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.6"
4+
},
5+
"autoload-dev": {
6+
"psr-4": {
7+
"Google\\Cloud\\Samples\\Functions\\HelloworldHttp\\Test\\": "test"
8+
}
9+
}
10+
}

functions/helloworld_http/index.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
* Copyright 2020 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+
// [START functions_helloworld_http]
19+
20+
use Psr\Http\Message\ServerRequestInterface;
21+
22+
function helloHttp(ServerRequestInterface $request): string
23+
{
24+
$name = 'World';
25+
$body = $request->getBody()->getContents();
26+
if (!empty($body)) {
27+
$json = json_decode($body, true);
28+
if (json_last_error() != JSON_ERROR_NONE) {
29+
throw new RuntimeException(sprintf(
30+
'Could not parse body: %s', json_last_error_msg()
31+
));
32+
}
33+
$name = $json['name'] ?? $name;
34+
}
35+
$queryString = $request->getQueryParams();
36+
$name = $queryString['name'] ?? $name;
37+
38+
return sprintf('Hello, %s!', htmlspecialchars($name));
39+
}
40+
41+
// [END functions_helloworld_http]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions Hello World HTTP Test Suite">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">.directory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
phpunit>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2020 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\HelloworldHttp\Test;
21+
22+
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Class DeployTest.
27+
*
28+
* This test is not run by the CI system.
29+
*
30+
* To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true".
31+
* To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true".
32+
*/
33+
class DeployTest extends TestCase
34+
{
35+
use CloudFunctionDeploymentTrait;
36+
use TestCasesTrait;
37+
38+
private static $name = 'helloHttp';
39+
40+
public function testFunction() : void
41+
{
42+
foreach (self::cases() as $test) {
43+
$body = json_encode($test['body']);
44+
$resp = $this->client->post('', [
45+
'body' => $body,
46+
'query' => $test['query'],
47+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
48+
// 'debug' => true,
49+
]);
50+
$actual = trim((string) $resp->getBody());
51+
$this->assertEquals($test['code'], $resp->getStatusCode(), $test['label'] . ':');
52+
// Failures often lead to a large HTML page in the response body.
53+
$this->assertContains($test['expected'], $actual, $test['label'] . ':');
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)