|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\Tests\Backend; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\NotificationBundle\Backend\AMQPBackendDispatcher; |
|
15
|
|
|
|
|
16
|
|
|
class AMQPBackendDispatcherTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
if (!class_exists('PhpAmqpLib\Message\AMQPMessage')) { |
|
21
|
|
|
$this->markTestSkipped('AMQP Lib not installed'); |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testQueue() |
|
26
|
|
|
{ |
|
27
|
|
|
$mock = $this->getMockQueue('foo', 'message.type.foo', $this->once()); |
|
28
|
|
|
$mock2 = $this->getMockQueue('bar', 'message.type.foo', $this->never()); |
|
29
|
|
|
$fooBackend = array('type' => 'message.type.foo', 'backend' => $mock); |
|
30
|
|
|
$barBackend = array('type' => 'message.type.bar', 'backend' => $mock2); |
|
31
|
|
|
$backends = array($fooBackend, $barBackend); |
|
32
|
|
|
$dispatcher = $this->getDispatcher($backends); |
|
33
|
|
|
$dispatcher->createAndPublish('message.type.foo', array()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testDefaultQueue() |
|
37
|
|
|
{ |
|
38
|
|
|
$mock = $this->getMockQueue('foo', 'message.type.foo', $this->once()); |
|
39
|
|
|
$fooBackend = array('type' => 'default', 'backend' => $mock); |
|
40
|
|
|
$dispatcher = $this->getDispatcher(array($fooBackend)); |
|
41
|
|
|
$dispatcher->createAndPublish('some.other.type', array()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @expectedException \Sonata\NotificationBundle\Exception\BackendNotFoundException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testDefaultQueueNotFound() |
|
48
|
|
|
{ |
|
49
|
|
|
$mock = $this->getMockQueue('foo', 'message.type.foo', $this->never()); |
|
50
|
|
|
$fooBackend = array('type' => 'message.type.foo', 'backend' => $mock); |
|
51
|
|
|
$dispatcher = $this->getDispatcher(array($fooBackend)); |
|
52
|
|
|
$dispatcher->createAndPublish('some.other.type', array()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @expectedException \Sonata\NotificationBundle\Exception\BackendNotFoundException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testInvalidQueue() |
|
59
|
|
|
{ |
|
60
|
|
|
$mock = $this->getMockQueue('foo', 'message.type.bar'); |
|
61
|
|
|
$dispatcher = $this->getDispatcher( |
|
62
|
|
|
array(array('type' => 'bar', 'backend' => $mock)), |
|
63
|
|
|
array(array('queue' => 'foo', 'routing_key' => 'message.type.bar')) |
|
64
|
|
|
); |
|
65
|
|
|
$dispatcher->createAndPublish('message.type.bar', array()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testAllQueueInitializeOnce() |
|
69
|
|
|
{ |
|
70
|
|
|
$queues = array( |
|
71
|
|
|
array('queue' => 'foo', 'routing_key' => 'message.type.foo'), |
|
72
|
|
|
array('queue' => 'bar', 'routing_key' => 'message.type.bar'), |
|
73
|
|
|
array('queue' => 'baz', 'routing_key' => 'message.type.baz'), |
|
74
|
|
|
); |
|
75
|
|
|
$backends = array(); |
|
76
|
|
|
foreach ($queues as $queue) { |
|
77
|
|
|
$mock = $this->getMockQueue($queue['queue'], $queue['routing_key']); |
|
78
|
|
|
$mock->expects($this->once()) |
|
79
|
|
|
->method('initialize'); |
|
80
|
|
|
$backends[] = array('type' => $queue['routing_key'], 'backend' => $mock); |
|
81
|
|
|
} |
|
82
|
|
|
$dispatcher = $this->getDispatcher($backends, $queues); |
|
83
|
|
|
$dispatcher->createAndPublish('message.type.foo', array()); |
|
84
|
|
|
$dispatcher->createAndPublish('message.type.foo', array()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getMockQueue($queue, $type, $called = null) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$methods = array('createAndPublish', 'initialize'); |
|
90
|
|
|
$args = array('', 'foo', false, 'message.type.foo'); |
|
91
|
|
|
$mock = $this->getMockBuilder('Sonata\NotificationBundle\Backend\AMQPBackend') |
|
92
|
|
|
->setConstructorArgs($args) |
|
93
|
|
|
->setMethods($methods) |
|
94
|
|
|
->getMock(); |
|
95
|
|
|
|
|
96
|
|
|
if ($called !== null) { |
|
97
|
|
|
$mock->expects($called) |
|
98
|
|
|
->method('createAndPublish') |
|
99
|
|
|
; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $mock; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function getDispatcher(array $backends, array $queues = array(array('queue' => 'foo', 'routing_key' => 'message.type.foo'))) |
|
106
|
|
|
{ |
|
107
|
|
|
$settings = array( |
|
108
|
|
|
'host' => 'foo', |
|
109
|
|
|
'port' => 'port', |
|
110
|
|
|
'user' => 'user', |
|
111
|
|
|
'pass' => 'pass', |
|
112
|
|
|
'vhost' => '/', |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
return new AMQPBackendDispatcher($settings, $queues, 'default', $backends); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.