1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RabbitMqModule; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
6
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
7
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
8
|
|
|
use RabbitMqModule\Options\Queue as QueueOptions; |
9
|
|
|
use RabbitMqModule\Options\Exchange as ExchangeOptions; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ProducerTest |
13
|
|
|
* @package RabbitMqModule |
14
|
|
|
*/ |
15
|
|
|
class ProducerTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
public function testProperties() |
18
|
|
|
{ |
19
|
|
|
/** @var AbstractConnection $connection */ |
20
|
|
|
$connection = static::getMockBuilder(AbstractConnection::class) |
21
|
|
|
->disableOriginalConstructor() |
22
|
|
|
->getMockForAbstractClass(); |
23
|
|
|
|
24
|
|
|
$producer = new Producer($connection); |
25
|
|
|
|
26
|
|
|
static::assertSame($connection, $producer->getConnection()); |
27
|
|
|
static::assertEquals('text/plain', $producer->getContentType()); |
28
|
|
|
static::assertEquals(2, $producer->getDeliveryMode()); |
29
|
|
|
|
30
|
|
|
$queueOptions = new QueueOptions(); |
31
|
|
|
$exchangeOptions = new ExchangeOptions(); |
32
|
|
|
|
33
|
|
|
$producer->setDeliveryMode(-1); |
34
|
|
|
$producer->setContentType('foo'); |
35
|
|
|
$producer->setQueueOptions($queueOptions); |
36
|
|
|
$producer->setExchangeOptions($exchangeOptions); |
37
|
|
|
$producer->setAutoSetupFabricEnabled(false); |
38
|
|
|
|
39
|
|
|
static::assertSame($connection, $producer->getConnection()); |
40
|
|
|
static::assertEquals('foo', $producer->getContentType()); |
41
|
|
|
static::assertEquals(-1, $producer->getDeliveryMode()); |
42
|
|
|
static::assertSame($queueOptions, $producer->getQueueOptions()); |
43
|
|
|
static::assertSame($exchangeOptions, $producer->getExchangeOptions()); |
44
|
|
|
static::assertFalse($producer->isAutoSetupFabricEnabled()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testSetupFabric() |
48
|
|
|
{ |
49
|
|
|
/** @var AbstractConnection $connection */ |
50
|
|
|
$connection = static::getMockBuilder(AbstractConnection::class) |
51
|
|
|
->disableOriginalConstructor() |
52
|
|
|
->getMockForAbstractClass(); |
53
|
|
|
$channel = static::getMockBuilder(AMQPChannel::class) |
54
|
|
|
->disableOriginalConstructor() |
55
|
|
|
->getMock(); |
56
|
|
|
|
57
|
|
|
$queueOptions = new QueueOptions(); |
58
|
|
|
$queueOptions->setName('foo'); |
59
|
|
|
$exchangeOptions = new ExchangeOptions(); |
60
|
|
|
|
61
|
|
|
$producer = new Producer($connection, $channel); |
62
|
|
|
$producer->setQueueOptions($queueOptions); |
63
|
|
|
$producer->setExchangeOptions($exchangeOptions); |
64
|
|
|
|
65
|
|
|
$channel->expects(static::once()) |
66
|
|
|
->method('exchange_declare'); |
67
|
|
|
$channel->expects(static::once()) |
68
|
|
|
->method('queue_declare'); |
69
|
|
|
|
70
|
|
|
static::assertSame($producer, $producer->setupFabric()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testPublish() |
74
|
|
|
{ |
75
|
|
|
/** @var AbstractConnection $connection */ |
76
|
|
|
$connection = static::getMockBuilder(AbstractConnection::class) |
77
|
|
|
->disableOriginalConstructor() |
78
|
|
|
->getMockForAbstractClass(); |
79
|
|
|
$channel = static::getMockBuilder(AMQPChannel::class) |
80
|
|
|
->disableOriginalConstructor() |
81
|
|
|
->getMock(); |
82
|
|
|
|
83
|
|
|
$queueOptions = new QueueOptions(); |
84
|
|
|
$queueOptions->setName('foo'); |
85
|
|
|
$exchangeOptions = new ExchangeOptions(); |
86
|
|
|
$exchangeOptions->setName('foo'); |
87
|
|
|
|
88
|
|
|
$producer = new Producer($connection, $channel); |
89
|
|
|
$producer->setQueueOptions($queueOptions); |
90
|
|
|
$producer->setExchangeOptions($exchangeOptions); |
91
|
|
|
|
92
|
|
|
$channel->expects(static::once()) |
93
|
|
|
->method('exchange_declare'); |
94
|
|
|
$channel->expects(static::once()) |
95
|
|
|
->method('queue_declare'); |
96
|
|
|
|
97
|
|
|
$channel->expects(static::once()) |
98
|
|
|
->method('basic_publish') |
99
|
|
|
->with(static::callback( |
100
|
|
|
function ($subject) { |
101
|
|
|
return $subject instanceof AMQPMessage |
102
|
|
|
&& $subject->body === 'test-body' |
103
|
|
|
&& $subject->get_properties() === [ |
104
|
|
|
'content_type' => 'foo/bar', |
105
|
|
|
'delivery_mode' => 2, |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
), 'foo', 'test-key'); |
109
|
|
|
|
110
|
|
|
static::assertSame($producer, $producer->publish('test-body', 'test-key', ['content_type' => 'foo/bar'])); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|