Completed
Push — 3.x-dev-kit ( cc4dbe )
by
unknown
03:15
created

AMQPBackendTest::testInvalidQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
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\Notification;
13
14
use Sonata\NotificationBundle\Backend\AMQPBackendDispatcher;
15
16
class AMQPBackendTest 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 \RuntimeException
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 \RuntimeException
57
     */
58
    public function testInvalidQueue()
59
    {
60
        $mock = $this->getMockQueue('foo', 'message.type.bar');
61
        $dispatcher = $this->getDispatcher(array(array('type' => 'bar', 'backend' => $mock)), 'foo', 'message.type.bar');
62
        $dispatcher->createAndPublish('message.type.bar', array());
63
    }
64
65
    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...
66
    {
67
        $methods = array('createAndPublish');
68
        $args = array(array(), '', 'foo', 'message.type.foo');
69
        $mock = $this->getMock('Sonata\NotificationBundle\Backend\AMQPBackend', $methods, $args);
70
71
        if ($called !== null) {
72
            $mock->expects($called)
73
                ->method('createAndPublish')
74
            ;
75
        }
76
77
        return $mock;
78
    }
79
80
    protected function getDispatcher(array $backends, $queue = 'foo', $key = 'message.type.foo')
81
    {
82
        $queues = array(array('queue' => $queue, 'routing_key' => $key));
83
        $settings = array(
84
                'host' => 'foo',
85
                'port' => 'port',
86
                'user' => 'user',
87
                'pass' => 'pass',
88
                'vhost' => '/',
89
        );
90
91
        return new AMQPBackendDispatcher($settings, $queues, 'default', $backends);
92
    }
93
}
94