forked from php-amqplib/Thumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProducerTest.php
169 lines (144 loc) · 4.68 KB
/
ProducerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Thumper\Test;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Exception\AMQPInvalidArgumentException;
use PhpAmqpLib\Exception\AMQPOutOfBoundsException;
use PhpAmqpLib\Exception\AMQPRuntimeException;
use PhpAmqpLib\Message\AMQPMessage;
use Thumper\Producer;
class ProducerTest extends BaseTest
{
/**
* @var Producer
*/
private $producer;
/**
* @var \PhpAmqpLib\Connection\AbstractConnection|\PHPUnit_Framework_MockObject_MockObject
*/
private $mockConnection;
/**
* @var AMQPChannel|\PHPUnit_Framework_MockObject_MockObject
*/
private $mockChannel;
protected function setUp(): void
{
$this->mockConnection = $this->getMockConnection();
$this->mockChannel = $this->getMockChannel();
$this->mockConnection
->expects($this->once())
->method('channel')
->willReturn($this->mockChannel);
$this->producer = new Producer($this->mockConnection);
}
/**
* @test
* @throws \Thumper\Exception\Exception
*/
public function publishHappyPath()
{
$body = uniqid('body', true);
$exchangeName = uniqid('exchangeName', true);
$exchangeType = uniqid('exchangeType', true);
$this->producer
->setExchangeOptions([
'name' => $exchangeName,
'type' => $exchangeType
]);
$this->mockChannel
->expects($this->once())
->method('exchange_declare')
->with($exchangeName, $exchangeType, false, true, false);
$this->mockChannel
->expects($this->once())
->method('basic_publish')
->with(
$this->callback(function (AMQPMessage $message) use ($body) {
return $message->body === $body;
}),
$exchangeName,
''
);
$this->producer
->publish($body);
}
/**
* @param \Exception $exception
* @test
* @dataProvider declareExchangeDataProvider
* @throws \Thumper\Exception\Exception
*/
public function publishThrowsExceptionWhenDeclaringExchange(\Exception $exception)
{
$this->setExpectedException(get_class($exception), $exception->getMessage());
$body = uniqid('body', true);
$exchangeName = uniqid('exchangeName', true);
$exchangeType = uniqid('exchangeType', true);
$this->producer
->setExchangeOptions([
'name' => $exchangeName,
'type' => $exchangeType
]);
$this->mockChannel
->expects($this->once())
->method('exchange_declare')
->with($exchangeName, $exchangeType, false, true, false)
->willThrowException($exception);
$this->mockChannel
->expects($this->never())
->method('basic_publish');
$this->producer
->publish($body);
}
/**
* @param \Exception $exception
* @test
* @dataProvider publishMessageExceptionDataProvider
* @throws \Thumper\Exception\Exception
*/
public function publishThrowsExceptionWhenPublishingMessage(\Exception $exception)
{
$this->setExpectedException(get_class($exception), $exception->getMessage());
$body = uniqid('body', true);
$exchangeName = uniqid('exchangeName', true);
$exchangeType = uniqid('exchangeType', true);
$this->producer
->setExchangeOptions([
'name' => $exchangeName,
'type' => $exchangeType
]);
$this->mockChannel
->expects($this->once())
->method('exchange_declare')
->with($exchangeName, $exchangeType, false, true, false);
$this->mockChannel
->expects($this->once())
->method('basic_publish')
->with(
$this->callback(function (AMQPMessage $message) use ($body) {
return $message->body === $body;
}),
$exchangeName,
''
)
->willThrowException($exception);
$this->producer
->publish($body);
}
/**
* @return array
*/
public function declareExchangeDataProvider()
{
return [
[new AMQPOutOfBoundsException('Out of Bounds')],
[new AMQPRuntimeException('Runtime Exception')]
];
}
public function publishMessageExceptionDataProvider()
{
return [
[new AMQPInvalidArgumentException('Invalid Argument')],
[new AMQPRuntimeException('Runtime Exception')]
];
}
}