Skip to content

Commit 5264a4a

Browse files
authored
fix: improves eventarc sample (GoogleCloudPlatform#1226)
1 parent b774b2c commit 5264a4a

File tree

5 files changed

+83
-31
lines changed

5 files changed

+83
-31
lines changed

eventarc/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Eventarc – PHP Samples
4+
5+
This directory contains samples for using Eventarc with PHP.
6+
7+
## Sample
8+
9+
| Sample | Description |
10+
| --------------------------------------- | ------------------------ |
11+
|[Generic](generic) | Quickstart |

eventarc/generic/README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
# Eventarc – Generic – PHP Sample
44

5-
This directory contains a sample for receiving a generic event using Cloud Run and Eventarc with PHP. For testing purposes, we use Cloud Pub/Sub as an event source for our sample.
5+
[<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_generic]
66

7-
## Sample
8-
9-
| Sample | Description | Deploy |
10-
| --------------------------------------- | ------------------------ | ------------- |
11-
|[Generic][generic] | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_generic] |
7+
This directory contains a sample for receiving a generic event using Cloud Run
8+
and Eventarc with PHP. For testing purposes, we use Cloud Pub/Sub as an event
9+
source for our sample.
1210

1311
## Setup
1412

@@ -20,7 +18,8 @@ This directory contains a sample for receiving a generic event using Cloud Run a
2018
gcloud components install beta
2119
```
2220

23-
1. Set the following gcloud configurations, where `PROJECT_ID` is your Google Cloud project ID:
21+
1. Set the following gcloud configurations, where `PROJECT_ID` is your Google
22+
Cloud project ID:
2423

2524
```sh
2625
gcloud config set project PROJECT_ID
@@ -45,16 +44,15 @@ This directory contains a sample for receiving a generic event using Cloud Run a
4544
1. [Build the container locally](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker):
4645

4746
```sh
48-
export SAMPLE='generic'
49-
docker build --tag "eventarc-$SAMPLE" .
47+
docker build --tag eventarc-generic .
5048
```
5149

5250
1. [Run containers locally](https://cloud.google.com/run/docs/testing/local)
5351

5452
With the built container:
5553

5654
```sh
57-
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
55+
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} eventarc-generic
5856
```
5957

6058
Test the web server with `cURL`:
@@ -64,7 +62,7 @@ This directory contains a sample for receiving a generic event using Cloud Run a
6462
```
6563

6664
Observe the output logs your HTTP request:
67-
65+
6866
```
6967
Event received!
7068

@@ -178,5 +176,4 @@ To clean up, delete the resources created above:
178176
```
179177
180178
[enable_apis_url]: https://console.cloud.google.com/flows/enableapi?apiid=run.googleapis.com,logging.googleapis.com,cloudbuild.googleapis.com,pubsub.googleapis.com,eventarc.googleapis.com
181-
[generic]: .
182179
[run_button_generic]: https://deploy.cloud.run/?dir=eventarc/generic

eventarc/generic/index.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
*/
1717

1818
// [START eventarc_generic_server]
19-
$log = fopen('php://stderr', 'wb');
2019
// [END eventarc_generic_server]
2120

2221
// [START eventarc_generic_handler]
23-
echo "Event received!\n";
24-
fwrite($log, "Event received!\n");
22+
$msg = "Event received!\n";
2523

26-
echo "\nHEADERS:\n";
27-
fwrite($log, "\nHEADERS:\n");
24+
$msg .= "\nHEADERS:\n";
2825
$headers = getallheaders();
2926
unset($headers['Authorization']); // do not log authorization header
3027
foreach ($headers as $name => $value) {
31-
echo "$name: $value\n";
32-
fwrite($log, "$name: $value\n");
28+
$msg .= "$name: $value\n";
3329
}
3430

35-
echo "\nBODY:\n";
36-
fwrite($log, "\nBODY:\n");
31+
$msg .= "\nBODY:\n";
3732
$body = file_get_contents('php://input');
38-
echo $body . "\n";
39-
fwrite($log, $body . "\n");
33+
$msg .= $body . "\n";
34+
35+
// Write to stderr for logging
36+
$log = fopen('php://stderr', 'wb');
37+
fwrite($log, $msg);
38+
// Echo to return in request body
39+
echo $msg;
4040
// [END eventarc_generic_handler]

eventarc/generic/test/DeployTest.php

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

18-
namespace Google\Cloud\Samples\Run\Helloworld;
18+
namespace Google\Cloud\Samples\EventArc\Generic\Test;
1919

2020
use Google\Auth\ApplicationDefaultCredentials;
2121
use Google\Cloud\TestUtils\DeploymentTrait;
22-
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
2322
use Google\Cloud\TestUtils\GcloudWrapper\CloudRun;
2423
use Google\Cloud\TestUtils\TestTrait;
2524
use GuzzleHttp\Client;
@@ -29,10 +28,9 @@
2928
/**
3029
* Class DeployTest.
3130
*/
32-
class DeloyTest extends TestCase
31+
class DeployTest extends TestCase
3332
{
3433
use DeploymentTrait;
35-
use EventuallyConsistentTestTrait;
3634
use TestTrait;
3735

3836
/** @var \Google\Cloud\TestUtils\GcloudWrapper\CloudRun */
@@ -48,10 +46,12 @@ class DeloyTest extends TestCase
4846
*/
4947
public static function setUpDeploymentVars()
5048
{
51-
$projectId = self::requireEnv('GOOGLE_PROJECT_ID');
52-
$versionId = self::requireEnv('GOOGLE_VERSION_ID');
53-
self::$service = new CloudRun($projectId, ['service' => $versionId]);
54-
self::$image = sprintf('gcr.io/%s/%s:latest', $projectId, $versionId);
49+
if (is_null(self::$service) || is_null(self::$image)) {
50+
$projectId = self::requireEnv('GOOGLE_PROJECT_ID');
51+
$versionId = self::requireEnv('GOOGLE_VERSION_ID');
52+
self::$service = new CloudRun($projectId, ['service' => $versionId]);
53+
self::$image = sprintf('gcr.io/%s/%s:latest', $projectId, $versionId);
54+
}
5555
}
5656

5757
private static function beforeDeploy()

eventarc/generic/test/LocalTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
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\Cloud\Samples\EventArc\Generic\Test;
19+
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* Class LocalTest.
24+
*/
25+
class LocalTest extends TestCase
26+
{
27+
public function testIndex()
28+
{
29+
ob_start();
30+
require_once(__DIR__ . '/../index.php');
31+
$output = ob_get_clean();
32+
$expected = <<
33+
Event received!
34+
35+
HEADERS:
36+
37+
BODY:
38+
39+
40+
EOF;
41+
42+
$this->assertEquals($expected, $output);
43+
}
44+
}

0 commit comments

Comments
 (0)