Skip to content

Commit 017d854

Browse files
author
ace-n
committed
Address comments
1 parent 4d408fc commit 017d854

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

functions/tips_scopes/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.6"
3+
"google/cloud-functions-framework": "^0.7.1"
44
}
55
}

functions/tips_scopes/index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@ function scopeDemo(ServerRequestInterface $request): string
4040
// https://packagist.org/providers/psr/cache-implementation
4141
$cachePath = sys_get_temp_dir() . '/cached_value.txt';
4242

43+
$response = '';
4344
if (file_exists($cachePath)) {
4445
// Read cached value from file
46+
$response .= "Reading cached value." . PHP_EOL;
4547
$instanceVar = file_get_contents($cachePath);
4648
} else {
4749
// Compute cached value + write to file
50+
$response .= "Cache empty, computing value." . PHP_EOL;
4851
$instanceVar = _heavyComputation();
4952
file_put_contents($cachePath, $instanceVar);
5053
}
5154

5255
// Lighter computations can re-run on each function invocation.
5356
$functionVar = _lightComputation();
5457

55-
$response = 'Per instance: ' . $instanceVar . PHP_EOL;
58+
$response .= 'Per instance: ' . $instanceVar . PHP_EOL;
5659
$response .= 'Per function: ' . $functionVar . PHP_EOL;
5760

5861
return $response;

functions/tips_scopes/test/DeployTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,28 @@ class DeployTest extends TestCase
3939
public function testFunction(): void
4040
{
4141
// Send a request to the function.
42-
$resp = $this->client->post('', [
42+
$firstResp = $this->client->post('', [
4343
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
4444
// 'debug' => true
4545
]);
46+
$secondResp = $this->client->post('', [
47+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
48+
// 'debug' => true
49+
]);
50+
51+
// Assert status codes.
52+
$this->assertEquals('200', $firstResp->getStatusCode());
53+
$this->assertEquals('200', $secondResp->getStatusCode());
54+
55+
$firstOutput = trim((string) $firstResp->getBody());
56+
$secondOutput = trim((string) $secondResp->getBody());
4657

47-
// Assert status code.
48-
$this->assertEquals('200', $resp->getStatusCode());
58+
// Assert generic function output.
59+
$this->assertContains('Per instance: 120', $firstOutput);
60+
$this->assertContains('Per function: 15', $firstOutput);
4961

50-
// Assert function output.
51-
$output = trim((string) $resp->getBody());
52-
$this->assertContains('Per instance: 120', $output);
53-
$this->assertContains('Per function: 15', $output);
62+
// Assert caching behavior.
63+
$this->assertContains('Cache empty', $firstOutput);
64+
$this->assertContains('Reading cached value', $secondOutput);
5465
}
5566
}

functions/tips_scopes/test/IntegrationTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ class IntegrationTest extends TestCase
3030

3131
private static $entryPoint = 'scopeDemo';
3232

33-
public function testFunction() : void
33+
public function testFunction(): void
3434
{
35-
// Send a request to the function.
36-
$resp = $this->client->post('/');
35+
// Send two requests to the function.
36+
// (This tests cross-request caching behavior.)
37+
$firstResp = $this->client->post('/');
38+
$secondResp = $this->client->post('/');
3739

38-
// Assert status code.
39-
$this->assertEquals('200', $resp->getStatusCode());
40+
// Assert status codes.
41+
$this->assertEquals('200', $firstResp->getStatusCode());
42+
$this->assertEquals('200', $secondResp->getStatusCode());
4043
}
4144
}

functions/tips_scopes/test/UnitTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ class UnitTest extends TestCase
2828
{
2929
private static $entryPoint = 'scopeDemo';
3030

31-
public static function setUpBeforeClass() : void
31+
public static function setUpBeforeClass(): void
3232
{
3333
require_once __DIR__ . '/../index.php';
3434
}
3535

36-
public function testFunction() : void
36+
public function testFunction(): void
3737
{
3838
$request = new ServerRequest('POST', '/');
3939
$output = $this->runFunction(self::$entryPoint, [$request]);
4040
$this->assertContains('Per instance: 120', $output);
4141
$this->assertContains('Per function: 15', $output);
4242
}
4343

44-
private static function runFunction($functionName, array $params = []) : string
44+
private static function runFunction($functionName, array $params = []): string
4545
{
4646
return call_user_func_array($functionName, $params);
4747
}

0 commit comments

Comments
 (0)