1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Tests\Transport\Worker; |
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\Tests\BaseTestCase; |
10
|
|
|
use Cmobi\RabbitmqBundle\Transport\Worker\Task; |
11
|
|
|
|
12
|
|
|
class TaskTest extends BaseTestCase |
13
|
|
|
{ |
14
|
|
|
public function testGetQueueName() |
15
|
|
|
{ |
16
|
|
|
$taskClient = new Task('test', $this->getConnectionManagerMock(), 'test'); |
17
|
|
|
|
18
|
|
|
$this->assertEquals('test', $taskClient->getQueueName()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testGetFromName() |
22
|
|
|
{ |
23
|
|
|
$taskClient = new Task('test', $this->getConnectionManagerMock(), 'caller_test'); |
24
|
|
|
|
25
|
|
|
$this->assertEquals('caller_test', $taskClient->getFromName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return ConnectionManager |
30
|
|
|
*/ |
31
|
|
|
protected function getConnectionManagerMock() |
32
|
|
|
{ |
33
|
|
|
$connectionManagerMock = $this->getMockBuilder(ConnectionManager::class) |
34
|
|
|
->disableOriginalConstructor() |
35
|
|
|
->getMock(); |
36
|
|
|
$connectionManagerMock->method('getConnection') |
37
|
|
|
->willReturn($this->getConnectionMock()); |
38
|
|
|
|
39
|
|
|
return $connectionManagerMock; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return CmobiAMQPConnection |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
protected function getConnectionMock() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$connectionMock = $this->getMockBuilder(CmobiAMQPConnection::class) |
48
|
|
|
->disableOriginalConstructor() |
49
|
|
|
->getMock(); |
50
|
|
|
$connectionMock->method('isConnected') |
51
|
|
|
->willReturn(true); |
52
|
|
|
$connectionMock->method('reconnect') |
53
|
|
|
->willReturn(true); |
54
|
|
|
$connectionMock->method('channel') |
55
|
|
|
->willReturn($this->getChannelMock()); |
56
|
|
|
|
57
|
|
|
return $connectionMock; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return CmobiAMQPChannel |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
protected function getChannelMock() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$channelMock = $this->getMockBuilder(CmobiAMQPChannel::class) |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->getMock(); |
68
|
|
|
$channelMock |
69
|
|
|
->expects($this->any()) |
70
|
|
|
->method('basic_publish') |
71
|
|
|
->willReturn(true); |
72
|
|
|
$channelMock |
73
|
|
|
->method('basicConsume') |
74
|
|
|
->willReturn(true); |
75
|
|
|
|
76
|
|
|
return $channelMock; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $msg |
81
|
|
|
* @param null $correlationId |
82
|
|
|
* |
83
|
|
|
* @return CmobiAMQPMessage |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
protected function getCmobiAMQPMessage($msg = '', $correlationId = null) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$msgMock = $this->getMockBuilder(CmobiAMQPMessage::class) |
88
|
|
|
->disableOriginalConstructor() |
89
|
|
|
->getMock(); |
90
|
|
|
$msgMock->method('get') |
91
|
|
|
->willReturn('correlation_id') |
92
|
|
|
->willReturn($correlationId); |
93
|
|
|
$msgMock->method('getBody') |
94
|
|
|
->willReturn($msg); |
95
|
|
|
|
96
|
|
|
return $msgMock; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.