Skip to content

Commit

Permalink
Add DispatcherInterface::unregisterAllHandlers + implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
burithetech authored and tomaj committed Apr 29, 2024
1 parent c0d22f9 commit 99ca784
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ public function registerHandlers(string $type, array $handlers): DispatcherInter
return $this;
}

/**
* {@inheritdoc}
*/
public function unregisterAllHandlers(): DispatcherInterface
{
$this->handlers = [];
return $this;
}

/**
* Serialize message to logger context
*
Expand Down
7 changes: 7 additions & 0 deletions src/DispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public function registerHandler(string $type, HandlerInterface $handler): Dispat
*/
public function registerHandlers(string $type, array $handler): DispatcherInterface;

/**
* Will unregister all handlers. This method is useful for testing.
*
* @return DispatcherInterface
*/
public function unregisterAllHandlers(): DispatcherInterface;

/**
* Basic method for background job to star listening.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/HandleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,24 @@ public function testMaxItemProcess(): void
$this->assertEquals(['n' => 2], $receivedMessages[0]->getPayload());
$this->assertEquals(['n' => 1], $receivedMessages[1]->getPayload());
}

public function testUnregisterAllHandlers(): void
{
$message1 = new Message('event1', ['a' => 'b']);
$message2 = new Message('event2', ['c' => 'd']);

$driver = new DummyDriver([$message1, $message2]);
$dispatcher = new Dispatcher($driver);

$handler = new TestHandler();

$dispatcher->registerHandler('event2', $handler);
$dispatcher->handle();

$dispatcher->unregisterAllHandlers();
$dispatcher->handle();

$receivedMessages = $handler->getReceivedMessages();
$this->assertCount(1, $receivedMessages);
}
}

0 comments on commit 99ca784

Please sign in to comment.