Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Activation middleware decorator #27320

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

Merged
merged 1 commit into from
Jul 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions src/Symfony/Component/Messenger/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ public function getMessage()
{
return $this->message;
}

/**
* @param object $target
*
* @return Envelope|object The original message or the envelope if the target supports it
* (i.e implements {@link EnvelopeAwareInterface}).
*/
public function getMessageFor($target)
{
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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

namespace Symfony\Component\Messenger\Middleware\Enhancers;
Copy link
Member

Choose a reason for hiding this comment

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

Is the Enhancers sub-namespace required? Nesting hurts discovery IMHO, better remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was mainly echoing Symfony\Component\Messenger\Transport\Enhancers which are decorators. But of course I'm fine with removing this namespace.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd keep it that way; it helps when exploring the code base I believe.


use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;

/**
* Execute the inner middleware according to an activation strategy.
*
* @author Maxime Steinhausser <[email protected]>
*/
class ActivationMiddlewareDecorator implements MiddlewareInterface, EnvelopeAwareInterface
{
private $inner;
private $activated;

/**
* @param bool|callable $activated
*/
public function __construct(MiddlewareInterface $inner, $activated)
{
$this->inner = $inner;
$this->activated = $activated;
}

/**
* @param Envelope $message
*/
public function handle($message, callable $next)
Copy link
Contributor

Choose a reason for hiding this comment

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

PHP allows you to rename $message to $envelope here, right?

{
if (\is_callable($this->activated) ? ($this->activated)($message) : $this->activated) {
return $this->inner->handle($message->getMessageFor($this->inner), $next);
}

return $next($message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Enhancers;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Middleware\Enhancers\ActivationMiddlewareDecorator;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

/**
* @author Maxime Steinhausser <[email protected]>
*/
class ActivationMiddlewareDecoratorTest extends TestCase
{
public function testExecuteMiddlewareOnActivated()
{
$message = new DummyMessage('Hello');
$envelope = Envelope::wrap($message);

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

$middleware = $this->createMock(MiddlewareInterface::class);
$middleware->expects($this->once())->method('handle')->with($message, $next)->willReturn('Hello from middleware');

$decorator = new ActivationMiddlewareDecorator($middleware, true);

$this->assertSame('Hello from middleware', $decorator->handle($envelope, $next));
}

public function testExecuteMiddlewareOnActivatedWithCallable()
{
$message = new DummyMessage('Hello');
$envelope = Envelope::wrap($message);

$activated = $this->createPartialMock(\stdClass::class, array('__invoke'));
$activated->expects($this->once())->method('__invoke')->with($envelope)->willReturn(true);

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

$middleware = $this->createMock(MiddlewareInterface::class);
$middleware->expects($this->once())->method('handle')->with($message, $next)->willReturn('Hello from middleware');

$decorator = new ActivationMiddlewareDecorator($middleware, $activated);

$this->assertSame('Hello from middleware', $decorator->handle($envelope, $next));
}

public function testExecuteEnvelopeAwareMiddlewareWithEnvelope()
{
$message = new DummyMessage('Hello');
$envelope = Envelope::wrap($message);

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

$middleware = $this->createMock(array(MiddlewareInterface::class, EnvelopeAwareInterface::class));
$middleware->expects($this->once())->method('handle')->with($envelope, $next)->willReturn('Hello from middleware');

$decorator = new ActivationMiddlewareDecorator($middleware, true);

$this->assertSame('Hello from middleware', $decorator->handle($envelope, $next));
}

public function testExecuteMiddlewareOnDeactivated()
{
$message = new DummyMessage('Hello');
$envelope = Envelope::wrap($message);

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

$middleware = $this->createMock(MiddlewareInterface::class);
$middleware->expects($this->never())->method('handle');

$decorator = new ActivationMiddlewareDecorator($middleware, false);

$this->assertSame('Hello from $next', $decorator->handle($envelope, $next));
}
}