Skip to content

Commit 37e3ded

Browse files
authored
fix: tests, exceptions and readme (GoogleCloudPlatform#54)
1 parent 5191f92 commit 37e3ded

File tree

9 files changed

+60
-46
lines changed

9 files changed

+60
-46
lines changed

README.md

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ different environments, including:
1515
The framework allows you to go from:
1616

1717
```php
18-
function helloHttp()
18+
use Psr\Http\Message\ServerRequestInterface;
19+
20+
function helloHttp(ServerRequestInterface $request)
1921
{
2022
return "Hello World from a PHP HTTP function!" . PHP_EOL;
2123
}
@@ -235,26 +237,6 @@ You can configure the Functions Framework using the environment variables shown
235237
| `FUNCTION_SOURCE` | The name of the file containing the source code for your function to load. Default: **`index.php`** (if it exists)
236238
| `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Can be either `http`, `event`, or `cloudevent`. Default: **`http`**
237239

238-
# Enable Background Events
239-
240-
The Functions Framework can unmarshall incoming event payloads to `data` and
241-
`context` objects. These will be passed as arguments to your function when it
242-
receives a request. Note that your function must use the event-style function
243-
signature:
244-
245-
```php
246-
function helloEvents($data, $context)
247-
{
248-
var_dump($data);
249-
var_dump($context);
250-
}
251-
```
252-
253-
To enable automatic unmarshalling, set the `FUNCTION_SIGNATURE_TYPE` environment
254-
variable to `event`. For more details on this signature type, check out the Google Cloud Functions
255-
documentation on
256-
[background functions](https://cloud.google.com/functions/docs/writing/background#cloud_pubsub_example).
257-
258240
# Enable CloudEvents
259241

260242
The Functions Framework can unmarshall incoming [CloudEvents](http://cloudevents.io)

src/FunctionWrapper.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,34 @@ private function validateFunctionSignature(
6969
ReflectionFunctionAbstract $reflection
7070
) {
7171
$parameters = $reflection->getParameters();
72-
if (count($parameters) != 1) {
73-
throw new LogicException(
74-
'Wrong number of parameters to your function, must be exactly 1'
75-
);
72+
// Check there is at least one parameter
73+
if (count($parameters) < 1) {
74+
$this->throwInvalidFunctionException();
7675
}
77-
78-
$class = $this->getFunctionParameterClassName();
76+
// Check the first parameter has the proper typehint
7977
$type = $parameters[0]->getType();
78+
$class = $this->getFunctionParameterClassName();
8079
if (!$type || $type->getName() !== $class) {
81-
throw new LogicException(
82-
sprintf(
83-
'Your function must have "%s" as the typehint for the first argument',
84-
$class
85-
)
86-
);
80+
$this->throwInvalidFunctionException();
81+
}
82+
83+
if (count($parameters) > 1) {
84+
for ($i = 1; $i < count($parameters); $i++) {
85+
if (!$parameters[$i]->isOptional()) {
86+
throw new LogicException(
87+
'If your function accepts more than one parameter the '
88+
. 'additional parameters must be optional'
89+
);
90+
}
91+
}
8792
}
8893
}
94+
95+
private function throwInvalidFunctionException()
96+
{
97+
throw new LogicException(sprintf(
98+
'Your function must have "%s" as the typehint for the first argument',
99+
$this->getFunctionParameterClassName()
100+
));
101+
}
89102
}

tests/CloudEventFunctionsWrapperTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testNoFunctionParameters()
5858
{
5959
$this->expectException('LogicException');
6060
$this->expectExceptionMessage(
61-
'Wrong number of parameters to your function, must be exactly 1'
61+
'Your function must have "Google\CloudFunctions\CloudEvent" as the typehint for the first argument'
6262
);
6363
$request = new ServerRequest('POST', '/', []);
6464
$cloudEventFunctionWrapper = new CloudEventFunctionWrapper(
@@ -71,10 +71,10 @@ public function testTooManyFunctionParameters()
7171
{
7272
$this->expectException('LogicException');
7373
$this->expectExceptionMessage(
74-
'Wrong number of parameters to your function, must be exactly 1'
74+
'If your function accepts more than one parameter the additional parameters must be optional'
7575
);
7676
$cloudEventFunctionWrapper = new CloudEventFunctionWrapper(
77-
function ($foo, $bar) {
77+
function (CloudEvent $foo, $bar) {
7878
}
7979
);
8080
}
@@ -117,6 +117,12 @@ function (CloudEvent $foo = null) {
117117
}
118118
);
119119
$this->assertTrue(true, 'No exception was thrown');
120+
// additional optional parameters are ok
121+
$cloudEventFunctionWrapper = new CloudEventFunctionWrapper(
122+
function (CloudEvent $foo, $bar = null) {
123+
}
124+
);
125+
$this->assertTrue(true, 'No exception was thrown');
120126
}
121127

122128
public function testWithFullCloudEvent()

tests/HttpFunctionWrapperTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testNoFunctionParameters()
3131
{
3232
$this->expectException('LogicException');
3333
$this->expectExceptionMessage(
34-
'Wrong number of parameters to your function, must be exactly 1'
34+
'Your function must have "Psr\Http\Message\ServerRequestInterface" as the typehint for the first argument'
3535
);
3636
$request = new ServerRequest('POST', '/', []);
3737
$httpFunctionWrapper = new HttpFunctionWrapper(
@@ -44,10 +44,10 @@ public function testTooManyFunctionParameters()
4444
{
4545
$this->expectException('LogicException');
4646
$this->expectExceptionMessage(
47-
'Wrong number of parameters to your function, must be exactly 1'
47+
'If your function accepts more than one parameter the additional parameters must be optional'
4848
);
4949
$httpFunctionWrapper = new HttpFunctionWrapper(
50-
function ($foo, $bar) {
50+
function (ServerRequestInterface $foo, $bar) {
5151
}
5252
);
5353
}
@@ -90,6 +90,12 @@ function (ServerRequestInterface $foo = null) {
9090
}
9191
);
9292
$this->assertTrue(true, 'No exception was thrown');
93+
// additional optional parameters are ok
94+
$httpFunctionWrapper = new HttpFunctionWrapper(
95+
function (ServerRequestInterface $foo, $bar = null) {
96+
}
97+
);
98+
$this->assertTrue(true, 'No exception was thrown');
9399
}
94100

95101
public function testHttpHttpFunctionWrapper()

tests/exampleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class exampleTest extends TestCase
3131

3232
public static function setUpBeforeClass(): void
3333
{
34-
if ('true' === getenv('TRAVIS')) {
35-
self::markTestSkipped('These tests do not pass on travis');
34+
if ('true' === getenv('SKIP_EXAMPLE_TESTS')) {
35+
self::markTestSkipped('Explicitly skipping the example tests');
3636
}
3737

3838
$exampleDir = __DIR__ . '/../examples/hello';

tests/fixtures/absolute.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
function helloDefault()
3+
use Psr\Http\Message\ServerRequestInterface;
4+
5+
function helloDefault(ServerRequestInterface $request)
46
{
57
return "Hello Absolute!";
68
}

tests/fixtures/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
function helloDefault()
3+
use Psr\Http\Message\ServerRequestInterface;
4+
5+
function helloDefault(ServerRequestInterface $request)
46
{
57
return "Hello Default!";
68
}

tests/fixtures/relative.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
function helloDefault()
3+
use Psr\Http\Message\ServerRequestInterface;
4+
5+
function helloDefault(ServerRequestInterface $request)
46
{
57
return "Hello Relative!";
68
}

tests/vendorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ class vendorTest extends TestCase
3131

3232
public static function setUpBeforeClass(): void
3333
{
34-
if ('true' === getenv('TRAVIS')) {
35-
self::markTestSkipped('These tests do not pass on travis');
34+
if ('true' === getenv('SKIP_EXAMPLE_TESTS')) {
35+
self::markTestSkipped('Explicitly skipping the example tests');
3636
}
37+
3738
mkdir($tmpDir = sys_get_temp_dir() . '/ff-php-test-' . rand());
3839
chdir($tmpDir);
3940

0 commit comments

Comments
 (0)