-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
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
commit d89ffd3ef24a62923438bec360676e1cce8d36f0
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Messenger/Exception/NonNullResultException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/Messenger/Middleware/EnforceNullResultMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/Messenger/Tests/Middleware/EnforceNullResultMiddlewareTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.