Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40f2d30

Browse files
authored
docs: updates examples for PSR-7, adds example test (GoogleCloudPlatform#30)
1 parent 4e59cea commit 40f2d30

File tree

4 files changed

+125
-16
lines changed

4 files changed

+125
-16
lines changed

README.md

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ Create an `index.php` file with the following contents:
5757
```php
5858
<?php
5959

60-
use Symfony\Component\HttpFoundation\Request;
60+
use Psr\Http\Message\ServerRequestInterface;
6161

62-
function helloHttp(Request $request)
62+
function helloHttp(ServerRequestInterface $request)
6363
{
6464
return "Hello World from a PHP HTTP function!" . PHP_EOL;
6565
}
@@ -159,25 +159,57 @@ in the [Cloud Console][cloud-run-console].
159159
[cloud-run-regions]: https://cloud.google.com/run/docs/locations
160160
[cloud-run-console]: https://console.cloud.google.com/run
161161

162-
## Accessing the HTTP Object
162+
## Working with PSR-7 HTTP Objects
163163

164-
The first parameter of your function is a `Request` object from `symfony/http-foundation`:
164+
The first parameter of your function is a `Request` object which implements the
165+
PSR-7 `ServerRequestInterface`:
165166

166167
```php
167-
use Symfony\Component\HttpFoundation\Request;
168+
use Psr\Http\Message\ServerRequestInterface;
168169

169-
function helloHttp(Request $request)
170+
function helloHttp(ServerRequestInterface $request): string
170171
{
171172
return sprintf("Hello %s from PHP HTTP function!" . PHP_EOL,
172-
$request->query->get('name') ?: 'World'
173-
);
173+
$request->getQueryParams()['name'] ?? 'World');
174174
}
175175
```
176176

177-
See the [HttpFoundation documentation][httpfoundation] documentation for more on working
178-
with the request object.
177+
You can return a PSR-7 compatible `ResponseInterface` instead of a string. This
178+
allows you to set additional request properties such as the HTTP Status Code
179+
and headers.
179180

180-
[httpfoundation]: https://symfony.com/doc/current/components/http_foundation.html
181+
```php
182+
use Psr\Http\Message\ServerRequestInterface;
183+
use Psr\Http\Message\ResponseInterface;
184+
use GuzzleHttp\Psr7\Response;
185+
186+
function helloHttp(ServerRequestInterface $request): ResponseInterface
187+
{
188+
$body = sprintf("Hello %s from PHP HTTP function!" . PHP_EOL,
189+
$request->getQueryParams()['name'] ?? 'World');
190+
191+
return (new Response())
192+
->withBody(GuzzleHttp\Psr7\stream_for($body))
193+
->withStatus(418) // I'm a teapot
194+
->withHeader('Foo', 'Bar');
195+
}
196+
```
197+
198+
A request to this function will produce a response similar to the following:
199+
200+
```
201+
HTTP/1.1 418 I'm a teapot
202+
Host: localhost:8080
203+
Date: Wed, 03 Jun 2020 00:48:38 GMT
204+
Foo: Bar
205+
206+
Hello World from PHP HTTP function!
207+
```
208+
209+
See the [PSR-7 documentation][psr7] documentation for more on working
210+
with the request and response objects.
211+
212+
[psr7]: https://www.php-fig.org/psr/psr-7/
181213

182214
## Run your function on Knative
183215

examples/hello/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-functions-framework": "^0.2"
3+
"google/cloud-functions-framework": "^0.3"
44
}
55
}

examples/hello/index.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
use Symfony\Component\HttpFoundation\Request;
18+
use Psr\Http\Message\ServerRequestInterface;
1919

20-
function helloHttp(Request $request)
20+
function helloHttp(ServerRequestInterface $request)
2121
{
2222
return sprintf("Hello %s from PHP HTTP function!" . PHP_EOL,
23-
$request->query->get('name') ?: 'World'
24-
);
23+
$request->getQueryParams()['name'] ?? 'World');
2524
}

tests/exampleTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
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+
namespace Google\CloudFunctions\Tests;
19+
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* Tests for when this package is installed in a vendored directory
24+
*
25+
* @group gcf-framework
26+
*/
27+
class exampleTest extends TestCase
28+
{
29+
private static $containerId;
30+
private static $imageId;
31+
32+
public static function setUpBeforeClass(): void
33+
{
34+
if ('true' === getenv('TRAVIS')) {
35+
self::markTestSkipped('These tests do not pass on travis');
36+
}
37+
38+
$exampleDir = __DIR__ . '/../examples/hello';
39+
self::$imageId = 'test-image-' . time();
40+
41+
$cmd = 'composer update -d ' . $exampleDir;
42+
43+
passthru($cmd, $output);
44+
45+
$cmd = sprintf('docker build %s -t %s', $exampleDir, self::$imageId);
46+
47+
passthru($cmd, $output);
48+
49+
$cmd = 'docker run -d -p 8080:8080 '
50+
. '-e FUNCTION_TARGET=helloHttp '
51+
. self::$imageId;
52+
53+
exec($cmd, $output);
54+
self::$containerId = $output[0];
55+
// Tests fail if we do not wait before sending requests in
56+
sleep(1);
57+
}
58+
59+
public function testIndex(): void
60+
{
61+
exec('curl -v http://localhost:8080', $output);
62+
$this->assertContains('Hello World from PHP HTTP function!', $output);
63+
}
64+
65+
public function testIndexWithQuery(): void
66+
{
67+
exec('curl -v http://localhost:8080?name=Foo', $output);
68+
$this->assertContains('Hello Foo from PHP HTTP function!', $output);
69+
}
70+
71+
public static function tearDownAfterClass(): void
72+
{
73+
if (self::$containerId) {
74+
passthru('docker rm -f ' . self::$containerId);
75+
passthru('docker rmi -f ' . self::$imageId);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)