Skip to content

Commit 41135fb

Browse files
author
Ace Nassri
authored
chore(functions): clean up existing helloworld samples (GoogleCloudPlatform#1210)
1 parent 215be20 commit 41135fb

File tree

8 files changed

+68
-52
lines changed

8 files changed

+68
-52
lines changed

functions/helloworld_get/test/DeployTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DeployTest extends TestCase
4242
/**
4343
* @dataProvider cases
4444
*/
45-
public function testFunction($status_code, $expected): void
45+
public function testFunction($statusCode, $expected): void
4646
{
4747
// Send a request to the function.
4848
$resp = $this->client->get('', [
@@ -52,7 +52,7 @@ public function testFunction($status_code, $expected): void
5252

5353
// Assert status code.
5454
$this->assertEquals(
55-
$status_code,
55+
$statusCode,
5656
$resp->getStatusCode()
5757
);
5858

functions/helloworld_get/test/SystemTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class SystemTest extends TestCase
3636
/**
3737
* @dataProvider cases
3838
*/
39-
public function testFunction($status_code, $expected): void
39+
public function testFunction($statusCode, $expected): void
4040
{
4141
// Send a request to the function.
4242
$resp = $this->client->get('');
4343

4444
// Assert status code.
45-
$this->assertEquals($status_code, $resp->getStatusCode());
45+
$this->assertEquals($statusCode, $resp->getStatusCode());
4646

4747
// Assert function output.
4848
$actual = trim((string) $resp->getBody());

functions/helloworld_get/test/TestCasesTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function cases(): array
2424
{
2525
return [
2626
[
27-
'status_code' => 200,
27+
'statusCode' => 200,
2828
'expected' => 'Hello, World!'
2929
],
3030
];

functions/helloworld_get/test/UnitTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ public static function setUpBeforeClass(): void
4040
/**
4141
* @dataProvider cases
4242
*/
43-
public function testFunction($status_code, $expected): void
43+
public function testFunction($statusCode, $expected): void
4444
{
45-
foreach (self::cases() as $test) {
46-
$request = new ServerRequest('GET', '');
47-
$output = $this->runFunction(self::$name, [$request]);
48-
$this->assertContains($expected, $output);
49-
}
45+
$request = new ServerRequest('GET', '');
46+
$output = $this->runFunction(self::$name, [$request]);
47+
$this->assertContains($expected, $output);
5048
}
5149

5250
private static function runFunction($functionName, array $params = []): string

functions/helloworld_http/test/DeployTest.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,26 @@ class DeployTest extends TestCase
3939

4040
private static $name = 'helloHttp';
4141

42-
public function testFunction(): void
43-
{
44-
foreach (self::cases() as $test) {
45-
$body = json_encode($test['body']);
46-
$resp = $this->client->post('', [
47-
'body' => $body,
48-
'query' => $test['query'],
49-
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
50-
// 'debug' => true,
51-
]);
52-
$actual = trim((string) $resp->getBody());
53-
$this->assertEquals($test['code'], $resp->getStatusCode(), $test['label'] . ':');
54-
// Failures often lead to a large HTML page in the response body.
55-
$this->assertContains($test['expected'], $actual, $test['label'] . ':');
56-
}
42+
/**
43+
* @dataProvider cases
44+
*/
45+
public function testFunction(
46+
$label,
47+
$query,
48+
$body,
49+
$expected,
50+
$statusCode
51+
): void {
52+
$body = json_encode($body);
53+
$resp = $this->client->post('', [
54+
'body' => $body, 'query' => $query ]);
55+
$actual = trim((string) $resp->getBody());
56+
$this->assertEquals(
57+
$statusCode,
58+
$resp->getStatusCode(),
59+
$label . ':'
60+
);
61+
// Failures often lead to a large HTML page in the response body.
62+
$this->assertContains($expected, $actual, $label . ':');
5763
}
5864
}

functions/helloworld_http/test/SystemTest.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,23 @@ class SystemTest extends TestCase
3434

3535
private static $name = 'helloHttp';
3636

37-
public function testFunction(): void
38-
{
39-
foreach (self::cases() as $test) {
40-
$body = json_encode($test['body']);
41-
$resp = $this->client->post('/', [
42-
'body' => $body,
43-
'query' => $test['query'],
44-
]);
45-
$this->assertEquals($test['code'], $resp->getStatusCode(), $test['label'] . ' code:');
46-
$actual = trim((string) $resp->getBody());
47-
$this->assertContains($test['expected'], $actual, $test['label'] . ':');
48-
}
37+
/**
38+
* @dataProvider cases
39+
*/
40+
public function testFunction(
41+
$label,
42+
$query,
43+
$body,
44+
$expected,
45+
$statusCode
46+
): void {
47+
$body = json_encode($body);
48+
$resp = $this->client->post('/', [
49+
'body' => $body,
50+
'query' => $query,
51+
]);
52+
$this->assertEquals($statusCode, $resp->getStatusCode(), $label . ' code:');
53+
$actual = trim((string) $resp->getBody());
54+
$this->assertContains($expected, $actual, $label . ':');
4955
}
5056
}

functions/helloworld_http/test/TestCasesTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,35 @@ public static function cases(): array
2828
'query' => [],
2929
'body' => [],
3030
'expected' => 'Hello, World!',
31-
'code' => '200',
31+
'statusCode' => '200',
3232
],
3333
[
3434
'label' => 'Querystring Sets Name',
3535
'query' => ['name' => 'Galaxy'],
3636
'body' => [],
3737
'expected' => 'Hello, Galaxy!',
38-
'code' => '200',
38+
'statusCode' => '200',
3939
],
4040
[
4141
'label' => 'Body Sets Name',
4242
'query' => [],
4343
'body' => ['name' => 'Universe'],
4444
'expected' => 'Hello, Universe!',
45-
'code' => '200',
45+
'statusCode' => '200',
4646
],
4747
[
4848
'label' => 'Querystring Overrides Body',
4949
'query' => ['name' => 'Priority'],
5050
'body' => ['name' => 'Overridden'],
5151
'expected' => 'Hello, Priority!',
52-
'code' => '200',
52+
'statusCode' => '200',
5353
],
5454
[
5555
'label' => 'HTML Escape',
5656
'query' => [],
5757
'body' => ['name' => ''],
5858
'expected' => sprintf('Hello, %s!', '<script>script</script>'),
59-
'code' => '200',
59+
'statusCode' => '200',
6060
],
6161
];
6262
}

functions/helloworld_http/test/UnitTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,21 @@ public static function setUpBeforeClass(): void
3636
require_once __DIR__ . '/../index.php';
3737
}
3838

39-
public function testFunction(): void
40-
{
41-
foreach (self::cases() as $test) {
42-
$body = json_encode($test['body']);
43-
$request = (new ServerRequest('POST', '/', [], $body))
44-
->withQueryParams($test['query']);
45-
$actual = $this->runFunction(self::$name, [$request]);
46-
$this->assertContains($test['expected'], $actual, $test['label'] . ':');
47-
}
39+
/**
40+
* @dataProvider cases
41+
*/
42+
public function testFunction(
43+
$label,
44+
$query,
45+
$body,
46+
$expected,
47+
$statusCode
48+
): void {
49+
$body = json_encode($body);
50+
$request = (new ServerRequest('POST', '/', [], $body))
51+
->withQueryParams($query);
52+
$actual = $this->runFunction(self::$name, [$request]);
53+
$this->assertContains($expected, $actual, $label . ':');
4854
}
4955

5056
private static function runFunction($functionName, array $params = []): string

0 commit comments

Comments
 (0)