Skip to content

Commit 3593c1e

Browse files
committed
Add SetupFabricTest For BaseAmqpTest
1 parent dd24f2b commit 3593c1e

2 files changed

Lines changed: 196 additions & 36 deletions

File tree

src/BaseAmqp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use RabbitMqModule\Options\Exchange as ExchangeOptions;
1010
use RabbitMqModule\Options\Queue as QueueOptions;
1111
use RabbitMqModule\Service\SetupFabricAwareInterface;
12-
use AMQPTable;
12+
use PhpAmqpLib\Wire\AMQPTable;
1313

1414
abstract class BaseAmqp implements SetupFabricAwareInterface
1515
{
@@ -159,7 +159,7 @@ protected function declareExchange(ExchangeOptions $options = null): void
159159
foreach ($binds as $bind) {
160160
$this->declareExchange($bind->getExchange());
161161
$routingKeys = $bind->getRoutingKeys();
162-
if (! \count($routingKeys)) {
162+
if (empty($routingKeys)) {
163163
$routingKeys = [''];
164164
}
165165
foreach ($routingKeys as $routingKey) {
@@ -200,7 +200,7 @@ protected function declareQueue(): void
200200
);
201201

202202
$routingKeys = $queueOptions->getRoutingKeys();
203-
if (! \count($routingKeys)) {
203+
if (empty($routingKeys)) {
204204
$routingKeys = [''];
205205
}
206206
foreach ($routingKeys as $routingKey) {

tests/unit/BaseAmqpTest.php

Lines changed: 193 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,95 @@
22

33
namespace RabbitMqModule;
44

5-
class BaseAmqpTest extends \PHPUnit\Framework\TestCase
5+
use PhpAmqpLib\Channel\AMQPChannel;
6+
use PhpAmqpLib\Connection\AbstractConnection;
7+
use PhpAmqpLib\Wire\AMQPTable;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
use RabbitMqModule\Options\Exchange as ExchangeOptions;
11+
use RabbitMqModule\Options\Queue as QueueOptions;
12+
13+
class BaseAmqpTest extends TestCase
614
{
715
public function testConstructor()
816
{
9-
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
17+
$connection = $this->getMockBuilder(AbstractConnection::class)
1018
->disableOriginalConstructor()
1119
->getMock();
12-
$channel = $this->getMockBuilder('PhpAmqpLib\\Channel\\AMQPChannel')
20+
21+
$channel = $this->getMockBuilder(AMQPChannel::class)
1322
->disableOriginalConstructor()
1423
->getMock();
15-
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
24+
25+
/** @var MockObject|BaseAmqp $baseAmqp */
26+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
1627
->setConstructorArgs([$connection])
1728
->setMethods(['__destruct'])
1829
->getMock();
1930

20-
$baseAmqp->method('__destruct');
21-
22-
$connection->expects(static::once())->method('channel')->willReturn($channel);
31+
$connection->expects(static::once())
32+
->method('channel')
33+
->willReturn($channel);
2334

24-
/** @var \RabbitMqModule\BaseAmqp $baseAmqp */
2535
static::assertEquals($channel, $baseAmqp->getChannel());
2636
}
2737

2838
public function testSetChannel()
2939
{
30-
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
40+
$connection = $this->getMockBuilder(AbstractConnection::class)
3141
->disableOriginalConstructor()
3242
->getMock();
33-
$channel = $this->getMockBuilder('PhpAmqpLib\\Channel\\AMQPChannel')
43+
44+
$channel = $this->getMockBuilder(AMQPChannel::class)
3445
->disableOriginalConstructor()
3546
->getMock();
36-
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
47+
48+
/** @var MockObject|BaseAmqp $baseAmqp */
49+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
3750
->setConstructorArgs([$connection])
3851
->setMethods(['__destruct'])
3952
->getMock();
4053

41-
$baseAmqp->method('__destruct');
42-
43-
/* @var \RabbitMqModule\BaseAmqp $baseAmqp */
54+
/** @var MockObject|AMQPChannel $channel */
4455
$baseAmqp->setChannel($channel);
45-
static::assertEquals($channel, $baseAmqp->getChannel());
46-
}
47-
48-
protected static function getMethod($name)
49-
{
50-
$class = new \ReflectionClass('RabbitMqModule\\BaseAmqp');
51-
$method = $class->getMethod($name);
52-
$method->setAccessible(true);
5356

54-
return $method;
57+
static::assertEquals($channel, $baseAmqp->getChannel());
5558
}
5659

5760
public function testDestruct()
5861
{
59-
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
62+
$connection = $this->getMockBuilder(AbstractConnection::class)
6063
->disableOriginalConstructor()
6164
->getMock();
62-
$channel = $this->getMockBuilder('PhpAmqpLib\\Channel\\AMQPChannel')
65+
66+
$channel = $this->getMockBuilder(AMQPChannel::class)
6367
->disableOriginalConstructor()
6468
->getMock();
65-
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
69+
70+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
6671
->setConstructorArgs([$connection])
6772
->setMethods(null)
6873
->getMock();
6974

70-
$connection->expects(static::once())->method('isConnected')->willReturn(true);
71-
$connection->expects(static::once())->method('close');
75+
$connection->expects(static::once())
76+
->method('isConnected')
77+
->willReturn(true);
7278

79+
$connection->expects(static::once())->method('close');
7380
$channel->expects(static::once())->method('close');
7481

75-
/* @var \RabbitMqModule\BaseAmqp $baseAmqp */
82+
/* @var BaseAmqp $baseAmqp */
7683
$baseAmqp->setChannel($channel);
7784
$baseAmqp->__destruct();
7885
}
7986

8087
public function testReconnect()
8188
{
82-
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
89+
$connection = $this->getMockBuilder(AbstractConnection::class)
8390
->disableOriginalConstructor()
8491
->getMock();
85-
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
92+
93+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
8694
->setConstructorArgs([$connection])
8795
->setMethods(['__destruct'])
8896
->getMock();
@@ -95,10 +103,11 @@ public function testReconnect()
95103

96104
public function testReconnectWhenConnected()
97105
{
98-
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
106+
$connection = $this->getMockBuilder(AbstractConnection::class)
99107
->disableOriginalConstructor()
100108
->getMock();
101-
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
109+
110+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
102111
->setConstructorArgs([$connection])
103112
->setMethods(['__destruct'])
104113
->getMock();
@@ -108,4 +117,155 @@ public function testReconnectWhenConnected()
108117

109118
$baseAmqp->reconnect();
110119
}
120+
121+
public function testQueueOptions()
122+
{
123+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
124+
->disableOriginalConstructor()
125+
->setMethods(['__destruct'])
126+
->getMock();
127+
128+
$options = new QueueOptions();
129+
130+
$baseAmqp->setQueueOptions($options);
131+
static::assertEquals($options, $baseAmqp->getQueueOptions());
132+
}
133+
134+
public function testExchangeOptions()
135+
{
136+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
137+
->disableOriginalConstructor()
138+
->setMethods(['__destruct'])
139+
->getMock();
140+
141+
$options = new ExchangeOptions();
142+
143+
$baseAmqp->setExchangeOptions($options);
144+
static::assertEquals($options, $baseAmqp->getExchangeOptions());
145+
}
146+
147+
/**
148+
* @param bool $isEnabled
149+
*
150+
* @dataProvider getDataProviderForAutoSetupFabricEnabled
151+
*/
152+
public function testAutoSetupFabricEnabled(bool $isEnabled)
153+
{
154+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
155+
->disableOriginalConstructor()
156+
->setMethods(['__destruct'])
157+
->getMock();
158+
159+
$baseAmqp->setAutoSetupFabricEnabled($isEnabled);
160+
static::assertEquals($isEnabled, $baseAmqp->isAutoSetupFabricEnabled());
161+
}
162+
163+
public function getDataProviderForAutoSetupFabricEnabled(): array
164+
{
165+
return [
166+
[true],
167+
[false],
168+
];
169+
}
170+
171+
/**
172+
* @param array $exchangeData
173+
* @param array $queueData
174+
*
175+
* @dataProvider getDataProviderForSetupFabric
176+
*/
177+
public function testSetupFabricWithoutBinds(array $exchangeData, array $queueData)
178+
{
179+
$channel = $this->getMockBuilder(AMQPChannel::class)
180+
->disableOriginalConstructor()
181+
->setMethods(['exchange_declare', 'queue_declare', 'queue_bind'])
182+
->getMock();
183+
$channel->expects(static::once())
184+
->method('exchange_declare')
185+
->with(
186+
$exchangeData['name'],
187+
$exchangeData['type'],
188+
$exchangeData['passive'],
189+
$exchangeData['durable'],
190+
$exchangeData['auto_delete'],
191+
$exchangeData['internal'],
192+
$exchangeData['no_wait'],
193+
$exchangeData['arguments'],
194+
$exchangeData['ticket']
195+
);
196+
$channel->expects(static::once())
197+
->method('queue_declare')
198+
->with(
199+
$queueData['name'],
200+
$queueData['passive'],
201+
$queueData['durable'],
202+
$queueData['exclusive'],
203+
$queueData['auto_delete'],
204+
$queueData['no_wait'],
205+
new AMQPTable($queueData['arguments']),
206+
$queueData['ticket']
207+
)
208+
->willReturn([$queueData['name']]);
209+
210+
$routingKeys = $queueData['routing_keys'] ?? [''];
211+
$channel->expects(static::exactly(count($routingKeys)))
212+
->method('queue_bind')
213+
->withConsecutive(...array_map(function ($routingKey) use ($queueData, $exchangeData) {
214+
return [
215+
$queueData['name'],
216+
$exchangeData['name'],
217+
$routingKey,
218+
];
219+
}, $routingKeys));
220+
221+
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
222+
->disableOriginalConstructor()
223+
->setMethods(['__destruct', 'getChannel'])
224+
->getMock();
225+
$baseAmqp->expects(static::any())
226+
->method('getChannel')
227+
->willReturn($channel);
228+
229+
$queueOptions = new QueueOptions($queueData);
230+
$baseAmqp->setQueueOptions($queueOptions);
231+
232+
$exchangeOptions = new ExchangeOptions($exchangeData);
233+
$baseAmqp->setExchangeOptions($exchangeOptions);
234+
235+
$baseAmqp->setupFabric();
236+
237+
// declare only once
238+
$baseAmqp->setupFabric();
239+
}
240+
241+
public function getDataProviderForSetupFabric(): array
242+
{
243+
return [
244+
[
245+
[
246+
'declare' => true,
247+
'name' => 'some_exchange_name',
248+
'type' => 'some_exchange_type',
249+
'passive' => true,
250+
'durable' => true,
251+
'auto_delete' => true,
252+
'internal' => true,
253+
'no_wait' => true,
254+
'arguments' => [],
255+
'ticket' => 1,
256+
],
257+
[
258+
'name' => 'some_queue_name',
259+
'passive' => true,
260+
'durable' => true,
261+
'exclusive' => true,
262+
'auto_delete' => true,
263+
'no_wait' => true,
264+
'arguments' => ['some_argument'],
265+
'ticket' => 1,
266+
'routing_keys' => ['some_key'],
267+
],
268+
],
269+
];
270+
}
111271
}

0 commit comments

Comments
 (0)