|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\Tests\RabbitMq; |
|
4
|
|
|
|
|
5
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\RpcClient; |
|
6
|
|
|
use PhpAmqpLib\Exception\AMQPTimeoutException; |
|
7
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class RpcClientTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testProcessMessageWithCustomUnserializer() |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var RpcClient $client */ |
|
15
|
|
|
$client = $this->getMockBuilder('\OldSound\RabbitMqBundle\RabbitMq\RpcClient') |
|
16
|
|
|
->setMethods(array('sendReply', 'maybeStopConsumer')) |
|
17
|
|
|
->disableOriginalConstructor() |
|
18
|
|
|
->getMock(); |
|
19
|
|
|
/** @var AMQPMessage $message */ |
|
20
|
|
|
$message = $this->getMockBuilder('\PhpAmqpLib\Message\AMQPMessage') |
|
21
|
|
|
->setMethods(array('get')) |
|
22
|
|
|
->setConstructorArgs(array('message')) |
|
23
|
|
|
->getMock(); |
|
24
|
|
|
$serializer = $this->getMockBuilder('\Symfony\Component\Serializer\SerializerInterface') |
|
25
|
|
|
->setMethods(array('serialize', 'deserialize')) |
|
26
|
|
|
->getMock(); |
|
27
|
|
|
$serializer->expects($this->once())->method('deserialize')->with('message', 'json', null); |
|
28
|
|
|
$client->initClient(true); |
|
29
|
|
|
$client->setUnserializer(function($data) use ($serializer) { |
|
30
|
|
|
$serializer->deserialize($data, 'json',''); |
|
31
|
|
|
}); |
|
32
|
|
|
$client->processMessage($message); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testProcessMessageWithNotifyMethod() |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var RpcClient $client */ |
|
38
|
|
|
$client = $this->getMockBuilder('\OldSound\RabbitMqBundle\RabbitMq\RpcClient') |
|
39
|
|
|
->setMethods(array('sendReply', 'maybeStopConsumer')) |
|
40
|
|
|
->disableOriginalConstructor() |
|
41
|
|
|
->getMock(); |
|
42
|
|
|
$expectedNotify = 'message'; |
|
43
|
|
|
/** @var AMQPMessage $message */ |
|
44
|
|
|
$message = $this->getMockBuilder('\PhpAmqpLib\Message\AMQPMessage') |
|
45
|
|
|
->setMethods(array('get')) |
|
46
|
|
|
->setConstructorArgs(array($expectedNotify)) |
|
47
|
|
|
->getMock(); |
|
48
|
|
|
$notified = false; |
|
49
|
|
|
$client->notify(function ($message) use (&$notified) { |
|
50
|
|
|
$notified = $message; |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
$client->initClient(false); |
|
54
|
|
|
$client->processMessage($message); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertSame($expectedNotify, $notified); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testInvalidParameterOnNotify() |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var RpcClient $client */ |
|
62
|
|
|
$client = $this->getMockBuilder('\OldSound\RabbitMqBundle\RabbitMq\RpcClient') |
|
63
|
|
|
->setMethods(array('sendReply', 'maybeStopConsumer')) |
|
64
|
|
|
->disableOriginalConstructor() |
|
65
|
|
|
->getMock(); |
|
66
|
|
|
|
|
67
|
|
|
$this->expectException('\InvalidArgumentException'); |
|
68
|
|
|
|
|
69
|
|
|
$client->notify('not a callable'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testChannelCancelOnGetRepliesException() |
|
73
|
|
|
{ |
|
74
|
|
|
$client = $this->getMockBuilder('\OldSound\RabbitMqBundle\RabbitMq\RpcClient') |
|
75
|
|
|
->setMethods(null) |
|
76
|
|
|
->disableOriginalConstructor() |
|
77
|
|
|
->getMock(); |
|
78
|
|
|
|
|
79
|
|
|
$channel = $this->createMock('\PhpAmqpLib\Channel\AMQPChannel'); |
|
80
|
|
|
$channel->expects($this->any()) |
|
81
|
|
|
->method('getChannelId') |
|
82
|
|
|
->willReturn('test'); |
|
83
|
|
|
$channel->expects($this->once()) |
|
84
|
|
|
->method('wait') |
|
85
|
|
|
->willThrowException(new AMQPTimeoutException()); |
|
86
|
|
|
|
|
87
|
|
|
$this->expectException('\PhpAmqpLib\Exception\AMQPTimeoutException'); |
|
88
|
|
|
|
|
89
|
|
|
$channel->expects($this->once()) |
|
90
|
|
|
->method('basic_cancel'); |
|
91
|
|
|
|
|
92
|
|
|
$client->setChannel($channel); |
|
93
|
|
|
$client->addRequest('a', 'b', 'c'); |
|
94
|
|
|
|
|
95
|
|
|
$client->getReplies(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|