Skip to content

Commit 6ef24f8

Browse files
authored
fix: missing context keys do not throw PHP warning (GoogleCloudPlatform#43)
1 parent 7579d11 commit 6ef24f8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function fromArray(array $arr)
3737
$argKeys = ['eventId', 'timestamp', 'eventType', 'resource'];
3838
$args = [];
3939
foreach ($argKeys as $key) {
40-
$args[] = $arr[$key];
40+
$args[] = $arr[$key] ?? null;
4141
}
4242

4343
return new static(...$args);

tests/ContextTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,13 @@ public function testFromArray()
3838
$this->assertEquals('ghi', $context->eventType);
3939
$this->assertEquals('jkl', $context->resource);
4040
}
41+
42+
public function testFromEmptyArray()
43+
{
44+
$context = Context::fromArray([]);
45+
$this->assertEquals(null, $context->eventId);
46+
$this->assertEquals(null, $context->timestamp);
47+
$this->assertEquals(null, $context->eventType);
48+
$this->assertEquals(null, $context->resource);
49+
}
4150
}

0 commit comments

Comments
 (0)