|
8 | 8 |
|
9 | 9 | /**
|
10 | 10 | * Class Simple TestCase.
|
11 |
| - * It depends on the <code>gcloud</code> command to obtain the project ID. |
12 | 11 | *
|
13 | 12 | * @author Martin Zeitler
|
14 | 13 | * @version 1.0.0
|
15 | 14 | */
|
16 | 15 | class SimpleTestCase extends TestCase {
|
17 | 16 |
|
18 | 17 | private static string $project_id;
|
19 |
| - private static string $test_topic; |
20 |
| - private static string $test_bucket; |
| 18 | + private static array $json_data; |
| 19 | + private static string $pubsub_topic; |
| 20 | + private static string $gcs_bucket; |
21 | 21 |
|
22 | 22 | public static function setUpBeforeClass(): void {
|
23 | 23 | require_once __DIR__.'/../index.php';
|
24 |
| - self::$project_id = self::get_project_id(); |
25 |
| - self::$test_topic = 'play-notifications'; |
26 |
| - self::$test_bucket = 'gs://php-cloudfunctions'; |
| 24 | + self::$project_id = self::get_project_id(); |
| 25 | + self::$json_data = ['data' => uniqid()]; |
| 26 | + self::$pubsub_topic = 'play-notifications'; |
| 27 | + self::$gcs_bucket = 'gs://php-cloudfunctions'; |
27 | 28 | }
|
28 |
| - |
| 29 | + |
| 30 | + /** It depends on the <code>gcloud</code> command to obtain the project ID. */ |
29 | 31 | private static function get_project_id(): string {
|
30 | 32 | $stdout = shell_exec('gcloud config get-value project');
|
31 | 33 | if ($stdout == null) {return 'unknown';}
|
32 | 34 | else {return $stdout;}
|
33 | 35 | }
|
34 | 36 |
|
| 37 | + /** Test: On HTTPS */ |
35 | 38 | public function test_on_https(): void {
|
36 |
| - $payload = json_encode(['data' => uniqid()]); |
| 39 | + $payload = json_encode(self::$json_data); |
37 | 40 | $request = new ServerRequest('POST', '/', [], $payload);
|
38 | 41 | $response = CloudFunctions::on_https( $request );
|
39 | 42 | // $this->assertTrue($response->getReasonPhrase().equals("OK"));
|
40 | 43 | $this->assertTrue($response->getStatusCode() == 200);
|
41 | 44 | }
|
42 | 45 |
|
| 46 | + /** Test: On Pub/Sub Event */ |
43 | 47 | public function test_on_pubsub(): void {
|
44 | 48 | CloudFunctions::on_pubsub(new CloudEventImmutable(
|
45 | 49 | uniqId(), // id
|
46 |
| - '//pubsub.googleapis.com/projects/'.self::$project_id.'/topics/'.self::$test_topic, // source |
| 50 | + '//pubsub.googleapis.com/projects/'.self::$project_id.'/topics/'.self::$pubsub_topic, // source |
47 | 51 | 'google.cloud.pubsub.topic.v1.messagePublished', // type
|
48 | 52 | ['data' => base64_encode('Test')], // data
|
49 | 53 | 'application/json' // data content-type
|
50 | 54 | ));
|
51 | 55 | self::assertTrue( true );
|
52 | 56 | }
|
53 | 57 |
|
| 58 | + /** Test: On GCS Event */ |
54 | 59 | public function test_on_gcs(): void {
|
55 | 60 | CloudFunctions::on_gcs(new CloudEventImmutable(
|
56 | 61 | uniqId(), // id
|
57 | 62 | 'pubsub.googleapis.com', // source
|
58 | 63 | 'google.storage.object.finalize', [ // type
|
59 |
| - 'bucket' => self::$test_bucket, |
| 64 | + 'bucket' => self::$gcs_bucket, |
60 | 65 | 'name' => 'test.json',
|
61 | 66 | 'metageneration' => '',
|
62 | 67 | 'timeCreated' => time(),
|
|
0 commit comments