|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Tests\Iterator; |
|
15
|
|
|
|
|
16
|
|
|
use Interop\Amqp\AmqpConsumer; |
|
17
|
|
|
use Interop\Amqp\Impl\AmqpMessage; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Sonata\NotificationBundle\Iterator\AMQPMessageIterator; |
|
20
|
|
|
use Sonata\NotificationBundle\Iterator\MessageIteratorInterface; |
|
21
|
|
|
use Sonata\NotificationBundle\Model\Message; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @covers \Sonata\NotificationBundle\Iterator\AMQPMessageIterator |
|
25
|
|
|
*/ |
|
26
|
|
|
class AMQPMessageIteratorTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
public function testShouldImplementMessageIteratorInterface(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$rc = new \ReflectionClass(AMQPMessageIterator::class); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertTrue($rc->implementsInterface(MessageIteratorInterface::class)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @doesNotPerformAssertions |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testCouldBeConstructedWithContextAsFirstArgument(): void |
|
39
|
|
|
{ |
|
40
|
|
|
new AMQPMessageIterator($this->createMock(AmqpConsumer::class)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testShouldIterateOverThreeMessagesAndExit(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$firstMessage = new AmqpMessage('{"body": {"value": "theFirstMessageBody"}, "type": "aType", "state": "aState"}'); |
|
46
|
|
|
$secondMessage = new AmqpMessage('{"body": {"value": "theSecondMessageBody"}, "type": "aType", "state": "aState"}'); |
|
47
|
|
|
$thirdMessage = new AmqpMessage('{"body": {"value": "theThirdMessageBody"}, "type": "aType", "state": "aState"}'); |
|
48
|
|
|
|
|
49
|
|
|
$consumerMock = $this->createMock(AmqpConsumer::class); |
|
50
|
|
|
$consumerMock |
|
51
|
|
|
->expects($this->exactly(4)) |
|
52
|
|
|
->method('receive') |
|
53
|
|
|
->willReturnOnConsecutiveCalls($firstMessage, $secondMessage, $thirdMessage, null); |
|
54
|
|
|
|
|
55
|
|
|
$iterator = new AMQPMessageIterator($consumerMock); |
|
56
|
|
|
|
|
57
|
|
|
$values = []; |
|
58
|
|
|
foreach ($iterator as $message) { |
|
59
|
|
|
/* @var Message $message */ |
|
60
|
|
|
|
|
61
|
|
|
$this->assertInstanceOf(Message::class, $message); |
|
62
|
|
|
$this->assertInstanceOf(\Interop\Amqp\AmqpMessage::class, $message->getValue('interopMessage')); |
|
63
|
|
|
|
|
64
|
|
|
$values[] = $message->getValue('value'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->assertSame(['theFirstMessageBody', 'theSecondMessageBody', 'theThirdMessageBody'], $values); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param mixed $queueName |
|
72
|
|
|
* |
|
73
|
|
|
* @return AmqpConsumer|\PHPUnit_Framework_MockObject_MockObject |
|
74
|
|
|
*/ |
|
75
|
|
|
private function createConsumerStub($queueName = null) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$queue = $this->createMock(AmqpQueue::class); |
|
78
|
|
|
$queue |
|
79
|
|
|
->method('getQueueName') |
|
80
|
|
|
->willReturn($queueName) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
|
|
$consumer = $this->createMock(AmqpConsumer::class); |
|
84
|
|
|
$consumer |
|
85
|
|
|
->method('getQueue') |
|
86
|
|
|
->willReturn($queue) |
|
87
|
|
|
; |
|
88
|
|
|
|
|
89
|
|
|
return $consumer; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return AMQPChannel|\PHPUnit_Framework_MockObject_MockObject|AMQPChannel |
|
94
|
|
|
*/ |
|
95
|
|
|
private function createChannelMock() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
return $this->createMock(AMQPChannel::class); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|