Skip to content

Commit a460291

Browse files
authored
feat: allow context property to be omitted for background functions (GoogleCloudPlatform#34)
1 parent a316105 commit a460291

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/BackgroundFunctionWrapper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ public function execute(ServerRequestInterface $request): ResponseInterface
4141
}
4242

4343
$data = $event['data'];
44-
$context = Context::fromArray($event['context']);
44+
45+
if (array_key_exists('context', $event)) {
46+
$context = $event['context'];
47+
} else {
48+
unset($event['data']);
49+
$context = $event;
50+
}
51+
52+
$context = Context::fromArray($context);
4553
call_user_func($this->function, $data, $context);
4654

4755
return new Response();

tests/BackgroundFunctionWrapperTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testEmptyRequestBody()
4747
$backgroundFunctionWrapper->execute($request);
4848
}
4949

50-
public function testHttpBackgroundFunctionWrapper()
50+
public function testWithContextProperty()
5151
{
5252
$backgroundFunctionWrapper = new BackgroundFunctionWrapper([$this, 'invokeThis']);
5353
$request = new ServerRequest('GET', '/', [], json_encode([
@@ -63,6 +63,20 @@ public function testHttpBackgroundFunctionWrapper()
6363
$this->assertTrue(self::$functionCalled);
6464
}
6565

66+
public function testWrapperWithoutContextProperty()
67+
{
68+
$backgroundFunctionWrapper = new BackgroundFunctionWrapper([$this, 'invokeThis']);
69+
$request = new ServerRequest('GET', '/', [], json_encode([
70+
'data' => 'foo',
71+
'eventId' => 'abc',
72+
'timestamp' => 'def',
73+
'eventType' => 'ghi',
74+
'resource' => 'jkl',
75+
]));
76+
$backgroundFunctionWrapper->execute($request);
77+
$this->assertTrue(self::$functionCalled);
78+
}
79+
6680
public function invokeThis($data, Context $context)
6781
{
6882
self::$functionCalled = true;

0 commit comments

Comments
 (0)