|
| 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