|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[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 Sonata\NotificationBundle\Tests\Iterator; |
|
13
|
|
|
|
|
14
|
|
|
use Interop\Amqp\AmqpConsumer; |
|
15
|
|
|
use Interop\Amqp\Impl\AmqpMessage; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Sonata\NotificationBundle\Iterator\AMQPMessageIterator; |
|
18
|
|
|
use Sonata\NotificationBundle\Iterator\MessageIteratorInterface; |
|
19
|
|
|
use Sonata\NotificationBundle\Model\Message; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @covers \Sonata\NotificationBundle\Iterator\AMQPMessageIterator |
|
23
|
|
|
*/ |
|
24
|
|
|
class AMQPMessageIteratorTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
public function testShouldImplementMessageIteratorInterface() |
|
27
|
|
|
{ |
|
28
|
|
|
$rc = new \ReflectionClass(AMQPMessageIterator::class); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertTrue($rc->implementsInterface(MessageIteratorInterface::class)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testCouldBeConstructedWithContextAsFirstArgument() |
|
34
|
|
|
{ |
|
35
|
|
|
new AMQPMessageIterator($this->createMock(AmqpConsumer::class)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testShouldIterateOverThreeMessagesAndExit() |
|
39
|
|
|
{ |
|
40
|
|
|
$firstMessage = new AmqpMessage('{"body": {"value": "theFirstMessageBody"}, "type": "aType", "state": "aState"}'); |
|
41
|
|
|
$secondMessage = new AmqpMessage('{"body": {"value": "theSecondMessageBody"}, "type": "aType", "state": "aState"}'); |
|
42
|
|
|
$thirdMessage = new AmqpMessage('{"body": {"value": "theThirdMessageBody"}, "type": "aType", "state": "aState"}'); |
|
43
|
|
|
|
|
44
|
|
|
$consumerMock = $this->createMock(AmqpConsumer::class); |
|
45
|
|
|
$consumerMock |
|
46
|
|
|
->expects($this->exactly(4)) |
|
47
|
|
|
->method('receive') |
|
48
|
|
|
->willReturnOnConsecutiveCalls($firstMessage, $secondMessage, $thirdMessage, null); |
|
49
|
|
|
|
|
50
|
|
|
$iterator = new AMQPMessageIterator($consumerMock); |
|
51
|
|
|
|
|
52
|
|
|
$values = []; |
|
53
|
|
|
foreach ($iterator as $message) { |
|
54
|
|
|
/* @var Message $message */ |
|
55
|
|
|
|
|
56
|
|
|
$this->assertInstanceOf(Message::class, $message); |
|
57
|
|
|
$this->assertInstanceOf(\Interop\Amqp\AmqpMessage::class, $message->getValue('interopMessage')); |
|
58
|
|
|
|
|
59
|
|
|
$values[] = $message->getValue('value'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals(['theFirstMessageBody', 'theSecondMessageBody', 'theThirdMessageBody'], $values); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|