diff --git a/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php index da09206184194..c32669965866f 100644 --- a/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php +++ b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php @@ -13,6 +13,7 @@ use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Middleware\MiddlewareInterface; /** @@ -31,7 +32,7 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan $this->entityManagerName = $entityManagerName; } - public function handle($message, callable $next) + public function handle(Envelope $envelope, callable $next) { $entityManager = $this->managerRegistry->getManager($this->entityManagerName); @@ -41,7 +42,7 @@ public function handle($message, callable $next) $entityManager->getConnection()->beginTransaction(); try { - $result = $next($message); + $result = $next($envelope); $entityManager->flush(); $entityManager->getConnection()->commit(); } catch (\Throwable $exception) { diff --git a/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php b/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php index 16fdfd81f1083..1ea8bc7eeb8ec 100644 --- a/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php +++ b/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php @@ -15,14 +15,13 @@ use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface; use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\EnvelopeAwareInterface; use Symfony\Component\Messenger\Middleware\MiddlewareInterface; /** * @author Samuel Roze * @author Tobias Schultze */ -class SendMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface +class SendMessageMiddleware implements MiddlewareInterface { private $senderLocator; private $messagesToSendAndHandleMapping; @@ -36,12 +35,11 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag /** * {@inheritdoc} */ - public function handle($message, callable $next) + public function handle(Envelope $envelope, callable $next) { - $envelope = Envelope::wrap($message); if ($envelope->get(ReceivedMessage::class)) { // It's a received message. Do not send it back: - return $next($message); + return $next($envelope); } $sender = $this->senderLocator->getSenderForMessage($envelope->getMessage()); @@ -54,7 +52,7 @@ public function handle($message, callable $next) } } - return $next($message); + return $next($envelope); } private function mustSendAndHandle($message): bool diff --git a/src/Symfony/Component/Messenger/Envelope.php b/src/Symfony/Component/Messenger/Envelope.php index 4d5f7a02a9d5d..3b7d0f61efcd5 100644 --- a/src/Symfony/Component/Messenger/Envelope.php +++ b/src/Symfony/Component/Messenger/Envelope.php @@ -35,16 +35,6 @@ public function __construct($message, array $items = array()) } } - /** - * Wrap a message into an envelope if not already wrapped. - * - * @param Envelope|object $message - */ - public static function wrap($message): self - { - return $message instanceof self ? $message : new self($message); - } - /** * @return Envelope a new Envelope instance with additional item */ diff --git a/src/Symfony/Component/Messenger/EnvelopeAwareInterface.php b/src/Symfony/Component/Messenger/EnvelopeAwareInterface.php deleted file mode 100644 index c19bc8436286a..0000000000000 --- a/src/Symfony/Component/Messenger/EnvelopeAwareInterface.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger; - -/** - * A Messenger protagonist aware of the message envelope and its content. - * - * @author Maxime Steinhausser - * - * @experimental in 4.1 - */ -interface EnvelopeAwareInterface -{ -} diff --git a/src/Symfony/Component/Messenger/MessageBus.php b/src/Symfony/Component/Messenger/MessageBus.php index 89fa8e04fa23c..ecba5f7ea7a6a 100644 --- a/src/Symfony/Component/Messenger/MessageBus.php +++ b/src/Symfony/Component/Messenger/MessageBus.php @@ -44,10 +44,10 @@ public function dispatch($message) throw new InvalidArgumentException(sprintf('Invalid type for message argument. Expected object, but got "%s".', \gettype($message))); } - return \call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message); + return \call_user_func($this->callableForNextMiddleware(0), $message instanceof Envelope ? $message : new Envelope($message)); } - private function callableForNextMiddleware(int $index, Envelope $currentEnvelope): callable + private function callableForNextMiddleware(int $index): callable { if (null === $this->indexedMiddlewareHandlers) { $this->indexedMiddlewareHandlers = \is_array($this->middlewareHandlers) ? array_values($this->middlewareHandlers) : iterator_to_array($this->middlewareHandlers, false); @@ -59,19 +59,8 @@ private function callableForNextMiddleware(int $index, Envelope $currentEnvelope $middleware = $this->indexedMiddlewareHandlers[$index]; - return function ($message) use ($middleware, $index, $currentEnvelope) { - if ($message instanceof Envelope) { - $currentEnvelope = $message; - } else { - $message = $currentEnvelope->withMessage($message); - } - - if (!$middleware instanceof EnvelopeAwareInterface) { - // Do not provide the envelope if the middleware cannot read it: - $message = $message->getMessage(); - } - - return $middleware->handle($message, $this->callableForNextMiddleware($index + 1, $currentEnvelope)); + return function (Envelope $envelope) use ($middleware, $index) { + return $middleware->handle($envelope, $this->callableForNextMiddleware($index + 1)); }; } } diff --git a/src/Symfony/Component/Messenger/Middleware/AllowNoHandlerMiddleware.php b/src/Symfony/Component/Messenger/Middleware/AllowNoHandlerMiddleware.php index 33494548f832f..1f5245b008bb5 100644 --- a/src/Symfony/Component/Messenger/Middleware/AllowNoHandlerMiddleware.php +++ b/src/Symfony/Component/Messenger/Middleware/AllowNoHandlerMiddleware.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Messenger\Middleware; +use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; /** @@ -18,10 +19,10 @@ */ class AllowNoHandlerMiddleware implements MiddlewareInterface { - public function handle($message, callable $next) + public function handle(Envelope $envelope, callable $next) { try { - return $next($message); + return $next($envelope); } catch (NoHandlerForMessageException $e) { // We allow not having a handler for this message. } diff --git a/src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php b/src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php index 4d63a8ae6c5a4..cfa0f173ea478 100644 --- a/src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php +++ b/src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Messenger\Middleware; +use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Handler\Locator\HandlerLocatorInterface; /** @@ -28,12 +29,13 @@ public function __construct(HandlerLocatorInterface $messageHandlerResolver) /** * {@inheritdoc} */ - public function handle($message, callable $next) + public function handle(Envelope $envelope, callable $next) { + $message = $envelope->getMessage(); $handler = $this->messageHandlerResolver->resolve($message); $result = $handler($message); - $next($message); + $next($envelope); return $result; } diff --git a/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php b/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php index ebaf8525c0407..e1cdaf4d07b04 100644 --- a/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php +++ b/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Messenger\Middleware; use Psr\Log\LoggerInterface; +use Symfony\Component\Messenger\Envelope; /** * @author Samuel Roze @@ -28,12 +29,14 @@ public function __construct(LoggerInterface $logger) /** * {@inheritdoc} */ - public function handle($message, callable $next) + public function handle(Envelope $envelope, callable $next) { + $message = $envelope->getMessage(); + $this->logger->debug('Starting handling message {class}', $this->createContext($message)); try { - $result = $next($message); + $result = $next($envelope); } catch (\Throwable $e) { $this->logger->warning('An exception occurred while handling message {class}', array_merge( $this->createContext($message), diff --git a/src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php b/src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php index 68fd7f45d3f09..0778db6d45717 100644 --- a/src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php +++ b/src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Messenger\Middleware; +use Symfony\Component\Messenger\Envelope; + /** * @author Samuel Roze * @@ -19,9 +21,9 @@ interface MiddlewareInterface { /** - * @param object $message + * @param Envelope $envelope * * @return mixed */ - public function handle($message, callable $next); + public function handle(Envelope $envelope, callable $next); } diff --git a/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php b/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php index e588d9256bd32..fab1346f81733 100644 --- a/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php +++ b/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Messenger\Middleware; use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\EnvelopeAwareInterface; use Symfony\Component\Messenger\Exception\ValidationFailedException; use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -20,7 +19,7 @@ /** * @author Tobias Nyholm */ -class ValidationMiddleware implements MiddlewareInterface, EnvelopeAwareInterface +class ValidationMiddleware implements MiddlewareInterface { private $validator; @@ -29,21 +28,20 @@ public function __construct(ValidatorInterface $validator) $this->validator = $validator; } - public function handle($message, callable $next) + public function handle(Envelope $envelope, callable $next) { - $envelope = Envelope::wrap($message); - $subject = $envelope->getMessage(); + $message = $envelope->getMessage(); $groups = null; /** @var ValidationConfiguration|null $validationConfig */ if ($validationConfig = $envelope->get(ValidationConfiguration::class)) { $groups = $validationConfig->getGroups(); } - $violations = $this->validator->validate($subject, null, $groups); + $violations = $this->validator->validate($message, null, $groups); if (\count($violations)) { - throw new ValidationFailedException($subject, $violations); + throw new ValidationFailedException($message, $violations); } - return $next($message); + return $next($envelope); } } diff --git a/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php index c9d548360c7b0..fe033adf38fba 100644 --- a/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php @@ -25,21 +25,7 @@ class SendMessageMiddlewareTest extends TestCase { public function testItSendsTheMessageToAssignedSender() { - $message = new DummyMessage('Hey'); - $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); - $next = $this->createPartialMock(\stdClass::class, array('__invoke')); - - $middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender)); - - $sender->expects($this->once())->method('send')->with(Envelope::wrap($message)); - $next->expects($this->never())->method($this->anything()); - - $middleware->handle($message, $next); - } - - public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage() - { - $envelope = Envelope::wrap(new DummyMessage('Hey')); + $envelope = new Envelope(new DummyMessage('Hey')); $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); @@ -53,7 +39,7 @@ public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage() public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageClass() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope(new DummyMessage('Hey')); $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); @@ -61,15 +47,15 @@ public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageClass() DummyMessage::class => true, )); - $sender->expects($this->once())->method('send')->with(Envelope::wrap($message)); + $sender->expects($this->once())->method('send')->with($envelope); $next->expects($this->once())->method($this->anything()); - $middleware->handle($message, $next); + $middleware->handle($envelope, $next); } public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageParentClass() { - $message = new ChildDummyMessage('Hey'); + $envelope = (new Envelope(new ChildDummyMessage('Hey'))); $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); @@ -77,15 +63,15 @@ public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageParentClass() DummyMessage::class => true, )); - $sender->expects($this->once())->method('send')->with(Envelope::wrap($message)); + $sender->expects($this->once())->method('send')->with($envelope); $next->expects($this->once())->method($this->anything()); - $middleware->handle($message, $next); + $middleware->handle($envelope, $next); } public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageInterface() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope(new DummyMessage('Hey')); $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); @@ -93,15 +79,15 @@ public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageInterface() DummyMessageInterface::class => true, )); - $sender->expects($this->once())->method('send')->with(Envelope::wrap($message)); + $sender->expects($this->once())->method('send')->with($envelope); $next->expects($this->once())->method($this->anything()); - $middleware->handle($message, $next); + $middleware->handle($envelope, $next); } public function testItAlsoCallsTheNextMiddlewareBasedOnWildcard() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope(new DummyMessage('Hey')); $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); @@ -109,27 +95,27 @@ public function testItAlsoCallsTheNextMiddlewareBasedOnWildcard() '*' => true, )); - $sender->expects($this->once())->method('send')->with(Envelope::wrap($message)); - $next->expects($this->once())->method($this->anything()); + $sender->expects($this->once())->method('send')->with($envelope); + $next->expects($this->once())->method($this->anything())->with($envelope); - $middleware->handle($message, $next); + $middleware->handle($envelope, $next); } public function testItCallsTheNextMiddlewareWhenNoSenderForThisMessage() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope(new DummyMessage('Hey')); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); $middleware = new SendMessageMiddleware(new InMemorySenderLocator(null)); - $next->expects($this->once())->method($this->anything()); + $next->expects($this->once())->method($this->anything())->with($envelope); - $middleware->handle($message, $next); + $middleware->handle($envelope, $next); } public function testItSkipsReceivedMessages() { - $envelope = Envelope::wrap(new DummyMessage('Hey'))->with(new ReceivedMessage()); + $envelope = (new Envelope(new DummyMessage('Hey')))->with(new ReceivedMessage()); $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); diff --git a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php index 6c77704adb90f..f323ab994870c 100644 --- a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php +++ b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php @@ -554,7 +554,7 @@ class DummyReceiver implements ReceiverInterface public function receive(callable $handler): void { for ($i = 0; $i < 3; ++$i) { - $handler(Envelope::wrap(new DummyMessage("Dummy $i"))); + $handler(new Envelope(new DummyMessage("Dummy $i"))); } } @@ -687,8 +687,8 @@ public function __invoke() class UselessMiddleware implements MiddlewareInterface { - public function handle($message, callable $next) + public function handle(Envelope $envelope, callable $next) { - return $next($message); + return $next($envelope); } } diff --git a/src/Symfony/Component/Messenger/Tests/EnvelopeTest.php b/src/Symfony/Component/Messenger/Tests/EnvelopeTest.php index 053275b6e714c..b1ab120d5e849 100644 --- a/src/Symfony/Component/Messenger/Tests/EnvelopeTest.php +++ b/src/Symfony/Component/Messenger/Tests/EnvelopeTest.php @@ -14,7 +14,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\EnvelopeAwareInterface; use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; @@ -34,29 +33,16 @@ public function testConstruct() $this->assertSame($receivedConfig, $configs[ReceivedMessage::class]); } - public function testWrap() - { - $first = Envelope::wrap($dummy = new DummyMessage('dummy')); - - $this->assertInstanceOf(Envelope::class, $first); - $this->assertSame($dummy, $first->getMessage()); - - $envelope = Envelope::wrap($first); - $this->assertSame($first, $envelope); - } - public function testWithReturnsNewInstance() { - $envelope = Envelope::wrap($dummy = new DummyMessage('dummy')); + $envelope = new Envelope(new DummyMessage('dummy')); $this->assertNotSame($envelope, $envelope->with(new ReceivedMessage())); } public function testGet() { - $envelope = Envelope::wrap($dummy = new DummyMessage('dummy')) - ->with($config = new ReceivedMessage()) - ; + $envelope = (new Envelope(new DummyMessage('dummy')))->with($config = new ReceivedMessage()); $this->assertSame($config, $envelope->get(ReceivedMessage::class)); $this->assertNull($envelope->get(ValidationConfiguration::class)); @@ -64,7 +50,7 @@ public function testGet() public function testAll() { - $envelope = Envelope::wrap($dummy = new DummyMessage('dummy')) + $envelope = (new Envelope(new DummyMessage('dummy'))) ->with($receivedConfig = new ReceivedMessage()) ->with($validationConfig = new ValidationConfiguration(array('foo'))) ; @@ -76,7 +62,3 @@ public function testAll() $this->assertSame($validationConfig, $configs[ValidationConfiguration::class]); } } - -class FooConfigurationConsumer implements EnvelopeAwareInterface -{ -} diff --git a/src/Symfony/Component/Messenger/Tests/MessageBusTest.php b/src/Symfony/Component/Messenger/Tests/MessageBusTest.php index 9dc93a9d15213..010c39600de25 100644 --- a/src/Symfony/Component/Messenger/Tests/MessageBusTest.php +++ b/src/Symfony/Component/Messenger/Tests/MessageBusTest.php @@ -14,7 +14,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\EnvelopeAwareInterface; use Symfony\Component\Messenger\MessageBus; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Middleware\MiddlewareInterface; @@ -42,20 +41,21 @@ public function testItDispatchInvalidMessageType() public function testItCallsMiddlewareAndChainTheReturnValue() { $message = new DummyMessage('Hello'); + $envelope = new Envelope($message); $responseFromDepthMiddleware = 1234; $firstMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); $firstMiddleware->expects($this->once()) ->method('handle') - ->with($message, $this->anything()) - ->will($this->returnCallback(function ($message, $next) { - return $next($message); + ->with($envelope, $this->anything()) + ->will($this->returnCallback(function (Envelope $envelope, callable $next) { + return $next($envelope); })); $secondMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); $secondMiddleware->expects($this->once()) ->method('handle') - ->with($message, $this->anything()) + ->with($envelope, $this->anything()) ->willReturn($responseFromDepthMiddleware); $bus = new MessageBus(array( @@ -66,56 +66,29 @@ public function testItCallsMiddlewareAndChainTheReturnValue() $this->assertEquals($responseFromDepthMiddleware, $bus->dispatch($message)); } - public function testItKeepsTheEnvelopeEvenThroughAMiddlewareThatIsNotEnvelopeAware() - { - $message = new DummyMessage('Hello'); - $envelope = new Envelope($message, array(new ReceivedMessage())); - - $firstMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); - $firstMiddleware->expects($this->once()) - ->method('handle') - ->with($message, $this->anything()) - ->will($this->returnCallback(function ($message, $next) { - return $next($message); - })); - - $secondMiddleware = $this->getMockBuilder(array(MiddlewareInterface::class, EnvelopeAwareInterface::class))->getMock(); - $secondMiddleware->expects($this->once()) - ->method('handle') - ->with($envelope, $this->anything()) - ; - - $bus = new MessageBus(array( - $firstMiddleware, - $secondMiddleware, - )); - - $bus->dispatch($envelope); - } - public function testThatAMiddlewareCanAddSomeItemsToTheEnvelope() { $message = new DummyMessage('Hello'); $envelope = new Envelope($message, array(new ReceivedMessage())); $envelopeWithAnotherItem = $envelope->with(new AnEnvelopeItem()); - $firstMiddleware = $this->getMockBuilder(array(MiddlewareInterface::class, EnvelopeAwareInterface::class))->getMock(); + $firstMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); $firstMiddleware->expects($this->once()) ->method('handle') ->with($envelope, $this->anything()) - ->will($this->returnCallback(function ($message, $next) { - return $next($message->with(new AnEnvelopeItem())); + ->will($this->returnCallback(function (Envelope $envelope, callable $next) { + return $next($envelope->with(new AnEnvelopeItem())); })); $secondMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); $secondMiddleware->expects($this->once()) ->method('handle') - ->with($message, $this->anything()) - ->will($this->returnCallback(function ($message, $next) { - return $next($message); + ->with($envelopeWithAnotherItem, $this->anything()) + ->will($this->returnCallback(function (Envelope $envelope, callable $next) { + return $next($envelope); })); - $thirdMiddleware = $this->getMockBuilder(array(MiddlewareInterface::class, EnvelopeAwareInterface::class))->getMock(); + $thirdMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); $thirdMiddleware->expects($this->once()) ->method('handle') ->with($envelopeWithAnotherItem, $this->anything()) @@ -141,12 +114,12 @@ public function testThatAMiddlewareCanUpdateTheMessageWhileKeepingTheEnvelopeIte $firstMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); $firstMiddleware->expects($this->once()) ->method('handle') - ->with($message, $this->anything()) - ->will($this->returnCallback(function ($message, $next) use ($changedMessage) { - return $next($changedMessage); + ->with($envelope, $this->anything()) + ->will($this->returnCallback(function (Envelope $envelope, callable $next) use ($changedMessage) { + return $next($envelope->withMessage($changedMessage)); })); - $secondMiddleware = $this->getMockBuilder(array(MiddlewareInterface::class, EnvelopeAwareInterface::class))->getMock(); + $secondMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); $secondMiddleware->expects($this->once()) ->method('handle') ->with($expectedEnvelope, $this->anything()) diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/AllowNoHandlerMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/AllowNoHandlerMiddlewareTest.php index 74610d8029c70..118a6238adfd6 100644 --- a/src/Symfony/Component/Messenger/Tests/Middleware/AllowNoHandlerMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Middleware/AllowNoHandlerMiddlewareTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Messenger\Tests\Middleware; use PHPUnit\Framework\TestCase; +use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; use Symfony\Component\Messenger\Middleware\AllowNoHandlerMiddleware; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; @@ -20,13 +21,13 @@ class AllowNoHandlerMiddlewareTest extends TestCase { public function testItCallsNextMiddlewareAndReturnsItsResult() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope(new DummyMessage('Hey')); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); - $next->expects($this->once())->method('__invoke')->with($message)->willReturn('Foo'); + $next->expects($this->once())->method('__invoke')->with($envelope)->willReturn('Foo'); $middleware = new AllowNoHandlerMiddleware(); - $this->assertSame('Foo', $middleware->handle($message, $next)); + $this->assertSame('Foo', $middleware->handle($envelope, $next)); } public function testItCatchesTheNoHandlerException() @@ -36,7 +37,7 @@ public function testItCatchesTheNoHandlerException() $middleware = new AllowNoHandlerMiddleware(); - $this->assertNull($middleware->handle(new DummyMessage('Hey'), $next)); + $this->assertNull($middleware->handle(new Envelope(new DummyMessage('Hey')), $next)); } /** @@ -49,6 +50,6 @@ public function testItDoesNotCatchOtherExceptions() $next->expects($this->once())->method('__invoke')->will($this->throwException(new \RuntimeException('Something went wrong.'))); $middleware = new AllowNoHandlerMiddleware(); - $middleware->handle(new DummyMessage('Hey'), $next); + $middleware->handle(new Envelope(new DummyMessage('Hey')), $next); } } diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php index 993a2de81265d..0eddf652a6873 100644 --- a/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Messenger\Tests\Middleware; use PHPUnit\Framework\TestCase; +use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Handler\Locator\HandlerLocator; use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; @@ -20,7 +21,7 @@ class HandleMessageMiddlewareTest extends TestCase { public function testItCallsTheHandlerAndNextMiddleware() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope($message = new DummyMessage('Hey')); $handler = $this->createPartialMock(\stdClass::class, array('__invoke')); $handler->method('__invoke')->willReturn('Hello'); @@ -32,8 +33,8 @@ public function testItCallsTheHandlerAndNextMiddleware() ))); $handler->expects($this->once())->method('__invoke')->with($message); - $next->expects($this->once())->method('__invoke')->with($message); + $next->expects($this->once())->method('__invoke')->with($envelope); - $middleware->handle($message, $next); + $middleware->handle($envelope, $next); } } diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/LoggingMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/LoggingMiddlewareTest.php index e662a130fbba9..65c3e7d3da6b2 100644 --- a/src/Symfony/Component/Messenger/Tests/Middleware/LoggingMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Middleware/LoggingMiddlewareTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Middleware\LoggingMiddleware; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; @@ -20,7 +21,7 @@ class LoggingMiddlewareTest extends TestCase { public function testDebugLogAndNextMiddleware() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope(new DummyMessage('Hey')); $logger = $this->createMock(LoggerInterface::class); $logger @@ -31,11 +32,11 @@ public function testDebugLogAndNextMiddleware() $next ->expects($this->once()) ->method('__invoke') - ->with($message) + ->with($envelope) ->willReturn('Hello') ; - $result = (new LoggingMiddleware($logger))->handle($message, $next); + $result = (new LoggingMiddleware($logger))->handle($envelope, $next); $this->assertSame('Hello', $result); } @@ -45,7 +46,7 @@ public function testDebugLogAndNextMiddleware() */ public function testWarningLogOnException() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope(new DummyMessage('Hey')); $logger = $this->createMock(LoggerInterface::class); $logger @@ -60,10 +61,10 @@ public function testWarningLogOnException() $next ->expects($this->once()) ->method('__invoke') - ->with($message) + ->with($envelope) ->willThrowException(new \Exception()) ; - (new LoggingMiddleware($logger))->handle($message, $next); + (new LoggingMiddleware($logger))->handle($envelope, $next); } } diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php index b2a44b5fa6723..ad9b391c3557e 100644 --- a/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php @@ -23,7 +23,7 @@ class ValidationMiddlewareTest extends TestCase { public function testValidateAndNextMiddleware() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope($message = new DummyMessage('Hey')); $validator = $this->createMock(ValidatorInterface::class); $validator @@ -36,18 +36,18 @@ public function testValidateAndNextMiddleware() $next ->expects($this->once()) ->method('__invoke') - ->with($message) + ->with($envelope) ->willReturn('Hello') ; - $result = (new ValidationMiddleware($validator))->handle($message, $next); + $result = (new ValidationMiddleware($validator))->handle($envelope, $next); $this->assertSame('Hello', $result); } public function testValidateWithConfigurationAndNextMiddleware() { - $envelope = Envelope::wrap($message = new DummyMessage('Hey'))->with(new ValidationConfiguration($groups = array('Default', 'Extra'))); + $envelope = (new Envelope($message = new DummyMessage('Hey')))->with(new ValidationConfiguration($groups = array('Default', 'Extra'))); $validator = $this->createMock(ValidatorInterface::class); $validator @@ -75,7 +75,7 @@ public function testValidateWithConfigurationAndNextMiddleware() */ public function testValidationFailedException() { - $message = new DummyMessage('Hey'); + $envelope = new Envelope($message = new DummyMessage('Hey')); $violationList = $this->createMock(ConstraintViolationListInterface::class); $violationList @@ -96,6 +96,6 @@ public function testValidationFailedException() ->method('__invoke') ; - (new ValidationMiddleware($validator))->handle($message, $next); + (new ValidationMiddleware($validator))->handle($envelope, $next); } } diff --git a/src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php b/src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php index 8a2946ee42778..f6a0718e18895 100644 --- a/src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php +++ b/src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php @@ -39,7 +39,7 @@ public function testItTracesResult() public function testItTracesResultWithEnvelope() { - $envelope = Envelope::wrap($message = new DummyMessage('Hello'))->with($envelopeItem = new AnEnvelopeItem()); + $envelope = (new Envelope($message = new DummyMessage('Hello')))->with($envelopeItem = new AnEnvelopeItem()); $bus = $this->getMockBuilder(MessageBusInterface::class)->getMock(); $bus->expects($this->once())->method('dispatch')->with($envelope)->willReturn($result = array('foo' => 'bar')); diff --git a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php index c01ffdadb7f0a..068715804560a 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php @@ -51,8 +51,8 @@ public function testItSendsAndReceivesMessages() $sender = new AmqpSender($serializer, $connection); $receiver = new AmqpReceiver($serializer, $connection); - $sender->send($first = Envelope::wrap(new DummyMessage('First'))); - $sender->send($second = Envelope::wrap(new DummyMessage('Second'))); + $sender->send($first = new Envelope(new DummyMessage('First'))); + $sender->send($second = new Envelope(new DummyMessage('Second'))); $receivedMessages = 0; $receiver->receive(function (?Envelope $envelope) use ($receiver, &$receivedMessages, $first, $second) { @@ -75,7 +75,7 @@ public function testItReceivesSignals() $connection->queue()->purge(); $sender = new AmqpSender($serializer, $connection); - $sender->send(Envelope::wrap(new DummyMessage('Hello'))); + $sender->send(new Envelope(new DummyMessage('Hello'))); $amqpReadTimeout = 30; $dsn = getenv('MESSENGER_AMQP_DSN').'?read_timeout='.$amqpReadTimeout; diff --git a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php index 8848b022f6aad..cec53f56abdd2 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php @@ -25,7 +25,7 @@ class AmqpSenderTest extends TestCase { public function testItSendsTheEncodedMessage() { - $envelope = Envelope::wrap(new DummyMessage('Oy')); + $envelope = new Envelope(new DummyMessage('Oy')); $encoded = array('body' => '...', 'headers' => array('type' => DummyMessage::class)); $encoder = $this->getMockBuilder(EncoderInterface::class)->getMock(); diff --git a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpTransportTest.php b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpTransportTest.php index 3d003a270d062..39401613db196 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpTransportTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpTransportTest.php @@ -46,7 +46,7 @@ public function testReceivesMessages() $amqpEnvelope->method('getBody')->willReturn('body'); $amqpEnvelope->method('getHeaders')->willReturn(array('my' => 'header')); - $decoder->method('decode')->with(array('body' => 'body', 'headers' => array('my' => 'header')))->willReturn(Envelope::wrap($decodedMessage)); + $decoder->method('decode')->with(array('body' => 'body', 'headers' => array('my' => 'header')))->willReturn(new Envelope($decodedMessage)); $connection->method('get')->willReturn($amqpEnvelope); $transport->receive(function (Envelope $envelope) use ($transport, $decodedMessage) { diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php index a34be3bfc2ce5..11e9d336823ce 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php @@ -26,7 +26,7 @@ class StopWhenMemoryUsageIsExceededReceiverTest extends TestCase public function testReceiverStopsWhenMemoryLimitExceeded(int $memoryUsage, int $memoryLimit, bool $shouldStop) { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('API'))); + $handler(new Envelope(new DummyMessage('API'))); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) @@ -59,7 +59,7 @@ public function memoryProvider() public function testReceiverLogsMemoryExceededWhenLoggerIsGiven() { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('API'))); + $handler(new Envelope(new DummyMessage('API'))); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php index e5c51335b3b8e..b7c2d2cc55cf0 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php @@ -26,9 +26,9 @@ class StopWhenMessageCountIsExceededReceiverTest extends TestCase public function testReceiverStopsWhenMaximumCountExceeded($max, $shouldStop) { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('First message'))); - $handler(Envelope::wrap(new DummyMessage('Second message'))); - $handler(Envelope::wrap(new DummyMessage('Third message'))); + $handler(new Envelope(new DummyMessage('First message'))); + $handler(new Envelope(new DummyMessage('Second message'))); + $handler(new Envelope(new DummyMessage('Third message'))); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) @@ -79,7 +79,7 @@ public function testReceiverDoesntIncreaseItsCounterWhenReceiveNullMessage() public function testReceiverLogsMaximumCountExceededWhenLoggerIsGiven() { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('First message'))); + $handler(new Envelope(new DummyMessage('First message'))); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php index 214b773092e8c..5ba93b239cbae 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php @@ -29,7 +29,7 @@ public function testEncodedIsDecodable() new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) ); - $envelope = Envelope::wrap(new DummyMessage('Hello')); + $envelope = new Envelope(new DummyMessage('Hello')); $this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope))); } @@ -40,7 +40,7 @@ public function testEncodedWithConfigurationIsDecodable() new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) ); - $envelope = Envelope::wrap(new DummyMessage('Hello')) + $envelope = (new Envelope(new DummyMessage('Hello'))) ->with(new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo')))) ->with(new ValidationConfiguration(array('foo', 'bar'))) ; @@ -54,7 +54,7 @@ public function testEncodedIsHavingTheBodyAndTypeHeader() new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) ); - $encoded = $serializer->encode(Envelope::wrap(new DummyMessage('Hello'))); + $encoded = $serializer->encode(new Envelope(new DummyMessage('Hello'))); $this->assertArrayHasKey('body', $encoded); $this->assertArrayHasKey('headers', $encoded); @@ -73,7 +73,7 @@ public function testUsesTheCustomFormatAndContext() $encoder = new Serializer($serializer, 'csv', array('foo' => 'bar')); - $encoded = $encoder->encode(Envelope::wrap($message)); + $encoded = $encoder->encode((new Envelope($message))); $decoded = $encoder->decode($encoded); $this->assertSame('Yay', $encoded['body']); @@ -86,7 +86,7 @@ public function testEncodedWithSerializationConfiguration() new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) ); - $envelope = Envelope::wrap(new DummyMessage('Hello')) + $envelope = (new Envelope(new DummyMessage('Hello'))) ->with(new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo')))) ->with(new ValidationConfiguration(array('foo', 'bar'))) ; diff --git a/src/Symfony/Component/Messenger/Tests/WorkerTest.php b/src/Symfony/Component/Messenger/Tests/WorkerTest.php index bdfa6fe188d62..2c99a8e625176 100644 --- a/src/Symfony/Component/Messenger/Tests/WorkerTest.php +++ b/src/Symfony/Component/Messenger/Tests/WorkerTest.php @@ -27,14 +27,14 @@ public function testWorkerDispatchTheReceivedMessage() $ipaMessage = new DummyMessage('IPA'); $receiver = new CallbackReceiver(function ($handler) use ($apiMessage, $ipaMessage) { - $handler(Envelope::wrap($apiMessage)); - $handler(Envelope::wrap($ipaMessage)); + $handler(new Envelope($apiMessage)); + $handler(new Envelope($ipaMessage)); }); $bus = $this->getMockBuilder(MessageBusInterface::class)->getMock(); - $bus->expects($this->at(0))->method('dispatch')->with(Envelope::wrap($apiMessage)->with(new ReceivedMessage())); - $bus->expects($this->at(1))->method('dispatch')->with(Envelope::wrap($ipaMessage)->with(new ReceivedMessage())); + $bus->expects($this->at(0))->method('dispatch')->with((new Envelope($apiMessage))->with(new ReceivedMessage())); + $bus->expects($this->at(1))->method('dispatch')->with((new Envelope($ipaMessage))->with(new ReceivedMessage())); $worker = new Worker($receiver, $bus); $worker->run(); @@ -42,7 +42,7 @@ public function testWorkerDispatchTheReceivedMessage() public function testWorkerDoesNotWrapMessagesAlreadyWrappedWithReceivedMessage() { - $envelop = Envelope::wrap(new DummyMessage('API'))->with(new ReceivedMessage()); + $envelop = (new Envelope(new DummyMessage('API')))->with(new ReceivedMessage()); $receiver = new CallbackReceiver(function ($handler) use ($envelop) { $handler($envelop); }); @@ -59,7 +59,7 @@ public function testWorkerIsThrowingExceptionsBackToGenerators() { $receiver = new CallbackReceiver(function ($handler) { try { - $handler(Envelope::wrap(new DummyMessage('Hello'))); + $handler(new Envelope(new DummyMessage('Hello'))); $this->assertTrue(false, 'This should not be called because the exception is sent back to the generator.'); } catch (\InvalidArgumentException $e) {