Completed
Push — master ( 166fc0...765fa7 )
by
unknown
13:29
created

AMQPBackendDispatcherTest::testQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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
    protected 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
    public function testDefaultQueueNotFound()
45
    {
46
        $mock = $this->getMockQueue('foo', 'message.type.foo', $this->never());
47
        $fooBackend = array('type' => 'message.type.foo', 'backend' => $mock);
48
        $dispatcher = $this->getDispatcher(array($fooBackend));
49
50
        $this->setExpectedException('\Sonata\NotificationBundle\Exception\BackendNotFoundException');
51
        $dispatcher->createAndPublish('some.other.type', array());
52
    }
53
54
    public function testInvalidQueue()
55
    {
56
        $mock = $this->getMockQueue('foo', 'message.type.bar');
57
        $dispatcher = $this->getDispatcher(
58
            array(array('type' => 'bar', 'backend' => $mock)),
59
            array(array('queue' => 'foo', 'routing_key' => 'message.type.bar'))
60
        );
61
62
        $this->setExpectedException('\Sonata\NotificationBundle\Exception\BackendNotFoundException');
63
        $dispatcher->createAndPublish('message.type.bar', array());
64
    }
65
66
    public function testAllQueueInitializeOnce()
67
    {
68
        $queues = array(
69
            array('queue' => 'foo', 'routing_key' => 'message.type.foo'),
70
            array('queue' => 'bar', 'routing_key' => 'message.type.bar'),
71
            array('queue' => 'baz', 'routing_key' => 'message.type.baz'),
72
        );
73
74
        $backends = array();
75
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
83
        $dispatcher = $this->getDispatcher($backends, $queues);
84
85
        $dispatcher->createAndPublish('message.type.foo', array());
86
        $dispatcher->createAndPublish('message.type.foo', array());
87
    }
88
89
    protected function getMockQueue($queue, $type, $called = null)
0 ignored issues
show
Unused Code introduced by
The parameter $queue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $methods = array('createAndPublish', 'initialize');
92
        $args = array('', 'foo', false, 'message.type.foo');
93
        $mock = $this->getMockBuilder('Sonata\NotificationBundle\Backend\AMQPBackend')
94
                     ->setConstructorArgs($args)
95
                     ->setMethods($methods)
96
                     ->getMock();
97
98
        if ($called !== null) {
99
            $mock->expects($called)
100
                ->method('createAndPublish')
101
            ;
102
        }
103
104
        return $mock;
105
    }
106
107
    protected function getDispatcher(array $backends, array $queues = array(array('queue' => 'foo', 'routing_key' => 'message.type.foo')))
108
    {
109
        $settings = array(
110
                'host' => 'foo',
111
                'port' => 'port',
112
                'user' => 'user',
113
                'pass' => 'pass',
114
                'vhost' => '/',
115
        );
116
117
        return new AMQPBackendDispatcher($settings, $queues, 'default', $backends);
118
    }
119
}
120