Skip to content

Commit

Permalink
Remove calls to onConsecutiveCalls()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Apr 30, 2024
1 parent 56366af commit f85045f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
12 changes: 5 additions & 7 deletions Tests/Command/SetupTransportsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function testReceiverNames()
// get method must be call twice and will return consecutively a setup-able transport and a non setup-able transport
$serviceLocator->expects($this->exactly(2))
->method('get')
->will($this->onConsecutiveCalls(
->willReturn(
$this->createMock(SetupableTransportInterface::class),
$this->createMock(TransportInterface::class)
));
);
$serviceLocator
->method('has')
->willReturn(true);
Expand All @@ -53,12 +53,10 @@ public function testReceiverNameArgument()
/** @var MockObject&ServiceLocator $serviceLocator */
$serviceLocator = $this->createMock(ServiceLocator::class);
// get method must be call twice and will return consecutively a setup-able transport and a non setup-able transport
$serviceLocator->expects($this->exactly(1))
$serviceLocator->expects($this->once())
->method('get')
->will($this->onConsecutiveCalls(
$this->createMock(SetupableTransportInterface::class)
));
$serviceLocator->expects($this->exactly(1))
->willReturn($this->createMock(SetupableTransportInterface::class));
$serviceLocator->expects($this->once())
->method('has')
->willReturn(true);

Expand Down
76 changes: 40 additions & 36 deletions Tests/Middleware/DispatchAfterCurrentBusMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Tests\Middleware;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\Stub\ReturnCallback;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -67,12 +68,7 @@ public function testEventsInNewTransactionAreHandledAfterMainMessage()
->with($this->callback(function (Envelope $envelope) use (&$series) {
return $envelope->getMessage() === array_shift($series);
}))
->willReturnOnConsecutiveCalls(
$this->willHandleMessage(),
$this->willHandleMessage(),
$this->willHandleMessage(),
$this->willHandleMessage()
);
->will($this->willHandleMessage());

$messageBus->dispatch($message);
}
Expand Down Expand Up @@ -110,16 +106,19 @@ public function testThrowingEventsHandlingWontStopExecution()
$secondEvent,
];

$handlingMiddleware->expects($this->exactly(3))
$matcher = $this->exactly(3);
$handlingMiddleware->expects($matcher)
->method('handle')
->with($this->callback(function (Envelope $envelope) use (&$series) {
return $envelope->getMessage() === array_shift($series);
}))
->willReturnOnConsecutiveCalls(
$this->willHandleMessage(),
$this->throwException(new \RuntimeException('Some exception while handling first event')),
$this->willHandleMessage()
);
->willReturnCallback(function ($envelope, StackInterface $stack) use ($matcher) {
if (2 === $matcher->getInvocationCount()) {
throw new \RuntimeException('Some exception while handling first event');
}

return $stack->next()->handle($envelope, $stack);
});

$this->expectException(DelayedMessageHandlingException::class);
$this->expectExceptionMessage('RuntimeException: Some exception while handling first event');
Expand Down Expand Up @@ -176,34 +175,39 @@ public function testLongChainWithExceptions()
// Note: $eventL3a should not be handled.
];

$handlingMiddleware->expects($this->exactly(7))
$matcher = $this->exactly(7);
$handlingMiddleware->expects($matcher)
->method('handle')
->with($this->callback(function (Envelope $envelope) use (&$series) {
return $envelope->getMessage() === array_shift($series);
}))
->willReturnOnConsecutiveCalls(
$this->willHandleMessage(),
$this->willHandleMessage(),
$this->returnCallback(function ($envelope, StackInterface $stack) use ($eventBus, $eventL2a, $eventL2b) {
$envelope1 = new Envelope($eventL2a, [new DispatchAfterCurrentBusStamp()]);
$eventBus->dispatch($envelope1);
$eventBus->dispatch(new Envelope($eventL2b, [new DispatchAfterCurrentBusStamp()]));

return $stack->next()->handle($envelope, $stack);
}),
$this->willHandleMessage(),
$this->returnCallback(function () use ($eventBus, $eventL3a) {
$eventBus->dispatch(new Envelope($eventL3a, [new DispatchAfterCurrentBusStamp()]));

throw new \RuntimeException('Some exception while handling Event level 2a');
}),
$this->returnCallback(function ($envelope, StackInterface $stack) use ($eventBus, $eventL3b) {
$eventBus->dispatch(new Envelope($eventL3b, [new DispatchAfterCurrentBusStamp()]));

return $stack->next()->handle($envelope, $stack);
}),
$this->willHandleMessage()
);
->willReturnCallback(function ($envelope, StackInterface $stack) use ($eventBus, $eventL2a, $eventL2b, $eventL3a, $eventL3b, $matcher) {
switch ($matcher->getInvocationCount()) {
case 1:
case 2:
case 4:
case 7:
return $stack->next()->handle($envelope, $stack);

case 3:
$envelope1 = new Envelope($eventL2a, [new DispatchAfterCurrentBusStamp()]);
$eventBus->dispatch($envelope1);
$eventBus->dispatch(new Envelope($eventL2b, [new DispatchAfterCurrentBusStamp()]));

return $stack->next()->handle($envelope, $stack);

case 5:
$eventBus->dispatch(new Envelope($eventL3a, [new DispatchAfterCurrentBusStamp()]));

throw new \RuntimeException('Some exception while handling Event level 2a');
case 6:
$eventBus->dispatch(new Envelope($eventL3b, [new DispatchAfterCurrentBusStamp()]));

return $stack->next()->handle($envelope, $stack);
}

throw new AssertionFailedError('Unexpected call to handle');
});

$this->expectException(DelayedMessageHandlingException::class);
$this->expectExceptionMessage('RuntimeException: Some exception while handling Event level 2a');
Expand Down

0 comments on commit f85045f

Please sign in to comment.