|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Transport\Enhancers; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Messenger\Transport\ReceiverInterface; |
| 16 | +use Symfony\Component\Messenger\Tests\Fixtures\CallbackReceiver; |
| 17 | +use Symfony\Component\Messenger\Transport\Enhancers\InfiniteLoopReceiver; |
| 18 | + |
| 19 | +class InfiniteLoopReceiverTest extends TestCase |
| 20 | +{ |
| 21 | + public function testReceiverReceivesUntilStopIsCalled() |
| 22 | + { |
| 23 | + $i = 0; |
| 24 | + $decoratedReceiver = null; |
| 25 | + $receiver = new CallbackReceiver(function ($handler) use (&$i, &$decoratedReceiver) { |
| 26 | + $i += 1; |
| 27 | + if ($i === 3) { |
| 28 | + $decoratedReceiver->stop(); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + $decoratedReceiver = new InfiniteLoopReceiver($receiver); |
| 33 | + $decoratedReceiver->receive(function() {}); |
| 34 | + $this->assertEquals(3, $i); |
| 35 | + } |
| 36 | + |
| 37 | + public function testReceiverDelegatesStopToInnerReceiver() |
| 38 | + { |
| 39 | + $decoratedReceiver = null; |
| 40 | + $receiver = new class($decoratedReceiver) implements ReceiverInterface { |
| 41 | + public $wasStopped = 0; |
| 42 | + private $decoratedReceiver; |
| 43 | + |
| 44 | + public function __construct(&$decoratedReceiver) { |
| 45 | + $this->decoratedReceiver = &$decoratedReceiver; |
| 46 | + } |
| 47 | + |
| 48 | + public function receive(callable $handle): void { |
| 49 | + $this->decoratedReceiver->stop(); |
| 50 | + } |
| 51 | + public function stop(): void { |
| 52 | + $this->wasStopped += 1; |
| 53 | + } |
| 54 | + }; |
| 55 | + $decoratedReceiver = new InfiniteLoopReceiver($receiver); |
| 56 | + $decoratedReceiver->receive(function() {}); |
| 57 | + $this->assertEquals(1, $receiver->wasStopped); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @expectedException Exception |
| 62 | + * @expectedExceptionMessage test |
| 63 | + */ |
| 64 | + public function testReceiverRethrowsAnyExceptions() |
| 65 | + { |
| 66 | + $decoratedReceiver = null; |
| 67 | + $receiver = new CallbackReceiver(function ($handler) { |
| 68 | + throw new \Exception('test'); |
| 69 | + }); |
| 70 | + |
| 71 | + $decoratedReceiver = new InfiniteLoopReceiver($receiver); |
| 72 | + $decoratedReceiver->receive(function() {}); |
| 73 | + } |
| 74 | +} |
0 commit comments