Completed
Push — 2.x-dev-kit ( 8d77e1 )
by
unknown
28:22 queued 25:50
created

AMQPBackendTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 78
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 2
A getMockQueue() 0 14 2
A getDispatcher() 0 13 1
A testQueue() 0 10 1
A testDefaultQueue() 0 7 1
A testDefaultQueueNotFound() 0 7 1
A testInvalidQueue() 0 6 1
1
<?php
2
3
namespace Sonata\NotificationBundle\Tests\Notification;
4
5
use Sonata\NotificationBundle\Backend\AMQPBackendDispatcher;
6
7
class AMQPBackendTest extends \PHPUnit_Framework_TestCase
8
{
9
    public function setUp()
10
    {
11
        if (!class_exists('PhpAmqpLib\Message\AMQPMessage')) {
12
            $this->markTestSkipped('AMQP Lib not installed');
13
        }
14
    }
15
16
    protected function getMockQueue($queue, $type, $called = null)
17
    {
18
        $methods = array('createAndPublish');
19
        $args = array(array(), '', 'foo', 'message.type.foo');
20
        $mock = $this->getMock('Sonata\NotificationBundle\Backend\AMQPBackend', $methods, $args);
21
22
        if ($called !== null) {
23
            $mock->expects($called)
24
                ->method('createAndPublish')
25
            ;
26
        }
27
28
        return $mock;
29
    }
30
31
    protected function getDispatcher(array $backends, $queue = 'foo', $key = 'message.type.foo')
32
    {
33
        $queues = array(array('queue' => $queue, 'routing_key' => $key));
34
        $settings = array(
35
                'host'  => 'foo',
36
                'port'  => 'port',
37
                'user'  => 'user',
38
                'pass'  => 'pass',
39
                'vhost' => '/',
40
        );
41
42
        return new AMQPBackendDispatcher($settings, $queues, 'default', $backends);
43
    }
44
45
    public function testQueue()
46
    {
47
        $mock = $this->getMockQueue('foo', 'message.type.foo', $this->once());
48
        $mock2 = $this->getMockQueue('bar', 'message.type.foo', $this->never());
49
        $fooBackend = array('type' => 'message.type.foo', 'backend' => $mock);
50
        $barBackend = array('type' => 'message.type.bar', 'backend' => $mock2);
51
        $backends = array($fooBackend, $barBackend);
52
        $dispatcher = $this->getDispatcher($backends);
53
        $dispatcher->createAndPublish('message.type.foo', array());
54
    }
55
56
    public function testDefaultQueue()
57
    {
58
        $mock = $this->getMockQueue('foo', 'message.type.foo', $this->once());
59
        $fooBackend = array('type' => 'default', 'backend' => $mock);
60
        $dispatcher = $this->getDispatcher(array($fooBackend));
61
        $dispatcher->createAndPublish('some.other.type', array());
62
    }
63
64
    /**
65
     * @expectedException \RuntimeException
66
     */
67
    public function testDefaultQueueNotFound()
68
    {
69
        $mock = $this->getMockQueue('foo', 'message.type.foo', $this->never());
70
        $fooBackend = array('type' => 'message.type.foo', 'backend' => $mock);
71
        $dispatcher = $this->getDispatcher(array($fooBackend));
72
        $dispatcher->createAndPublish('some.other.type', array());
73
    }
74
75
    /**
76
     * @expectedException \RuntimeException
77
     */
78
    public function testInvalidQueue()
79
    {
80
        $mock = $this->getMockQueue('foo', 'message.type.bar');
81
        $dispatcher = $this->getDispatcher(array(array('type' => 'bar', 'backend' => $mock)), 'foo', 'message.type.bar');
82
        $dispatcher->createAndPublish('message.type.bar', array());
83
    }
84
}
85