Skip to content

Commit

Permalink
Test requestId is properly set in RoadRunnerTaskListenerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Apr 7, 2024
1 parent 69d871c commit c276f37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/RoadRunner/RoadRunnerTaskConsumerToListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
}

/**
* @param callable(string): void|null $setCurrentRequestId
* @param (callable(string): void)|null $setCurrentRequestId
*/
public function listenForTasks(?callable $setCurrentRequestId = null): void
{
Expand All @@ -41,15 +41,15 @@ public function listenForTasks(?callable $setCurrentRequestId = null): void
}

[
'listenerServiceName' => $listener,
'listenerServiceName' => $listenerService,
'eventPayload' => $payload,
'requestId' => $requestId,
] = json_decode($task->getPayload());
if ($setCurrentRequestId !== null) {
$setCurrentRequestId($requestId);
}

$this->container->get($listener)($event::fromPayload($payload));
$this->container->get($listenerService)($event::fromPayload($payload));
$task->complete();
} catch (Throwable $e) {
$task->fail($e);
Expand Down
15 changes: 12 additions & 3 deletions test/RoadRunner/RoadRunnerTaskConsumerToListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ShlinkioTest\Shlink\EventDispatcher\RoadRunner;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -58,15 +59,18 @@ function () use (&$callCount, $task) {
}

#[Test]
public function listenerIsLoadedAndInvoked(): void
#[TestWith(['123'])]
#[TestWith(['456'])]
#[TestWith(['abc'])]
public function listenerIsLoadedAndInvoked(string $requestId): void
{
$callCount = 0;
$task = $this->createMock(ReceivedTaskInterface::class);
$task->method('getName')->willReturn(DummyJsonDeserializable::class);
$task->method('getPayload')->willReturn(json_encode([
'listenerServiceName' => 'my_listener',
'eventPayload' => [],
'requestId' => '123',
'requestId' => $requestId,
]));
$task->expects($this->once())->method('complete');
$task->expects($this->never())->method('fail');
Expand All @@ -80,7 +84,12 @@ function () use (&$callCount, $task) {
});
$this->logger->expects($this->never())->method('warning');

$this->taskConsumer->listenForTasks();
$providedRequestId = null;
$this->taskConsumer->listenForTasks(function (string $id) use (&$providedRequestId): void {
$providedRequestId = $id;
});

self::assertEquals($requestId, $providedRequestId);
}

#[Test]
Expand Down

0 comments on commit c276f37

Please sign in to comment.