Skip to content

Commit d8cfd73

Browse files
committed
Reverted 4961528
1 parent 4961528 commit d8cfd73

File tree

2 files changed

+36
-198
lines changed

2 files changed

+36
-198
lines changed

src/BaseAmqp.php

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

1413
abstract class BaseAmqp implements SetupFabricAwareInterface
1514
{
@@ -159,7 +158,7 @@ protected function declareExchange(ExchangeOptions $options = null): void
159158
foreach ($binds as $bind) {
160159
$this->declareExchange($bind->getExchange());
161160
$routingKeys = $bind->getRoutingKeys();
162-
if (empty($routingKeys)) {
161+
if (! \count($routingKeys)) {
163162
$routingKeys = [''];
164163
}
165164
foreach ($routingKeys as $routingKey) {
@@ -186,7 +185,6 @@ protected function declareQueue(): void
186185
}
187186

188187
$exchangeOptions = $this->getExchangeOptions();
189-
$arguments = $queueOptions->getArguments();
190188

191189
[$queueName] = $this->getChannel()->queue_declare(
192190
$queueOptions->getName(),
@@ -195,12 +193,12 @@ protected function declareQueue(): void
195193
$queueOptions->isExclusive(),
196194
$queueOptions->isAutoDelete(),
197195
$queueOptions->isNoWait(),
198-
$arguments ? new AMQPTable($arguments) : [],
196+
$queueOptions->getArguments(),
199197
$queueOptions->getTicket()
200198
);
201199

202200
$routingKeys = $queueOptions->getRoutingKeys();
203-
if (empty($routingKeys)) {
201+
if (! \count($routingKeys)) {
204202
$routingKeys = [''];
205203
}
206204
foreach ($routingKeys as $routingKey) {

tests/unit/BaseAmqpTest.php

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

33
namespace RabbitMqModule;
44

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
5+
class BaseAmqpTest extends \PHPUnit\Framework\TestCase
146
{
157
public function testConstructor()
168
{
17-
$connection = $this->getMockBuilder(AbstractConnection::class)
9+
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
1810
->disableOriginalConstructor()
1911
->getMock();
20-
21-
$channel = $this->getMockBuilder(AMQPChannel::class)
12+
$channel = $this->getMockBuilder('PhpAmqpLib\\Channel\\AMQPChannel')
2213
->disableOriginalConstructor()
2314
->getMock();
24-
25-
/** @var MockObject|BaseAmqp $baseAmqp */
26-
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
15+
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
2716
->setConstructorArgs([$connection])
2817
->setMethods(['__destruct'])
2918
->getMock();
3019

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

24+
/** @var \RabbitMqModule\BaseAmqp $baseAmqp */
3525
static::assertEquals($channel, $baseAmqp->getChannel());
3626
}
3727

3828
public function testSetChannel()
3929
{
40-
$connection = $this->getMockBuilder(AbstractConnection::class)
30+
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
4131
->disableOriginalConstructor()
4232
->getMock();
43-
44-
$channel = $this->getMockBuilder(AMQPChannel::class)
33+
$channel = $this->getMockBuilder('PhpAmqpLib\\Channel\\AMQPChannel')
4534
->disableOriginalConstructor()
4635
->getMock();
47-
48-
/** @var MockObject|BaseAmqp $baseAmqp */
49-
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
36+
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
5037
->setConstructorArgs([$connection])
5138
->setMethods(['__destruct'])
5239
->getMock();
5340

54-
/** @var MockObject|AMQPChannel $channel */
55-
$baseAmqp->setChannel($channel);
41+
$baseAmqp->method('__destruct');
5642

43+
/* @var \RabbitMqModule\BaseAmqp $baseAmqp */
44+
$baseAmqp->setChannel($channel);
5745
static::assertEquals($channel, $baseAmqp->getChannel());
5846
}
5947

48+
protected static function getMethod($name)
49+
{
50+
$class = new \ReflectionClass('RabbitMqModule\\BaseAmqp');
51+
$method = $class->getMethod($name);
52+
$method->setAccessible(true);
53+
54+
return $method;
55+
}
56+
6057
public function testDestruct()
6158
{
62-
$connection = $this->getMockBuilder(AbstractConnection::class)
59+
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
6360
->disableOriginalConstructor()
6461
->getMock();
65-
66-
$channel = $this->getMockBuilder(AMQPChannel::class)
62+
$channel = $this->getMockBuilder('PhpAmqpLib\\Channel\\AMQPChannel')
6763
->disableOriginalConstructor()
6864
->getMock();
69-
70-
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
65+
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
7166
->setConstructorArgs([$connection])
7267
->setMethods(null)
7368
->getMock();
7469

75-
$connection->expects(static::once())
76-
->method('isConnected')
77-
->willReturn(true);
78-
70+
$connection->expects(static::once())->method('isConnected')->willReturn(true);
7971
$connection->expects(static::once())->method('close');
72+
8073
$channel->expects(static::once())->method('close');
8174

82-
/* @var BaseAmqp $baseAmqp */
75+
/* @var \RabbitMqModule\BaseAmqp $baseAmqp */
8376
$baseAmqp->setChannel($channel);
8477
$baseAmqp->__destruct();
8578
}
8679

8780
public function testReconnect()
8881
{
89-
$connection = $this->getMockBuilder(AbstractConnection::class)
82+
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
9083
->disableOriginalConstructor()
9184
->getMock();
92-
93-
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
85+
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
9486
->setConstructorArgs([$connection])
9587
->setMethods(['__destruct'])
9688
->getMock();
@@ -103,11 +95,10 @@ public function testReconnect()
10395

10496
public function testReconnectWhenConnected()
10597
{
106-
$connection = $this->getMockBuilder(AbstractConnection::class)
98+
$connection = $this->getMockBuilder('PhpAmqpLib\\Connection\\AbstractConnection')
10799
->disableOriginalConstructor()
108100
->getMock();
109-
110-
$baseAmqp = $this->getMockBuilder(BaseAmqp::class)
101+
$baseAmqp = $this->getMockBuilder('RabbitMqModule\\BaseAmqp')
111102
->setConstructorArgs([$connection])
112103
->setMethods(['__destruct'])
113104
->getMock();
@@ -117,155 +108,4 @@ public function testReconnectWhenConnected()
117108

118109
$baseAmqp->reconnect();
119110
}
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-
}
271111
}

0 commit comments

Comments
 (0)