getStackMock example

$this->connection->expects($this->once())
            ->method('close')
        ;
        $this->connection->expects($this->once())
            ->method('connect')
        ;

        $envelope = new Envelope(new \stdClass()[
            new ConsumedByWorkerStamp(),
        ]);
        $this->middleware->handle($envelope$this->getStackMock());
    }

    public function testMiddlewarePingResetEntityManager()
    {
        $this->connection->expects($this->once())
            ->method('getDatabasePlatform')
            ->will($this->throwException(new DBALException()));

        $this->entityManager->expects($this->once())
            ->method('isOpen')
            ->willReturn(false)
        ;
/** * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> */
class ActivationMiddlewareTest extends MiddlewareTestCase
{
    public function testExecuteMiddlewareOnActivated()
    {
        $message = new DummyMessage('Hello');
        $envelope = new Envelope($message);

        $stack = $this->getStackMock(false);

        $middleware = $this->createMock(MiddlewareInterface::class);
        $middleware->expects($this->once())->method('handle')->with($envelope$stack)->willReturn($envelope);

        $decorator = new ActivationMiddleware($middleware, true);

        $decorator->handle($envelope$stack);
    }

    public function testExecuteMiddlewareOnActivatedWithCallable()
    {
        
use Symfony\Component\Messenger\Stamp\BusNameStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

class AddBusNameStampMiddlewareTest extends MiddlewareTestCase
{
    public function testItSendsTheMessageToAssignedSender()
    {
        $middleware = new AddBusNameStampMiddleware('the_bus_name');
        $envelope = new Envelope(new DummyMessage('the message'));

        $finalEnvelope = $middleware->handle($envelope$this->getStackMock());
        /** @var BusNameStamp $busNameStamp */
        $busNameStamp = $finalEnvelope->last(BusNameStamp::class);
        $this->assertNotNull($busNameStamp);
        $this->assertSame('the_bus_name', $busNameStamp->getBusName());

        // the stamp should not be added over and over again         $finalEnvelope = $middleware->handle($finalEnvelope$this->getStackMock());
        $this->assertCount(1, $finalEnvelope->all(BusNameStamp::class));
    }
}
$message = new DummyMessage('Hey');
        $envelope = new Envelope($message);

        $handler = $this->createPartialMock(HandleMessageMiddlewareTestCallable::class['__invoke']);

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

        $handler->expects($this->once())->method('__invoke')->with($message);

        $middleware->handle($envelope$this->getStackMock());
    }

    /** * @dataProvider itAddsHandledStampsProvider */
    public function testItAddsHandledStamps(array $handlers, array $expectedStamps, bool $nextIsCalled)
    {
        $message = new DummyMessage('Hey');
        $envelope = new Envelope($message);

        $middleware = new HandleMessageMiddleware(new HandlersLocator([
            
public function testItSendsTheMessageToAssignedSender()
    {
        $message = new DummyMessage('Hey');
        $envelope = new Envelope($message);
        $sender = $this->createMock(SenderInterface::class);

        $sendersLocator = $this->createSendersLocator([DummyMessage::class => ['my_sender']]['my_sender' => $sender]);
        $middleware = new SendMessageMiddleware($sendersLocator);

        $sender->expects($this->once())->method('send')->with($envelope->with(new SentStamp($sender::class, 'my_sender')))->willReturnArgument(0);

        $envelope = $middleware->handle($envelope$this->getStackMock(false));

        /* @var SentStamp $stamp */
        $this->assertInstanceOf(SentStamp::class$stamp = $envelope->last(SentStamp::class), 'it adds a sent stamp');
        $this->assertSame('my_sender', $stamp->getSenderAlias());
        $this->assertStringMatchesFormat('Mock_SenderInterface_%s', $stamp->getSenderClass());
    }

    public function testItSendsTheMessageToMultipleSenders()
    {
        $envelope = new Envelope(new DummyMessage('Hey'));
        $sender = $this->createMock(SenderInterface::class);
        
$this->middleware = new DoctrineOpenTransactionLoggerMiddleware($managerRegistry, null, $this->logger);
    }

    public function testMiddlewareWrapsInTransactionAndFlushes()
    {
        $this->connection->expects($this->exactly(1))
            ->method('isTransactionActive')
            ->will($this->onConsecutiveCalls(true, true, false))
        ;

        $this->middleware->handle(new Envelope(new \stdClass())$this->getStackMock());

        $this->assertSame(['error' => ['A handler opened a transaction but did not close it.']]$this->logger->logs);
    }
}
$message = new DummyMessage('Hey');
        $envelope = new Envelope($message);

        $validator = $this->createMock(ValidatorInterface::class);
        $validator
            ->expects($this->once())
            ->method('validate')
            ->with($message)
            ->willReturn($this->createMock(ConstraintViolationListInterface::class))
        ;

        (new ValidationMiddleware($validator))->handle($envelope$this->getStackMock());
    }

    public function testValidateWithStampAndNextMiddleware()
    {
        $message = new DummyMessage('Hey');
        $envelope = (new Envelope($message))->with(new ValidationStamp($groups = ['Default', 'Extra']));
        $validator = $this->createMock(ValidatorInterface::class);
        $validator
            ->expects($this->once())
            ->method('validate')
            ->with($message, null, $groups)
            


    public function testMiddlewareCloseConnection()
    {
        $this->connection->expects($this->once())
            ->method('close')
        ;

        $envelope = new Envelope(new \stdClass()[
            new ConsumedByWorkerStamp(),
        ]);
        $this->middleware->handle($envelope$this->getStackMock());
    }

    public function testInvalidEntityManagerThrowsException()
    {
        $managerRegistry = $this->createMock(ManagerRegistry::class);
        $managerRegistry
            ->method('getManager')
            ->with('unknown_manager')
            ->will($this->throwException(new \InvalidArgumentException()));

        $middleware = new DoctrineCloseConnectionMiddleware($managerRegistry, 'unknown_manager');

        
$context = new RequestContext('/', 'GET', 'symfony.com');

        $router = $this->createMock(RequestContextAwareInterface::class);
        $router
            ->expects($this->once())
            ->method('getContext')
            ->willReturn($context);

        $middleware = new RouterContextMiddleware($router);

        $envelope = new Envelope(new \stdClass());
        $envelope = $middleware->handle($envelope$this->getStackMock());

        $this->assertNotNull($stamp = $envelope->last(RouterContextStamp::class));
        $this->assertSame('symfony.com', $stamp->getHost());
    }

    public function testMiddlewareRestoreContext()
    {
        $router = $this->createMock(RequestContextAwareInterface::class);
        $context = new RequestContext('', 'POST', 'github.com');

        $router
            

        $this->connection->expects($this->once())
            ->method('beginTransaction')
        ;
        $this->connection->expects($this->once())
            ->method('commit')
        ;
        $this->entityManager->expects($this->once())
            ->method('flush')
        ;

        $this->middleware->handle(new Envelope(new \stdClass())$this->getStackMock());
    }

    public function testTransactionIsRolledBackOnException()
    {
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('Thrown from next middleware.');
        $this->connection->expects($this->once())
            ->method('beginTransaction')
        ;
        $this->connection->expects($this->once())
            ->method('rollBack')
        ;
Home | Imprint | This part of the site doesn't use cookies.