Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
minor #27192 [Messenger] Improved exception on invalid message type (…
…yceruto)

This PR was merged into the 4.1 branch.

Discussion
----------

[Messenger] Improved exception on invalid message type

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

before:
![invalid_message_type_before](https://user-images.githubusercontent.com/2028198/39728992-91f592e6-5227-11e8-8a29-b13d1fed82f8.png)

after:
![invalid_message_type_after](https://user-images.githubusercontent.com/2028198/39728982-8ba5b790-5227-11e8-949d-64265b702fa7.png)

Commits
-------

01fe648 Improved exception on invalid message type
  • Loading branch information
sroze committed May 9, 2018
2 parents 0998710 + 01fe648 commit 9ce5941
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Exception;

/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/MessageBus.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger;

use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;

/**
Expand Down Expand Up @@ -39,6 +40,10 @@ public function __construct(iterable $middlewareHandlers = array())
*/
public function dispatch($message)
{
if (!\is_object($message)) {
throw new InvalidArgumentException(sprintf('Invalid type for message argument. Expected object, but got "%s".', \gettype($message)));
}

return \call_user_func($this->callableForNextMiddleware(0), $message);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Messenger/Tests/MessageBusTest.php
Expand Up @@ -26,6 +26,15 @@ public function testItHasTheRightInterface()
$this->assertInstanceOf(MessageBusInterface::class, $bus);
}

/**
* @expectedException \Symfony\Component\Messenger\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid type for message argument. Expected object, but got "string".
*/
public function testItDispatchInvalidMessageType()
{
(new MessageBus())->dispatch('wrong');
}

public function testItCallsMiddlewareAndChainTheReturnValue()
{
$message = new DummyMessage('Hello');
Expand Down

0 comments on commit 9ce5941

Please sign in to comment.