Skip to content

Commit c5cc373

Browse files
committed
Binding object and test
1 parent a71652c commit c5cc373

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed

RabbitMq/Binding.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: brandon
5+
* Date: 6/24/15
6+
* Time: 3:49 PM
7+
*/
8+
9+
namespace OldSound\RabbitMqBundle\RabbitMq;
10+
11+
12+
class Binding extends BaseAmqp
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $exchange;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $destination;
23+
24+
/**
25+
* @var bool
26+
*/
27+
protected $destinationIsExchange = false;
28+
29+
/**
30+
* @var string
31+
*/
32+
protected $routingKey;
33+
34+
/**
35+
* @var bool
36+
*/
37+
protected $nowait = false;
38+
39+
/**
40+
* @var array
41+
*/
42+
protected $arguments;
43+
44+
/**
45+
* @return string
46+
*/
47+
public function getExchange()
48+
{
49+
return $this->exchange;
50+
}
51+
52+
/**
53+
* @param string $exchange
54+
*/
55+
public function setExchange($exchange)
56+
{
57+
$this->exchange = $exchange;
58+
}
59+
60+
/**
61+
* @return string
62+
*/
63+
public function getDestination()
64+
{
65+
return $this->destination;
66+
}
67+
68+
/**
69+
* @param bool $destination
70+
*/
71+
public function setDestination($destination)
72+
{
73+
$this->destination = $destination;
74+
}
75+
76+
/**
77+
* @return bool
78+
*/
79+
public function getDestinationIsExchange()
80+
{
81+
return $this->destinationIsExchange;
82+
}
83+
84+
/**
85+
* @param string $destinationIsExchange
86+
*/
87+
public function setDestinationIsExchange($destinationIsExchange)
88+
{
89+
$this->destinationIsExchange = $destinationIsExchange;
90+
}
91+
92+
/**
93+
* @return string
94+
*/
95+
public function getRoutingKey()
96+
{
97+
return $this->routingKey;
98+
}
99+
100+
/**
101+
* @param string $routingKey
102+
*/
103+
public function setRoutingKey($routingKey)
104+
{
105+
$this->routingKey = $routingKey;
106+
}
107+
108+
/**
109+
* @return boolean
110+
*/
111+
public function isNowait()
112+
{
113+
return $this->nowait;
114+
}
115+
116+
/**
117+
* @param boolean $nowait
118+
*/
119+
public function setNowait($nowait)
120+
{
121+
$this->nowait = $nowait;
122+
}
123+
124+
/**
125+
* @return array
126+
*/
127+
public function getArguments()
128+
{
129+
return $this->arguments;
130+
}
131+
132+
/**
133+
* @param array $arguments
134+
*/
135+
public function setArguments($arguments)
136+
{
137+
$this->arguments = $arguments;
138+
}
139+
140+
141+
/**
142+
* create bindings
143+
*
144+
* @return void
145+
*/
146+
public function setupFabric()
147+
{
148+
$method = ($this->destinationIsExchange) ? 'exchange_bind' : 'queue_bind';
149+
$channel = $this->getChannel();
150+
call_user_func(
151+
array($channel, $method),
152+
$this->destination,
153+
$this->exchange,
154+
$this->routingKey,
155+
$this->nowait,
156+
$this->arguments
157+
);
158+
}
159+
}

Tests/RabbitMq/BindingTest.php

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

0 commit comments

Comments
 (0)