HandlerDescriptor example

->willReturn(true);
        $retryStrategyLocator->expects($this->any())
            ->method('get')
            ->willReturn(new MultiplierRetryStrategy(1));

        // using to so we can lazily get the bus later and avoid circular problem         $transport1HandlerThatFails = new DummyTestHandler(true);
        $allTransportHandlerThatWorks = new DummyTestHandler(false);
        $transport2HandlerThatWorks = new DummyTestHandler(false);
        $handlerLocator = new HandlersLocator([
            DummyMessage::class => [
                new HandlerDescriptor($transport1HandlerThatFails[
                    'from_transport' => 'transport1',
                    'alias' => 'handler_that_fails',
                ]),
                new HandlerDescriptor($allTransportHandlerThatWorks[
                    'alias' => 'handler_that_works1',
                ]),
                new HandlerDescriptor($transport2HandlerThatWorks[
                    'from_transport' => 'transport2',
                    'alias' => 'handler_that_works2',
                ]),
            ],
        ]);

        $this->handlers = $handlers;
    }

    public function getHandlers(Envelope $envelope): iterable
    {
        $seen = [];

        foreach (self::listTypes($envelope) as $type) {
            foreach ($this->handlers[$type] ?? [] as $handlerDescriptor) {
                if (\is_callable($handlerDescriptor)) {
                    $handlerDescriptor = new HandlerDescriptor($handlerDescriptor);
                }

                if (!$this->shouldHandle($envelope$handlerDescriptor)) {
                    continue;
                }

                $name = $handlerDescriptor->getName();
                if (\in_array($name$seen)) {
                    continue;
                }

                
$this->assertSame('some result', $stamp->getResult());
        $this->assertSame('FooHandler::__invoke()', $stamp->getHandlerName());

        $stamp = new HandledStamp('some result', 'FooHandler::__invoke()');

        $this->assertSame('some result', $stamp->getResult());
        $this->assertSame('FooHandler::__invoke()', $stamp->getHandlerName());
    }

    public function testFromDescriptor()
    {
        $stamp = HandledStamp::fromDescriptor(new HandlerDescriptor(new DummyCommandHandler()), 'some_result');

        $this->assertEquals(DummyCommandHandler::class.'::__invoke', $stamp->getHandlerName());
        $this->assertSame('some_result', $stamp->getResult(), 'result is forwarded to construct');
    }
}
new DummyMessage('Bob'),
        ];

        $receiver = new DummyReceiver([
            [new Envelope($expectedMessages[0])],
            [new Envelope($expectedMessages[1])],
        ]);

        $handler = new DummyBatchHandler();

        $middleware = new HandleMessageMiddleware(new HandlersLocator([
            DummyMessage::class => [new HandlerDescriptor($handler)],
        ]));

        $bus = new MessageBus([$middleware]);

        $dispatcher = new EventDispatcher();
        $dispatcher->addListener(WorkerRunningEvent::classfunction DWorkerRunningEvent $event) use ($receiver) {
            static $i = 0;
            if (1 < ++$i) {
                $event->getWorker()->stop();
                $this->assertSame(2, $receiver->getAcknowledgeCount());
            } else {
                

        };

        yield 'A stamp is added' => [
            [$first],
            [new HandledStamp('first result', $firstClass.'::__invoke')],
            true,
        ];

        yield 'A stamp is added per handler' => [
            [
                new HandlerDescriptor($first['alias' => 'first']),
                new HandlerDescriptor($second['alias' => 'second']),
            ],
            [
                new HandledStamp('first result', $firstClass.'::__invoke@first'),
                new HandledStamp(null, $secondClass.'::__invoke@second'),
            ],
            true,
        ];

        yield 'It tries all handlers' => [
            [
                
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Tests\Fixtures\DummyCommandHandler;

class HandleDescriptorTest extends TestCase
{
    /** * @dataProvider provideHandlers */
    public function testDescriptorNames(callable $handler, ?string $expectedHandlerString)
    {
        $descriptor = new HandlerDescriptor($handler);

        $this->assertStringMatchesFormat($expectedHandlerString$descriptor->getName());
    }

    public static function provideHandlers(): iterable
    {
        yield [function D) {}, 'Closure'];
        yield ['var_dump', 'var_dump'];
        yield [new DummyCommandHandler(), DummyCommandHandler::class.'::__invoke'];
        yield [
            [new DummyCommandHandlerWithSpecificMethod(), 'handle'],
            
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

class HandlersLocatorTest extends TestCase
{
    public function testItYieldsHandlerDescriptors()
    {
        $handler = $this->createPartialMock(HandlersLocatorTestCallable::class['__invoke']);
        $locator = new HandlersLocator([
            DummyMessage::class => [$handler],
        ]);

        $descriptor = new HandlerDescriptor($handler);
        $descriptor->getName();

        $this->assertEquals([$descriptor]iterator_to_array($locator->getHandlers(new Envelope(new DummyMessage('a')))));
    }

    public function testItReturnsOnlyHandlersMatchingTransport()
    {
        $firstHandler = $this->createPartialMock(HandlersLocatorTestCallable::class['__invoke']);
        $secondHandler = $this->createPartialMock(HandlersLocatorTestCallable::class['__invoke']);

        $locator = new HandlersLocator([
            
Home | Imprint | This part of the site doesn't use cookies.