Skip to content

Commit

Permalink
Merge pull request #1398 from brefphp/add-context-fake
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli authored Feb 13, 2023
2 parents ced4d6a + 1e47074 commit 4b19845
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 1 addition & 2 deletions src/bref-local
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions tests/Event/Http/Psr7BridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/Event/Sqs/SqsEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' => [
Expand Down

0 comments on commit 4b19845

Please sign in to comment.