Skip to content

Commit c176244

Browse files
author
Ace Nassri
authored
feat(functions): add phpinfo sample (GoogleCloudPlatform#1252)
1 parent ff3d873 commit c176244

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

functions/tips_phpinfo/composer.json

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.7.1"
4+
}
5+
}

functions/tips_phpinfo/index.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/**
3+
* Copyright 2021 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+
19+
// [START functions_tips_phpinfo]
20+
21+
use Psr\Http\Message\ServerRequestInterface;
22+
23+
function phpInfoDemo(ServerRequestInterface $request): string
24+
{
25+
// phpinfo() displays its output directly in the function's
26+
// HTTP response, so we don't need to explicitly return it
27+
//
28+
// Note: we recommend deleting the deployed Cloud Function once you no
29+
// longer need it, as phpinfo() may broadcast potential security issues.
30+
phpinfo();
31+
return '';
32+
}
33+
34+
// [END functions_tips_phpinfo]
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 phpinfo() 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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* Copyright 2021 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 $entryPoint = 'phpInfoDemo';
38+
39+
public function testFunction(): void
40+
{
41+
// Send a request to the function.
42+
$resp = $this->client->post('', [
43+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
44+
// 'debug' => true
45+
]);
46+
47+
$output = trim((string) $resp->getBody());
48+
49+
$this->assertEquals('200', $resp->getStatusCode());
50+
$this->assertContains('PHP Quality Assurance Team', $output);
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
* Copyright 2021 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 IntegrationTest.
26+
*/
27+
class IntegrationTest extends TestCase
28+
{
29+
use CloudFunctionLocalTestTrait;
30+
31+
private static $entryPoint = 'phpInfoDemo';
32+
33+
public function testFunction(): void
34+
{
35+
$resp = $this->client->get('/');
36+
$output = trim((string) $resp->getBody());
37+
38+
$this->assertEquals('200', $resp->getStatusCode());
39+
$this->assertContains('PHP Quality Assurance Team', $output);
40+
}
41+
}

0 commit comments

Comments
 (0)