Passed
Push — master ( 905821...42a403 )
by Mr
07:23
created

testReceiveEnvelopeWithMissingChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.9
c 2
b 0
f 0
ccs 12
cts 12
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/message-bus project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Tests\MessageBus;
10
11
use Daikon\MessageBus\Channel\ChannelInterface;
12
use Daikon\MessageBus\Channel\ChannelMap;
13
use Daikon\MessageBus\Envelope;
14
use Daikon\MessageBus\EnvelopeInterface;
15
use Daikon\MessageBus\Error\ChannelUnknown;
16
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
17
use Daikon\MessageBus\MessageBus;
18
use Daikon\MessageBus\MessageInterface;
19
use Daikon\Metadata\Metadata;
20
use PHPUnit\Framework\TestCase;
21
22
final class MessageBusTest extends TestCase
23
{
24
    const CHANNEL_NAME = 'commands';
25
26 1
    public function testPublish(): void
27
    {
28
        /** @var MessageInterface $messageMock */
29 1
        $messageMock = $this->createMock(MessageInterface::class);
30
        $envelopeExpectation = $this->callback(function (EnvelopeInterface $envelope) use ($messageMock) {
31 1
            return $messageMock === $envelope->getMessage();
32 1
        });
33 1
        $channelMock = $this->createMock(ChannelInterface::class);
34 1
        $channelMock->expects($this->once())->method('getKey')->willReturn(self::CHANNEL_NAME);
35 1
        $channelMock->expects($this->once())->method('publish')->with($envelopeExpectation);
36
        /** @var ChannelInterface $channelMock */
37 1
        $messageBus = new MessageBus(new ChannelMap([$channelMock]));
38
39 1
        $this->assertNull($messageBus->publish($messageMock, self::CHANNEL_NAME));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $messageBus->publish($me...ck, self::CHANNEL_NAME) targeting Daikon\MessageBus\MessageBus::publish() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40 1
    }
41
42 1
    public function testReceive(): void
43
    {
44
        /** @var MessageInterface $messageMock */
45 1
        $messageMock = $this->createMock(MessageInterface::class);
46 1
        $envelopeExpectation = Envelope::wrap(
47 1
            $messageMock,
48 1
            Metadata::makeEmpty()->with(ChannelInterface::METADATA_KEY, self::CHANNEL_NAME)
49
        );
50 1
        $channelMock = $this->createMock(ChannelInterface::class);
51 1
        $channelMock->expects($this->once())->method('getKey')->willReturn(self::CHANNEL_NAME);
52 1
        $channelMock->expects($this->once())->method('receive')->with($envelopeExpectation);
53
        /** @var ChannelInterface $channelMock */
54 1
        $messageBus = new MessageBus(new ChannelMap([$channelMock]));
55
56 1
        $this->assertNull($messageBus->receive($envelopeExpectation));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $messageBus->receive($envelopeExpectation) targeting Daikon\MessageBus\MessageBus::receive() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57 1
    }
58
59 1
    public function testPublishToNonExistingChannel(): void
60
    {
61
        /** @var MessageInterface $messageMock */
62 1
        $messageMock = $this->createMock(MessageInterface::class);
63 1
        $channelMock = $this->createMock(ChannelInterface::class);
64 1
        $channelMock->expects($this->once())->method('getKey')->willReturn(self::CHANNEL_NAME);
65
        /** @var ChannelInterface $channelMock */
66 1
        $messageBus = new MessageBus(new ChannelMap([$channelMock]));
67
68 1
        $this->expectException(ChannelUnknown::class);
69 1
        $this->expectExceptionMessage("Channel 'events' has not been registered on message bus.");
70 1
        $this->expectExceptionCode(0);
71
72 1
        $messageBus->publish($messageMock, 'events');
73
    } // @codeCoverageIgnore
74
75 1
    public function testReceiveFromNonExistingChannel(): void
76
    {
77
        /** @var MessageInterface $messageMock */
78 1
        $messageMock = $this->createMock(MessageInterface::class);
79 1
        $envelope = Envelope::wrap(
80 1
            $messageMock,
81 1
            Metadata::makeEmpty()->with(ChannelInterface::METADATA_KEY, 'events')
82
        );
83 1
        $channelMock = $this->createMock(ChannelInterface::class);
84 1
        $channelMock->expects($this->once())->method('getKey')->willReturn(self::CHANNEL_NAME);
85
        /** @var ChannelInterface $channelMock */
86 1
        $messageBus = new MessageBus(new ChannelMap([$channelMock]));
87
88 1
        $this->expectException(ChannelUnknown::class);
89 1
        $this->expectExceptionMessage("Channel 'events' has not been registered on message bus.");
90 1
        $this->expectExceptionCode(0);
91
92 1
        $messageBus->receive($envelope);
93
    } // @codeCoverageIgnore
94
95 1
    public function testReceiveEnvelopeWithMissingChannel(): void
96
    {
97
        /** @var MessageInterface $messageMock */
98 1
        $messageMock = $this->createMock(MessageInterface::class);
99 1
        $envelope = Envelope::wrap($messageMock);
100 1
        $channelMock = $this->createMock(ChannelInterface::class);
101 1
        $channelMock->expects($this->once())->method('getKey')->willReturn(self::CHANNEL_NAME);
102 1
        $messageBus = new MessageBus(new ChannelMap([$channelMock]));
103
104 1
        $this->expectException(EnvelopeNotAcceptable::class);
105 1
        $this->expectExceptionMessage(
106 1
            "Channel key '".ChannelInterface::METADATA_KEY."' missing in metadata of ".
107 1
            "Envelope '{$envelope->getUuid()->toString()}' received on message bus."
108
        );
109 1
        $this->expectExceptionCode(EnvelopeNotAcceptable::CHANNEL_KEY_MISSING);
110
111 1
        $messageBus->receive($envelope);
112
    } // @codeCoverageIgnore
113
}
114