-
Notifications
You must be signed in to change notification settings - Fork 38
adds CloudEvent helloworld to examples, README, and tests #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"require": { | ||
"google/cloud-functions-framework": "^0.3" | ||
"google/cloud-functions-framework": "dev-master as 0.6.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,30 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* To use this, set the following environment variables: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: can these also be specified as command-line args? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no CLI for php at the moment |
||
* FUNCTION_TARGET=helloHttp | ||
* FUNCTION_EVENT_TYPE=http | ||
*/ | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
function helloHttp(ServerRequestInterface $request) | ||
{ | ||
return sprintf("Hello %s from PHP HTTP function!" . PHP_EOL, | ||
$request->getQueryParams()['name'] ?? 'World'); | ||
} | ||
|
||
/** | ||
* To use this, set the following environment variables: | ||
* FUNCTION_TARGET=helloCloudEvent | ||
* FUNCTION_EVENT_TYPE=cloudevent | ||
*/ | ||
|
||
use Google\CloudFunctions\CloudEvent; | ||
|
||
function helloCloudEvent(CloudEvent $cloudevent) | ||
{ | ||
$stdout = fopen('php://stdout', 'wb'); | ||
fwrite($stdout, $cloudevent); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,33 +45,77 @@ public static function setUpBeforeClass(): void | |
$cmd = sprintf('docker build %s -t %s', $exampleDir, self::$imageId); | ||
|
||
passthru($cmd, $output); | ||
} | ||
|
||
public function testHttp(): void | ||
{ | ||
$cmd = 'docker run -d -p 8080:8080 ' | ||
. '-e FUNCTION_TARGET=helloHttp ' | ||
. self::$imageId; | ||
|
||
exec($cmd, $output); | ||
self::$containerId = $output[0]; | ||
|
||
// Tests fail if we do not wait before sending requests in | ||
sleep(1); | ||
} | ||
|
||
public function testIndex(): void | ||
{ | ||
exec('curl -v http://localhost:8080', $output); | ||
$this->assertContains('Hello World from PHP HTTP function!', $output); | ||
} | ||
|
||
public function testIndexWithQuery(): void | ||
{ | ||
exec('curl -v http://localhost:8080?name=Foo', $output); | ||
$this->assertContains('Hello Foo from PHP HTTP function!', $output); | ||
|
||
passthru('docker rm -f ' . self::$containerId); | ||
self::$containerId = null; | ||
} | ||
|
||
public function testCloudEvent(): void | ||
{ | ||
$cmd = 'docker run -d -t -p 8080:8080 ' | ||
. '-e FUNCTION_TARGET=helloCloudEvent ' | ||
. '-e FUNCTION_SIGNATURE_TYPE=cloudevent ' | ||
. self::$imageId; | ||
|
||
exec($cmd, $output); | ||
self::$containerId = $output[0]; | ||
|
||
// Tests fail if we do not wait before sending requests in | ||
sleep(1); | ||
|
||
$curl = 'curl -v localhost:8080 ' | ||
. '-H "ce-id: 1234567890" ' | ||
. ' -H "ce-source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC" ' | ||
. '-H "ce-specversion: 1.0" ' | ||
. '-H "ce-type: com.google.cloud.pubsub.topic.publish" ' | ||
. '-d \'{"foo": "bar"}\' &> /dev/stdout'; | ||
|
||
exec($curl); | ||
|
||
exec('docker logs ' . self::$containerId, $output); | ||
|
||
$outputAsString = implode("\n", $output); | ||
$this->assertStringContainsString('- id: 1234567890', $outputAsString); | ||
$this->assertStringContainsString( | ||
'- type: com.google.cloud.pubsub.topic.publish', | ||
$outputAsString | ||
); | ||
$this->assertStringContainsString( | ||
'- source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC"', | ||
$outputAsString | ||
); | ||
|
||
passthru('docker rm -f ' . self::$containerId); | ||
self::$containerId = null; | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
// If a test failed before it could delete its container | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: would it be better to have a single "clean up test resources" comment? (Or maybe even delete these comments outright, if you're of the "code should be self-documenting" philosophy.) |
||
if (self::$containerId) { | ||
passthru('docker rm -f ' . self::$containerId); | ||
} | ||
// Remove the test image | ||
if (self::$imageId) { | ||
passthru('docker rmi -f ' . self::$imageId); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this downloading the latest code available directly from GitHub? (i.e. disabling version pinning)
(This is a beta version, so I'm willing to allow it for now - but we definitely shouldn't do this once we release publicly.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's only for testing. I will change it back once a version is tagged