diff --git a/src/Context/Context.php b/src/Context/Context.php index 7832f5e1f..19164caa5 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -22,6 +22,19 @@ public function __construct( ) { } + /** + * Test helper to create a fake context in one line. + */ + public static function fake(): self + { + return new self( + 'fake-aws-request-id', + time() + 1000 * 60 * 5, // 5 minutes from now (in milliseconds) + 'fake-invoked-function-arn', + 'fake-trace-id' + ); + } + /** * Returns the identifier of the invocation request. */ diff --git a/src/bref-local b/src/bref-local index aa9e012b2..d370bc924 100755 --- a/src/bref-local +++ b/src/bref-local @@ -35,12 +35,11 @@ try { ini_set('display_errors', '1'); error_reporting(E_ALL); -$requestId = '8f507cfc-example-4697-b07a-ac58fc914c95'; $startTime = logStart(); try { $invoker = new Invoker; - $result = $invoker->invoke($handler, $event, new Context($requestId, 0, '', '')); + $result = $invoker->invoke($handler, $event, Context::fake()); } catch (Throwable $e) { echo get_class($e) . ': ' . $e->getMessage() . PHP_EOL; echo 'Stack trace:' . PHP_EOL; diff --git a/tests/Event/Http/Psr7BridgeTest.php b/tests/Event/Http/Psr7BridgeTest.php index 19bf08f42..869a7e347 100644 --- a/tests/Event/Http/Psr7BridgeTest.php +++ b/tests/Event/Http/Psr7BridgeTest.php @@ -35,8 +35,7 @@ public function test I can create a response from a PSR7 response() protected function fromFixture(string $file): void { $event = new HttpRequestEvent(json_decode(file_get_contents($file), true, 512, JSON_THROW_ON_ERROR)); - $context = new Context('', 0, '', ''); - $this->request = Psr7Bridge::convertRequest($event, $context); + $this->request = Psr7Bridge::convertRequest($event, Context::fake()); } protected function assertBody(string $expected): void diff --git a/tests/Event/Sqs/SqsEventTest.php b/tests/Event/Sqs/SqsEventTest.php index a0fad0e18..0f678c959 100644 --- a/tests/Event/Sqs/SqsEventTest.php +++ b/tests/Event/Sqs/SqsEventTest.php @@ -2,7 +2,7 @@ namespace Bref\Test\Event\Sqs; -use Bref\Context\ContextBuilder; +use Bref\Context\Context; use Bref\Event\InvalidLambdaEvent; use Bref\Event\Sqs\SqsEvent; use Bref\Test\Fixture\SqsFakeHandler; @@ -47,7 +47,7 @@ public function test invalid event() public function test partial failure() { $event = json_decode(file_get_contents(__DIR__ . '/handler.json'), true); - $result = (new SqsFakeHandler)->handle($event, (new ContextBuilder)->buildContext()); + $result = (new SqsFakeHandler)->handle($event, Context::fake()); self::assertSame($result, [ 'batchItemFailures' => [