1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\Tests\RabbitMq; |
4
|
|
|
|
5
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\Binding; |
6
|
|
|
use PHPUnit\Framework\Assert; |
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class BindingTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
protected function getBinding($amqpConnection, $amqpChannel) |
13
|
|
|
{ |
14
|
|
|
return new Binding($amqpConnection, $amqpChannel); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return MockObject |
19
|
|
|
*/ |
20
|
|
|
protected function prepareAMQPConnection() |
21
|
|
|
{ |
22
|
|
|
return $this->getMockBuilder('\PhpAmqpLib\Connection\AMQPStreamConnection') |
23
|
|
|
->disableOriginalConstructor() |
24
|
|
|
->getMock(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function prepareAMQPChannel($channelId = null) |
28
|
|
|
{ |
29
|
|
|
$channelMock = $this->getMockBuilder('\PhpAmqpLib\Channel\AMQPChannel') |
30
|
|
|
->disableOriginalConstructor() |
31
|
|
|
->getMock(); |
32
|
|
|
|
33
|
|
|
$channelMock->expects($this->any()) |
34
|
|
|
->method('getChannelId') |
35
|
|
|
->willReturn($channelId); |
36
|
|
|
return $channelMock; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testQueueBind() |
40
|
|
|
{ |
41
|
|
|
$ch = $this->prepareAMQPChannel('channel_id'); |
42
|
|
|
$con = $this->prepareAMQPConnection(); |
43
|
|
|
|
44
|
|
|
$source = 'example_source'; |
45
|
|
|
$destination = 'example_destination'; |
46
|
|
|
$key = 'example_key'; |
47
|
|
|
$ch->expects($this->once()) |
48
|
|
|
->method('queue_bind') |
49
|
|
|
->will($this->returnCallback(function ($d, $s, $k, $n, $a) use ($destination, $source, $key) { |
50
|
|
|
Assert::assertSame($destination, $d); |
51
|
|
|
Assert::assertSame($source, $s); |
52
|
|
|
Assert::assertSame($key, $k); |
53
|
|
|
Assert::assertFalse($n); |
54
|
|
|
Assert::assertNull($a); |
55
|
|
|
})); |
56
|
|
|
|
57
|
|
|
$binding = $this->getBinding($con, $ch); |
58
|
|
|
$binding->setExchange($source); |
59
|
|
|
$binding->setDestination($destination); |
60
|
|
|
$binding->setRoutingKey($key); |
61
|
|
|
$binding->setupFabric(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testExhangeBind() |
65
|
|
|
{ |
66
|
|
|
$ch = $this->prepareAMQPChannel('channel_id'); |
67
|
|
|
$con = $this->prepareAMQPConnection(); |
68
|
|
|
|
69
|
|
|
$source = 'example_source'; |
70
|
|
|
$destination = 'example_destination'; |
71
|
|
|
$key = 'example_key'; |
72
|
|
|
$ch->expects($this->once()) |
73
|
|
|
->method('exchange_bind') |
74
|
|
|
->will($this->returnCallback(function ($d, $s, $k, $n, $a) use ($destination, $source, $key) { |
75
|
|
|
Assert::assertSame($destination, $d); |
76
|
|
|
Assert::assertSame($source, $s); |
77
|
|
|
Assert::assertSame($key, $k); |
78
|
|
|
Assert::assertFalse($n); |
79
|
|
|
Assert::assertNull($a); |
80
|
|
|
})); |
81
|
|
|
|
82
|
|
|
$binding = $this->getBinding($con, $ch); |
83
|
|
|
$binding->setExchange($source); |
84
|
|
|
$binding->setDestination($destination); |
85
|
|
|
$binding->setRoutingKey($key); |
86
|
|
|
$binding->setDestinationIsExchange(true); |
87
|
|
|
$binding->setupFabric(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|