AbstractMessageProcessorTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 70
dl 0
loc 124
rs 10
c 4
b 0
f 0
wmc 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$1 ➔ processMessage() 0 3 1
testSuccesfullyAck() 0 22 ?
A hp$4 ➔ processMessage() 0 4 1
A hp$4 ➔ testErrorNack() 0 27 1
testNackFromException() 0 22 ?
A hp$0 ➔ processMessage() 0 3 1
A hp$3 ➔ processMessage() 0 3 1
A hp$1 ➔ testNackFromException() 0 22 1
testErrorNack() 0 27 ?
testControlledNack() 0 22 ?
A hp$0 ➔ testSuccesfullyAck() 0 22 1
A hp$3 ➔ testMessageCounter() 0 21 1
testMessageCounter() 0 21 ?
A hp$2 ➔ processMessage() 0 3 1
A hp$2 ➔ testControlledNack() 0 22 1
1
<?php
2
namespace NeedleProject\LaravelRabbitMq\Processor;
3
4
use PhpAmqpLib\Channel\AMQPChannel;
5
use PhpAmqpLib\Message\AMQPMessage;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Log\LoggerInterface;
8
9
class AbstractMessageProcessorTest extends TestCase
10
{
11
    public function testSuccesfullyAck()
12
    {
13
        $messageProcessor = new class extends AbstractMessageProcessor {
14
            public function processMessage(AMQPMessage $message): bool
15
            {
16
                return true;
17
            }
18
        };
19
        $loggerMock = $this->createMock(LoggerInterface::class);
20
        $messageProcessor->setLogger($loggerMock);
21
22
        $channelMock = $this->createMock(AMQPChannel::class);
23
        $channelMock->expects($this->once())
24
            ->method('basic_ack')
25
            ->with('foo')
26
            ->willReturn(null);
27
28
        $amqpMessage = $this->createMock(AMQPMessage::class);
29
        $amqpMessage->delivery_info['channel'] = $channelMock;
0 ignored issues
show
Bug introduced by
Accessing delivery_info on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
30
        $amqpMessage->delivery_info['delivery_tag'] = 'foo';
31
32
        $messageProcessor->consume($amqpMessage);
33
    }
34
35
    public function testNackFromException()
36
    {
37
        $messageProcessor = new class extends AbstractMessageProcessor {
38
            public function processMessage(AMQPMessage $message): bool
39
            {
40
                throw new \Exception('foo');
41
            }
42
        };
43
        $loggerMock = $this->createMock(LoggerInterface::class);
44
        $messageProcessor->setLogger($loggerMock);
45
46
        $channelMock = $this->createMock(AMQPChannel::class);
47
        $channelMock->expects($this->once())
48
            ->method('basic_nack')
49
            ->with('foo')
50
            ->willReturn(null);
51
52
        $amqpMessage = $this->createMock(AMQPMessage::class);
53
        $amqpMessage->delivery_info['channel'] = $channelMock;
0 ignored issues
show
Bug introduced by
Accessing delivery_info on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
54
        $amqpMessage->delivery_info['delivery_tag'] = 'foo';
55
56
        $messageProcessor->consume($amqpMessage);
57
    }
58
59
    public function testControlledNack()
60
    {
61
        $messageProcessor = new class extends AbstractMessageProcessor {
62
            public function processMessage(AMQPMessage $message): bool
63
            {
64
                return false;
65
            }
66
        };
67
        $loggerMock = $this->createMock(LoggerInterface::class);
68
        $messageProcessor->setLogger($loggerMock);
69
70
        $channelMock = $this->createMock(AMQPChannel::class);
71
        $channelMock->expects($this->once())
72
            ->method('basic_nack')
73
            ->with('foo')
74
            ->willReturn(null);
75
76
        $amqpMessage = $this->createMock(AMQPMessage::class);
77
        $amqpMessage->delivery_info['channel'] = $channelMock;
0 ignored issues
show
Bug introduced by
Accessing delivery_info on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78
        $amqpMessage->delivery_info['delivery_tag'] = 'foo';
79
80
        $messageProcessor->consume($amqpMessage);
81
    }
82
83
    public function testMessageCounter()
84
    {
85
        $messageProcessor = new class extends AbstractMessageProcessor {
86
            public function processMessage(AMQPMessage $message): bool
87
            {
88
                return true;
89
            }
90
        };
91
        $loggerMock = $this->createMock(LoggerInterface::class);
92
        $messageProcessor->setLogger($loggerMock);
93
94
        $channelMock = $this->createMock(AMQPChannel::class);
95
        $amqpMessage = $this->createMock(AMQPMessage::class);
96
        $amqpMessage->delivery_info['channel'] = $channelMock;
0 ignored issues
show
Bug introduced by
Accessing delivery_info on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
97
        $amqpMessage->delivery_info['delivery_tag'] = 'foo';
98
99
        $messageProcessor->consume($amqpMessage);
100
        $messageProcessor->consume($amqpMessage);
101
        $messageProcessor->consume($amqpMessage);
102
103
        $this->assertEquals(3, $messageProcessor->getProcessedMessages());
104
    }
105
106
    public function testErrorNack()
107
    {
108
        $messageProcessor = new class extends AbstractMessageProcessor {
109
            public function processMessage(AMQPMessage $message): bool
110
            {
111
                // trigger nack
112
                return false;
113
            }
114
        };
115
        $loggerMock = $this->createMock(LoggerInterface::class);
116
        $messageProcessor->setLogger($loggerMock);
117
        $channelMock = $this->createMock(AMQPChannel::class);
118
        $channelMock->expects($this->atLeastOnce())
119
            ->method('basic_nack')
120
            ->will($this->throwException(new \RuntimeException('FooBar')));
121
122
        $amqpMessage = $this->createMock(AMQPMessage::class);
123
        $amqpMessage->delivery_info['channel'] = $channelMock;
0 ignored issues
show
Bug introduced by
Accessing delivery_info on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
124
        $amqpMessage->delivery_info['delivery_tag'] = 1;
125
        $amqpMessage->expects($this->atLeastOnce())
126
            ->method('getBody')
127
            ->willReturn('foo');
128
129
        $loggerMock->expects($this->atLeastOnce())
130
            ->method('debug')
131
            ->with('Did not processed with success message foo');
132
        $messageProcessor->consume($amqpMessage);
133
    }
134
}
135