8000 Add a messenger middleware to enforce null results by polc · Pull Request #28758 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Add a messenger middleware to enforce null results #28758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a messenger middleware to enforce null results
  • Loading branch information
polc committed Oct 7, 2018
commit d89ffd3ef24a62923438bec360676e1cce8d36f0
Original file line number Diff line number Diff line change
@@ -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 Paul Le Corre <paul@lecorre.me>
*/
class NonNullResultException extends \LogicException implements ExceptionInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Middleware;

use Symfony\Component\Messenger\Exception\NonNullResultException;

/**
* @author Paul Le Corre <paul@lecorre.me>
*/
class EnforceNullResultMiddleware implements MiddlewareInterface
{
public function handle($message, callable $next)
{
$result = $next($message);
if (null !== $result) {
throw new NonNullResultException(sprintf('Non null result for message %s.', \get_class($message)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- Non null result for message %s.
+ Non null result for message "%s": at least one handler returned something but this is prohibited by this middleware.

}

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Tests\Middleware;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Middleware\EnforceNullResultMiddleware;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

class EnforceNullResultMiddlewareTest extends TestCase
{
public function testItCallsNextMiddleware()
{
$message = new DummyMessage('Hey');

$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
$next->expects($this->once())->method('__invoke')->with($message);

$middleware = new EnforceNullResultMiddleware();
$middleware->handle($message, $next);
}

/**
* @expectedException \Symfony\Component\Messenger\Exception\NonNullResultException
* @expectedExceptionMessage Non null result for message Symfony\Component\Messenger\Tests\Fixtures\DummyMessage.
*/
public function testItThrowExceptionOnNonNullResult()
{
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
$next->expects($this->once())->method('__invoke')->will($this->returnValue('Non null value'));

$middleware = new EnforceNullResultMiddleware();
$middleware->handle(new DummyMessage('Hey'), $next);
}
}
0