Completed
Pull Request — master (#281)
by Grégoire
01:45
created

AMQPMessageIteratorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testShouldImplementMessageIteratorInterface() 0 6 1
A testCouldBeConstructedWithChannelAndContextAsArguments() 0 4 1
B testShouldIterateOverThreeMessagesAndExit() 0 27 2
A createConsumerStub() 0 18 1
A createChannelMock() 0 4 1
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\Iterator;
13
14
use Interop\Amqp\AmqpConsumer;
15
use Interop\Amqp\AmqpQueue;
16
use Interop\Amqp\Impl\AmqpMessage;
17
use PhpAmqpLib\Channel\AMQPChannel;
18
use PHPUnit\Framework\TestCase;
19
use Sonata\NotificationBundle\Iterator\AMQPMessageIterator;
20
use Sonata\NotificationBundle\Iterator\MessageIteratorInterface;
21
use Sonata\NotificationBundle\Model\Message;
22
23
/**
24
 * @covers \Sonata\NotificationBundle\Iterator\AMQPMessageIterator
25
 */
26
class AMQPMessageIteratorTest extends TestCase
27
{
28
    public function testShouldImplementMessageIteratorInterface()
29
    {
30
        $rc = new \ReflectionClass(AMQPMessageIterator::class);
31
32
        $this->assertTrue($rc->implementsInterface(MessageIteratorInterface::class));
33
    }
34
35
    public function testCouldBeConstructedWithChannelAndContextAsArguments()
36
    {
37
        new AMQPMessageIterator($this->createChannelMock(), $this->createConsumerStub());
38
    }
39
40
    public function testShouldIterateOverThreeMessagesAndExit()
41
    {
42
        $firstMessage = new AmqpMessage('{"body": {"value": "theFirstMessageBody"}, "type": "aType", "state": "aState"}');
43
        $secondMessage = new AmqpMessage('{"body": {"value": "theSecondMessageBody"}, "type": "aType", "state": "aState"}');
44
        $thirdMessage = new AmqpMessage('{"body": {"value": "theThirdMessageBody"}, "type": "aType", "state": "aState"}');
45
46
        $consumerMock = $this->createConsumerStub('aQueueName');
47
        $consumerMock
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Interop\Amqp\AmqpConsumer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
            ->expects($this->exactly(4))
49
            ->method('receive')
50
            ->willReturnOnConsecutiveCalls($firstMessage, $secondMessage, $thirdMessage, null);
51
52
        $iterator = new AMQPMessageIterator($this->createChannelMock(), $consumerMock);
53
54
        $values = [];
55
        foreach ($iterator as $message) {
56
            /* @var Message $message */
57
58
            $this->assertInstanceOf(Message::class, $message);
59
            $this->assertInstanceOf(\Interop\Amqp\AmqpMessage::class, $message->getValue('interopMessage'));
60
            $this->assertInstanceOf(\PhpAmqpLib\Message\AMQPMessage::class, $message->getValue('AMQMessage'));
61
62
            $values[] = $message->getValue('value');
63
        }
64
65
        $this->assertEquals(['theFirstMessageBody', 'theSecondMessageBody', 'theThirdMessageBody'], $values);
66
    }
67
68
    /**
69
     * @param mixed $queueName
70
     *
71
     * @return AmqpConsumer|\PHPUnit_Framework_MockObject_MockObject
72
     */
73
    private function createConsumerStub($queueName = null)
74
    {
75
        $queue = $this->createMock(AmqpQueue::class);
76
        $queue
77
            ->expects($this->any())
78
            ->method('getQueueName')
79
            ->willReturn($queueName)
80
        ;
81
82
        $consumer = $this->createMock(AmqpConsumer::class);
83
        $consumer
84
            ->expects($this->any())
85
            ->method('getQueue')
86
            ->willReturn($queue)
87
        ;
88
89
        return $consumer;
90
    }
91
92
    /**
93
     * @return AMQPChannel|\PHPUnit_Framework_MockObject_MockObject|AMQPChannel
94
     */
95
    private function createChannelMock()
96
    {
97
        return $this->createMock(AMQPChannel::class);
98
    }
99
}
100