8000 [Messenger] Activation middleware decorator · symfony/symfony@6e43838 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e43838

Browse files
committed
[Messenger] Activation middleware decorator
1 parent f59ce97 commit 6e43838

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/Symfony/Component/Messenger/Envelope.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,15 @@ public function getMessage()
8686
{
8787
return $this->message;
8888
}
89+
90+
/**
91+
* @param object $target
92+
*
93+
* @return Envelope|object The original message or the envelope if the target supports it
94+
* (i.e implements {@link EnvelopeAwareInterface}).
95+
*/
96+
public function getMessageFor($target)
97+
{
98+
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
99+
}
89100
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Middleware\Enhancers;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\EnvelopeAwareInterface;
16+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
17+
18+
/**
19+
* Execute the inner middleware according to an activation strategy.
20+
*
21+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
22+
*/
23+
class ActivationMiddlewareDecorator implements MiddlewareInterface, EnvelopeAwareInterface
24+
{
25+
private $inner;
26+
private $activated;
27+
28+
/**
29+
* @param bool|callable $activated
30+
*/
31+
public function __construct(MiddlewareInterface $inner, $activated)
32+
{
33+
$this->inner = $inner;
34+
$this->activated = $activated;
35+
}
36+
37+
/**
38+
* @param Envelope $message
39+
*/
40+
public function handle($message, callable $next)
41+
{
42+
if (\is_callable($this->activated) ? ($this->activated)($message) : $this->activated) {
43+
return $this->inner->handle($message->getMessageFor($this->inner), $next);
44+
}
45+
46+
return $next($message);
47+
}
48+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Middleware\Enhancers;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\EnvelopeAwareInterface;
17+
use Symfony\Component\Messenger\Middleware\Enhancers\ActivationMiddlewareDecorator;
18+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
19+
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
20+
21+
/**
22+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
23+
*/
24+
class ActivationMiddlewareDecoratorTest extends TestCase
25+
{
26+
public function testExecuteMiddlewareOnActivated()
27+
{
28+
$message = new DummyMessage('Hello');
29+
$envelope = Envelope::wrap($message);
30+
31+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
32+
$next->expects($this->never())->method('__invoke');
33+
34+
$middleware = $this->createMock(MiddlewareInterface::class);
35+
$middleware->expects($this->once())->method('handle')->with($message, $next)->willReturn('Hello from middleware');
36+
37+
$decorator = new ActivationMiddlewareDecorator($middleware, true);
38+
39+
$this->assertSame('Hello from middleware', $decorator->handle($envelope, $next));
40+
}
41+
42+
public function testExecuteMiddlewareOnActivatedWithCallable()
43+
{
44+
$message = new DummyMessage('Hello');
45+
$envelope = Envelope::wrap($message);
46+
47+
$activated = $this->createPartialMock(\stdClass::class, array('__invoke'));
48+
$activated->expects($this->once())->method('__invoke')->with($envelope)->willReturn(true);
49+
50+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
51+
$next->expects($this->never())->method('__invoke');
52+
53+
$middleware = $this->createMock(MiddlewareInterface::class);
54+
$middleware->expects($this->once())->method('handle')->with($message, $next)->willReturn('Hello from middleware');
55+
56+
$decorator = new ActivationMiddlewareDecorator($middleware, $activated);
57+
58+
$this->assertSame('Hello from middleware', $decorator->handle($envelope, $next));
59+
}
60+
61+
public function testExecuteEnvelopeAwareMiddlewareWithEnvelope()
62+
{
63+
$message = new DummyMessage('Hello');
64+
$envelope = Envelope::wrap($message);
65+
66+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
67+
$next->expects($this->never())->method('__invoke');
68+
69+
$middleware = $this->createMock(array(MiddlewareInterface::class, EnvelopeAwareInterface::class));
70+
$middleware->expects($this->once())->method('handle')->with($envelope, $next)->willReturn('Hello from middleware');
71+
72+
$decorator = new ActivationMiddlewareDecorator($middleware, true);
73+
74+
$this->assertSame('Hello from middleware', $decorator->handle($envelope, $next));
75+
}
76+
77+
public function testExecuteMiddlewareOnDeactivated()
78+
{
79+
$message = new DummyMessage('Hello');
80+
$envelope = Envelope::wrap($message);
81+
82+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
83+
$next->expects($this->once())->method('__invoke')->with($envelope)->willReturn('Hello from $next');
84+
85+
$middleware = $this->createMock(MiddlewareInterface::class);
86+
$middleware->expects($this->never())->method('handle');
87+
88+
$decorator = new ActivationMiddlewareDecorator($middleware, false);
89+
90+
$this->assertSame('Hello from $next', $decorator->handle($envelope, $next));
91+
}
92+
}

0 commit comments

Comments
 (0)
0