Hopefully this helps someone out there unit-testing Bref lambda consumers (eg. AWS lambda handlers for SNS / EventBridge / SQS, etc) with PHPUnit;
Essentially this includes the consumer (which is essentially a PHP function), and calls the function with array of event-data (in the same format AWS would normally give it).
The function (handler) would then return a response (hopefully with no thrown errors), and any unit-testing on the result would be done.
public function testConsumeUpdatePerson() {
$handler = include(__DIR__ . '/../bin/consume');
$data = json_encode([
'action' => 'update-person',
'id' => 1234
]);
$overallJson = '{
"Records": [
{
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-2:123456678:sns-lambda:abc-123",
"EventSource": "aws:sns",
"Sns": {
"SignatureVersion": "1",
"Timestamp": "2019-01-02T12:45:07.000Z",
"Signature": "aaaabbbb/ccccdddd/111111==",
"SigningCertUrl": "https://sns.us-east-2.amazonaws.com/SimpleNotificationService-aaaabbbb.pem",
"MessageId": "aaaabbbbb",
"Message": "' . addslashes($data) . '",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
},
"Type": "Notification",
"UnsubscribeUrl": "https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:111122222:test-lambda:aaaaa-bbbbb",
"TopicArn" : "arn:aws:sns:ap-southeast-2:1111222222:topic-name-goes-here",
"Subject": "TestInvoke"
}
}
]
}';
$event = json_decode($overallJson, true);
$response = $handler($event, new Context('', 300, '', ''));
$this->assertEquals('OK', $response);
}
More unit-tests can obviously be added below, but the basics of this test that there’s no errors, unhandled exceptions, etc which you hadn’t fully tested otherwise