Skip to content

Commit 02da6ba

Browse files
author
Ace Nassri
authored
chore(functions): use status code in tests (GoogleCloudPlatform#1209)
1 parent 44689f5 commit 02da6ba

File tree

3 files changed

+73
-67
lines changed

3 files changed

+73
-67
lines changed

functions/http_form_data/test/DeployTest.php

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
namespace Google\Cloud\Samples\Functions\HelloworldHttp\Test;
2121

2222
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
23-
use GuzzleHttp\Exception\ClientException;
2423
use PHPUnit\Framework\TestCase;
2524

2625
require_once __DIR__ . '/TestCasesTrait.php';
@@ -40,39 +39,43 @@ class DeployTest extends TestCase
4039

4140
private static $name = 'uploadFile';
4241

43-
public function testFunction(): void
44-
{
45-
foreach (self::cases() as $test) {
46-
$method = $test['method'];
47-
$resp = $this->client->$method('', [
48-
'multipart' => $test['multipart'],
49-
]);
50-
$this->assertEquals($test['code'], $resp->getStatusCode(), $test['label'] . ' code:');
51-
$actual = trim((string) $resp->getBody());
52-
$this->assertContains($test['expected'], $actual, $test['label'] . ':');
53-
}
42+
/**
43+
* @dataProvider cases
44+
*/
45+
public function testFunction(
46+
$label,
47+
$method,
48+
$multipart,
49+
$expected,
50+
$statusCode
51+
): void {
52+
$resp = $this->client->$method('', [
53+
'multipart' => $multipart,
54+
]);
55+
$this->assertEquals($statusCode, $resp->getStatusCode(), $label . ' code:');
56+
$actual = trim((string) $resp->getBody());
57+
$this->assertContains($expected, $actual, $label . ':');
5458
}
5559

56-
public function testErrorCases(): void
57-
{
58-
$actual = $actualCode = null;
59-
foreach (self::errorCases() as $test) {
60-
try {
61-
$method = $test['method'];
62-
$resp = $this->client->$method('', [
63-
'multipart' => $test['multipart'],
64-
]);
60+
/**
61+
* @dataProvider errorCases
62+
*/
63+
public function testErrorCases(
64+
$label,
65+
$method,
66+
$multipart,
67+
$expected,
68+
$statusCode
69+
): void {
70+
$method = $method;
71+
$resp = $this->client->$method('', [
72+
'multipart' => $multipart,
73+
]);
6574

66-
$actual = $resp->getBody()->getContents();
67-
$actualCode = $resp->getStatusCode();
68-
} catch (ClientException $e) {
69-
// Expected exception, nothing to do here.
70-
$actual = $actualCode = $e->getMessage();
71-
} finally {
72-
print $actual . $actualCode;
73-
$this->assertContains($test['code'], $actualCode, $test['label'] . ' code:');
74-
$this->assertContains($test['expected'], $actual, $test['label'] . ':');
75-
}
76-
}
75+
$actual = $resp->getBody()->getContents();
76+
$actualCode = $resp->getStatusCode();
77+
78+
$this->assertEquals($statusCode, $actualCode, $label . ' code:');
79+
$this->assertContains($expected, $actual, $label . ':');
7780
}
7881
}

functions/http_form_data/test/SystemTest.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
namespace Google\Cloud\Samples\Functions\HelloworldHttp\Test;
2121

2222
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
23-
use GuzzleHttp\Exception\ClientException;
2423
use PHPUnit\Framework\TestCase;
2524

2625
require_once __DIR__ . '/TestCasesTrait.php';
@@ -35,36 +34,40 @@ class SystemTest extends TestCase
3534

3635
private static $name = 'uploadFile';
3736

38-
public function testFunction(): void
39-
{
40-
foreach (self::cases() as $test) {
41-
$method = $test['method'];
42-
$resp = $this->client->$method('/', [
43-
'multipart' => $test['multipart'],
37+
/**
38+
* @dataProvider cases
39+
*/
40+
public function testFunction(
41+
$label,
42+
$method,
43+
$multipart,
44+
$expected,
45+
$statusCode
46+
): void {
47+
$resp = $this->client->$method('/', [
48+
'multipart' => $multipart,
4449
]);
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-
}
50+
$this->assertEquals($statusCode, $resp->getStatusCode(), $label . ' code:');
51+
$actual = trim((string) $resp->getBody());
52+
$this->assertContains($expected, $actual, $label . ':');
4953
}
5054

51-
public function testErrorCases(): void
52-
{
53-
$actual = null;
54-
foreach (self::errorCases() as $test) {
55-
try {
56-
$method = $test['method'];
57-
$resp = $this->client->$method('/', [
58-
'multipart' => $test['multipart'],
59-
]);
60-
$actual = $resp->getBody()->getContents();
61-
} catch (ClientException $e) {
62-
// Expected exception, nothing to do here.
63-
$actual = $e->getMessage();
64-
} finally {
65-
$this->assertContains($test['code'], $actual, $test['label'] . ' code:');
66-
$this->assertContains($test['expected'], $actual, $test['label'] . ':');
67-
}
68-
}
55+
/**
56+
* @dataProvider errorCases
57+
*/
58+
public function testErrorCases(
59+
$label,
60+
$method,
61+
$multipart,
62+
$expected,
63+
$statusCode
64+
): void {
65+
$resp = $this->client->$method('/', [
66+
'multipart' => $multipart,
67+
]);
68+
$actual = $resp->getBody()->getContents();
69+
70+
$this->assertEquals($statusCode, $resp->getStatusCode(), $label . ' code:');
71+
$this->assertContains($expected, $actual, $label . ':');
6972
}
7073
}

functions/http_form_data/test/TestCasesTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function errorCases(): array
2828
'method' => 'post',
2929
'multipart' => [],
3030
'expected' => 'no files sent for upload',
31-
'code' => '400',
31+
'status_code' => '400',
3232
],
3333
// Fails on DeployTest with 400 error. curl returns:
3434
// curl: (92) HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
@@ -55,7 +55,7 @@ public static function errorCases(): array
5555
]
5656
],
5757
'expected' => 'no files sent for upload',
58-
'code' => '400',
58+
'status_code' => '400',
5959
],
6060
];
6161
}
@@ -74,7 +74,7 @@ public static function cases(): array
7474
]
7575
],
7676
'expected' => 'Saved rename.txt',
77-
'code' => '201',
77+
'status_code' => '201',
7878
],
7979
[
8080
'label' => 'File Upload (inline)',
@@ -87,7 +87,7 @@ public static function cases(): array
8787
]
8888
],
8989
'expected' => 'Saved inline_file.txt',
90-
'code' => '201',
90+
'status_code' => '201',
9191
],
9292
[
9393
'label' => 'File Upload (multiple)',
@@ -105,7 +105,7 @@ public static function cases(): array
105105
]
106106
],
107107
'expected' => 'Saved painting.txt, ice.txt',
108-
'code' => '201',
108+
'status_code' => '201',
109109
],
110110
[
111111
// name property is the same for both files, so only the last file is handled.
@@ -124,7 +124,7 @@ public static function cases(): array
124124
]
125125
],
126126
'expected' => 'Saved ice.txt',
127-
'code' => '201',
127+
'status_code' => '201',
128128
],
129129
];
130130
}

0 commit comments

Comments
 (0)