BaseAmqpTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 39
c 3
b 1
f 0
dl 0
loc 71
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testNotLazyConnection() 0 14 1
A testDispatchEvent() 0 21 1
A invokeMethod() 0 7 1
A testLazyConnection() 0 14 1
1
<?php
2
3
namespace OldSound\RabbitMqBundle\Tests\RabbitMq;
4
5
use PHPUnit\Framework\MockObject\MockObject;
6
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
7
use OldSound\RabbitMqBundle\Event\AMQPEvent;
8
use OldSound\RabbitMqBundle\RabbitMq\BaseAmqp;
9
use OldSound\RabbitMqBundle\RabbitMq\Consumer;
10
use PHPUnit\Framework\TestCase;
11
12
class BaseAmqpTest extends TestCase
13
{
14
    public function testLazyConnection()
15
    {
16
        $connection = $this->getMockBuilder('PhpAmqpLib\Connection\AbstractConnection')
17
            ->disableOriginalConstructor()
18
            ->getMock();
19
20
        $connection
21
            ->method('connectOnConstruct')
22
            ->willReturn(false);
23
        $connection
24
            ->expects(static::never())
25
            ->method('channel');
26
27
        new Consumer($connection, null);
28
    }
29
30
    public function testNotLazyConnection()
31
    {
32
        $connection = $this->getMockBuilder('PhpAmqpLib\Connection\AbstractConnection')
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
36
        $connection
37
            ->method('connectOnConstruct')
38
            ->willReturn(true);
39
        $connection
40
            ->expects(static::once())
41
            ->method('channel');
42
43
        new Consumer($connection, null);
44
    }
45
46
    public function testDispatchEvent()
47
    {
48
        /** @var BaseAmqp|MockObject $baseAmqpConsumer */
49
        $baseAmqpConsumer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\BaseAmqp')
50
            ->disableOriginalConstructor()
51
            ->getMock();
52
53
        $eventDispatcher = $this->getMockBuilder('Symfony\Contracts\EventDispatcher\EventDispatcherInterface')
54
            ->disableOriginalConstructor()
55
            ->getMock();
56
57
        $baseAmqpConsumer->expects($this->atLeastOnce())
58
            ->method('getEventDispatcher')
59
            ->willReturn($eventDispatcher);
60
61
        $eventDispatcher->expects($this->once())
62
            ->method('dispatch')
63
            ->with(new AMQPEvent(), AMQPEvent::ON_CONSUME)
64
            ->willReturn(new AMQPEvent());
65
66
        $this->invokeMethod('dispatchEvent', $baseAmqpConsumer, [AMQPEvent::ON_CONSUME, new AMQPEvent()]);
67
    }
68
69
    /**
70
     * @param string $name
71
     * @param MockObject $obj
72
     * @param array $params
73
     *
74
     * @return mixed
75
     */
76
    protected function invokeMethod($name, $obj, $params)
77
    {
78
        $class = new \ReflectionClass(get_class($obj));
79
        $method = $class->getMethod($name);
80
        $method->setAccessible(true);
81
82
        return $method->invokeArgs($obj, $params);
83
    }
84
}
85