Skip to content

Commit 2808fa9

Browse files
author
Ace Nassri
authored
Merge branch 'master' into gcf-slack
2 parents 37a3ac0 + 41c4416 commit 2808fa9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2143
-142
lines changed

.kokoro/secrets.sh.enc

28 Bytes
Binary file not shown.

cloud_sql/sqlserver/pdo/src/Votes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function insertVote(string $value): bool
109109
$conn = $this->connection;
110110
$res = false;
111111

112-
# [START cloud_sql_server_pdo_connection]
112+
# [START cloud_sql_sqlserver_pdo_connection]
113113
// Use prepared statements to guard against SQL injection.
114114
$sql = "INSERT INTO votes (time_cast, vote_value) VALUES (GETDATE(), :voteValue)";
115115

@@ -126,7 +126,7 @@ public function insertVote(string $value): bool
126126
$e
127127
);
128128
}
129-
# [END cloud_sql_server_pdo_connection]
129+
# [END cloud_sql_sqlserver_pdo_connection]
130130

131131
return $res;
132132
}

cloud_sql/sqlserver/pdo/src/app.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@
5050
$dsn = sprintf('sqlsrv:server=%s;Database=%s', $host, $db_name);
5151

5252
// Connect to the database.
53-
# [START cloud_sql_server_pdo_timeout]
53+
# [START cloud_sql_sqlserver_pdo_timeout]
5454
// Here we set the connection timeout to five seconds and ask PDO to
5555
// throw an exception if any errors occur.
5656
$conn = new PDO($dsn, $username, $password, [
5757
PDO::ATTR_TIMEOUT => 5,
5858
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
5959
]);
60-
# [END cloud_sql_server_pdo_timeout]
61-
# [END cloud_sql_server_pdo_create_tcp]
60+
# [END cloud_sql_sqlserver_pdo_timeout]
61+
# [END cloud_sql_sqlserver_pdo_create_tcp]
6262
} catch (TypeError $e) {
6363
throw new RuntimeException(
6464
sprintf(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.6"
4+
},
5+
"scripts": {
6+
"start": [
7+
"Composer\\Config::disableProcessTimeout",
8+
"FUNCTION_TARGET=listFiles php -S localhost:${PORT:-8080} vendor/bin/router.php"
9+
]
10+
}
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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_concepts_filesystem]
19+
20+
use Psr\Http\Message\ServerRequestInterface;
21+
22+
function listFiles(ServerRequestInterface $request): string
23+
{
24+
$contents = scandir(__DIR__);
25+
26+
$output = "Files:" . PHP_EOL;
27+
28+
foreach ($contents as $file) {
29+
$output .= "\t" . $file . PHP_EOL;
30+
}
31+
32+
return $output;
33+
}
34+
35+
// [END functions_concepts_filesystem]
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 List Files 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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\ConceptsFilesystem\Test;
21+
22+
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
require_once __DIR__ . '/TestCasesTrait.php';
26+
27+
/**
28+
* Class DeployTest.
29+
*
30+
* This test is not run by the CI system.
31+
*
32+
* To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true".
33+
* To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true".
34+
*/
35+
class DeployTest extends TestCase
36+
{
37+
use CloudFunctionDeploymentTrait;
38+
use TestCasesTrait;
39+
40+
private static $entryPoint = 'listFiles';
41+
42+
/**
43+
* @dataProvider cases
44+
*/
45+
public function testFunction($file): void
46+
{
47+
// Send a request to the function.
48+
$resp = $this->client->get('', [
49+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
50+
// 'debug' => true
51+
]);
52+
53+
// Assert status code.
54+
$this->assertEquals('200', $resp->getStatusCode());
55+
56+
// Assert function output.
57+
$output = trim((string) $resp->getBody());
58+
// Failures often lead to a large HTML page in the response body.
59+
$this->assertContains($file, $output);
60+
}
61+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\ConceptsFilesystem\Test;
20+
21+
use PHPUnit\Framework\TestCase;
22+
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
23+
24+
require_once __DIR__ . '/TestCasesTrait.php';
25+
26+
/**
27+
* Class SystemTest.
28+
*/
29+
class SystemTest extends TestCase
30+
{
31+
use CloudFunctionLocalTestTrait;
32+
use TestCasesTrait;
33+
34+
private static $entryPoint = 'listFiles';
35+
36+
/**
37+
* @dataProvider cases
38+
*/
39+
public function testFunction($file): void
40+
{
41+
// Send a request to the function.
42+
$resp = $this->client->get('/');
43+
44+
// Assert status code.
45+
$this->assertEquals('200', $resp->getStatusCode());
46+
47+
// Assert function output.
48+
$output = trim((string) $resp->getBody());
49+
$this->assertContains($file, $output);
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\ConceptsFilesystem\Test;
20+
21+
trait TestCasesTrait
22+
{
23+
public static function cases(): array
24+
{
25+
return [
26+
[ 'file' => 'index.php' ],
27+
[ 'file' => 'composer.json', ],
28+
];
29+
}
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\ConceptsFilesystem\Test;
20+
21+
use GuzzleHttp\Psr7\ServerRequest;
22+
use PHPUnit\Framework\TestCase;
23+
24+
require_once __DIR__ . '/TestCasesTrait.php';
25+
26+
/**
27+
* Unit tests for the Cloud Function.
28+
*/
29+
class UnitTest extends TestCase
30+
{
31+
use TestCasesTrait;
32+
33+
private static $entryPoint = 'listFiles';
34+
35+
public static function setUpBeforeClass(): void
36+
{
37+
require_once __DIR__ . '/../index.php';
38+
}
39+
40+
/**
41+
* @dataProvider cases
42+
*/
43+
public function testFunction($file): void
44+
{
45+
$request = new ServerRequest('GET', '/');
46+
$response = $this->runFunction(self::$entryPoint, [$request]);
47+
$this->assertContains($file, $response);
48+
}
49+
50+
private static function runFunction($functionName, array $params = []): string
51+
{
52+
return call_user_func_array($functionName, $params);
53+
}
54+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.6",
4+
"guzzlehttp/guzzle": "^7.2.0"
5+
},
6+
"scripts": {
7+
"start": [
8+
"Composer\\Config::disableProcessTimeout",
9+
"FUNCTION_TARGET=makeRequest php -S localhost:${PORT:-8080} vendor/bin/router.php"
10+
]
11+
}
12+
}

functions/concepts_requests/index.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_concepts_requests]
19+
20+
use Psr\Http\Message\ServerRequestInterface;
21+
use Psr\Http\Message\ResponseInterface;
22+
use GuzzleHttp\Client;
23+
use GuzzleHttp\Psr7\Response;
24+
25+
function makeRequest(ServerRequestInterface $request): ResponseInterface
26+
{
27+
// This sample uses the GuzzleHTTP client
28+
// See its documentation for usage information
29+
// https://docs.guzzlephp.org/en/stable/
30+
31+
// Specify the URL to send requests to
32+
$client = new Client([
33+
'base_uri' => 'https://example.com',
34+
]);
35+
36+
// Send the request
37+
$url_response = $client->get('/');
38+
39+
$function_response = new Response(
40+
$url_response->getStatusCode(),
41+
[], // headers
42+
'' // body
43+
);
44+
45+
return $function_response;
46+
}
47+
48+
// [END functions_concepts_requests]

0 commit comments

Comments
 (0)