1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Tests\Transport\Rpc; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPChannel; |
6
|
|
|
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPConnection; |
7
|
|
|
use Cmobi\RabbitmqBundle\Connection\ConnectionManager; |
8
|
|
|
use Cmobi\RabbitmqBundle\Queue\CmobiAMQPMessage; |
9
|
|
|
use Cmobi\RabbitmqBundle\Transport\Rpc\RpcClient; |
10
|
|
|
use Cmobi\RabbitmqBundle\Tests\BaseTestCase; |
11
|
|
|
|
12
|
|
|
class RpcClientTest extends BaseTestCase |
13
|
|
|
{ |
14
|
|
|
public function testGetQueueName() |
15
|
|
|
{ |
16
|
|
|
$rpcClient = new RpcClient('test', $this->getConnectionManagerMock()); |
17
|
|
|
|
18
|
|
|
$this->assertEquals('test', $rpcClient->getQueueName()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testRefreshChannel() |
22
|
|
|
{ |
23
|
|
|
$rpcClient = new RpcClient('test', $this->getConnectionManagerMock()); |
24
|
|
|
|
25
|
|
|
$this->assertInstanceOf(CmobiAMQPChannel::class, $rpcClient->refreshChannel()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGetChannel() |
29
|
|
|
{ |
30
|
|
|
$rpcClient = new RpcClient('test', $this->getConnectionManagerMock()); |
31
|
|
|
|
32
|
|
|
$this->assertInstanceOf(CmobiAMQPChannel::class, $rpcClient->refreshChannel()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testGetFromName() |
36
|
|
|
{ |
37
|
|
|
$rpcClient = new RpcClient('test', $this->getConnectionManagerMock(), 'caller_test'); |
38
|
|
|
|
39
|
|
|
$this->assertEquals('caller_test', $rpcClient->getFromName()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testGetResponse() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$rpcClient = new RpcClient('test', $this->getConnectionManagerMock(), 'caller_test'); |
45
|
|
|
|
46
|
|
|
/** @Todo prevent infinite while - improve it */ |
47
|
|
|
$rpcClient->setResponse('testGetResponse() - OK'); |
48
|
|
|
|
49
|
|
|
$rpcClient->publish('test'); |
50
|
|
|
$rpcClient->onResponse( |
51
|
|
|
$this->getCmobiAMQPMessage('testGetResponse() - OK', |
52
|
|
|
$rpcClient->getCurrentCorrelationId()) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->assertEquals('testGetResponse() - OK', $rpcClient->getResponse()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGenerateCorrelationId() |
59
|
|
|
{ |
60
|
|
|
$rpcClient = new RpcClient('test', $this->getConnectionManagerMock(), 'caller_test'); |
61
|
|
|
|
62
|
|
|
$this->assertRegExp(sprintf('/%s/', $rpcClient->getCurrentCorrelationId()), $rpcClient->getQueueName()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testGetCurrentCorrelationId() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$rpcClient = new RpcClient('test', $this->getConnectionManagerMock(), 'caller_test'); |
68
|
|
|
/** @Todo prevent infinite while - improve it */ |
69
|
|
|
$rpcClient->setResponse('testGetResponse() - OK'); |
70
|
|
|
|
71
|
|
|
$rpcClient->publish('test'); |
72
|
|
|
$rpcClient->onResponse( |
73
|
|
|
$this->getCmobiAMQPMessage('testGetResponse() - OK', |
74
|
|
|
$rpcClient->getCurrentCorrelationId()) |
75
|
|
|
); |
76
|
|
|
$this->assertNotNull($rpcClient->getCurrentCorrelationId()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return ConnectionManager |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
protected function getConnectionManagerMock() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$connectionManagerMock = $this->getMockBuilder(ConnectionManager::class) |
85
|
|
|
->disableOriginalConstructor() |
86
|
|
|
->getMock(); |
87
|
|
|
$connectionManagerMock->method('getConnection') |
88
|
|
|
->willReturn($this->getConnectionMock()); |
89
|
|
|
|
90
|
|
|
return $connectionManagerMock; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return CmobiAMQPConnection |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
protected function getConnectionMock() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$connectionMock = $this->getMockBuilder(CmobiAMQPConnection::class) |
99
|
|
|
->disableOriginalConstructor() |
100
|
|
|
->getMock(); |
101
|
|
|
$connectionMock->method('isConnected') |
102
|
|
|
->willReturn(true); |
103
|
|
|
$connectionMock->method('reconnect') |
104
|
|
|
->willReturn(true); |
105
|
|
|
$connectionMock->method('channel') |
106
|
|
|
->willReturn($this->getChannelMock()); |
107
|
|
|
|
108
|
|
|
return $connectionMock; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return CmobiAMQPChannel |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
protected function getChannelMock() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$channelMock = $this->getMockBuilder(CmobiAMQPChannel::class) |
117
|
|
|
->disableOriginalConstructor() |
118
|
|
|
->getMock(); |
119
|
|
|
$channelMock |
120
|
|
|
->expects($this->any()) |
121
|
|
|
->method('basic_publish') |
122
|
|
|
->willReturn(true); |
123
|
|
|
$channelMock |
124
|
|
|
->method('basicConsume') |
125
|
|
|
->willReturn(true); |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
return $channelMock; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $msg |
133
|
|
|
* @param null $correlationId |
134
|
|
|
* @return CmobiAMQPMessage |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
protected function getCmobiAMQPMessage($msg = '', $correlationId = null) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$msgMock = $this->getMockBuilder(CmobiAMQPMessage::class) |
139
|
|
|
->disableOriginalConstructor() |
140
|
|
|
->getMock(); |
141
|
|
|
$msgMock->method('get') |
142
|
|
|
->willReturn('correlation_id') |
143
|
|
|
->willReturn($correlationId); |
144
|
|
|
$msgMock->method('getBody') |
145
|
|
|
->willReturn($msg); |
146
|
|
|
|
147
|
|
|
return $msgMock; |
148
|
|
|
} |
149
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.