Skip to content

Commit 82917e4

Browse files
committed
Improved exception on invalid message type
1 parent 13aa95f commit 82917e4

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Exception;
13+
14+
/**
15+
* @author Yonel Ceruto <[email protected]>
16+
*/
17+
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
18+
{
19+
}

MessageBus.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Messenger;
1313

14+
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1415
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1516

1617
/**
@@ -39,6 +40,10 @@ public function __construct(iterable $middlewareHandlers = array())
3940
*/
4041
public function dispatch($message)
4142
{
43+
if (!\is_object($message)) {
44+
throw new InvalidArgumentException(sprintf('Invalid type for message argument. Expected object, but got "%s".', \gettype($message)));
45+
}
46+
4247
return \call_user_func($this->callableForNextMiddleware(0), $message);
4348
}
4449

Tests/MessageBusTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public function testItHasTheRightInterface()
2626
$this->assertInstanceOf(MessageBusInterface::class, $bus);
2727
}
2828

29+
/**
30+
* @expectedException \Symfony\Component\Messenger\Exception\InvalidArgumentException
31+
* @expectedExceptionMessage Invalid type for message argument. Expected object, but got "string".
32+
*/
33+
public function testItDispatchInvalidMessageType()
34+
{
35+
(new MessageBus())->dispatch('wrong');
36+
}
37+
2938
public function testItCallsMiddlewareAndChainTheReturnValue()
3039
{
3140
$message = new DummyMessage('Hello');

0 commit comments

Comments
 (0)