testReceiveFromNonExistingChannel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
c 3
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.9332
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\Exception\ChannelUnknown;
16
use Daikon\MessageBus\Exception\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 1
        $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('publish')->with($envelopeExpectation);
35 1
        $messageBus = new MessageBus(new ChannelMap([self::CHANNEL_NAME => $channelMock]));
36
37 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...
38 1
    }
39
40 1
    public function testReceive(): void
41
    {
42
        /** @var MessageInterface $messageMock */
43 1
        $messageMock = $this->createMock(MessageInterface::class);
44 1
        $envelopeExpectation = Envelope::wrap(
45 1
            $messageMock,
46 1
            Metadata::makeEmpty()->with(ChannelInterface::METADATA_KEY, self::CHANNEL_NAME)
47
        );
48 1
        $channelMock = $this->createMock(ChannelInterface::class);
49 1
        $channelMock->expects($this->once())->method('receive')->with($envelopeExpectation);
50
        /** @var ChannelInterface $channelMock */
51 1
        $messageBus = new MessageBus(new ChannelMap([self::CHANNEL_NAME => $channelMock]));
52
53 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...
54 1
    }
55
56 1
    public function testPublishToNonExistingChannel(): void
57
    {
58
        /** @var MessageInterface $messageMock */
59 1
        $messageMock = $this->createMock(MessageInterface::class);
60 1
        $channelMock = $this->createMock(ChannelInterface::class);
61 1
        $messageBus = new MessageBus(new ChannelMap([self::CHANNEL_NAME => $channelMock]));
62
63 1
        $this->expectException(ChannelUnknown::class);
64 1
        $this->expectExceptionMessage("Channel 'events' has not been registered on message bus.");
65 1
        $this->expectExceptionCode(0);
66
67 1
        $messageBus->publish($messageMock, 'events');
68
    }
69
70 1
    public function testReceiveFromNonExistingChannel(): void
71
    {
72
        /** @var MessageInterface $messageMock */
73 1
        $messageMock = $this->createMock(MessageInterface::class);
74 1
        $envelope = Envelope::wrap(
75 1
            $messageMock,
76 1
            Metadata::makeEmpty()->with(ChannelInterface::METADATA_KEY, 'events')
77
        );
78 1
        $channelMock = $this->createMock(ChannelInterface::class);
79 1
        $messageBus = new MessageBus(new ChannelMap([self::CHANNEL_NAME => $channelMock]));
80
81 1
        $this->expectException(ChannelUnknown::class);
82 1
        $this->expectExceptionMessage("Channel 'events' has not been registered on message bus.");
83 1
        $this->expectExceptionCode(0);
84
85 1
        $messageBus->receive($envelope);
86
    }
87
88 1
    public function testReceiveEnvelopeWithMissingChannel(): void
89
    {
90
        /** @var MessageInterface $messageMock */
91 1
        $messageMock = $this->createMock(MessageInterface::class);
92 1
        $envelope = Envelope::wrap($messageMock);
93 1
        $channelMock = $this->createMock(ChannelInterface::class);
94 1
        $messageBus = new MessageBus(new ChannelMap([self::CHANNEL_NAME => $channelMock]));
95
96 1
        $this->expectException(EnvelopeNotAcceptable::class);
97 1
        $this->expectExceptionMessage(
98 1
            "Channel key '".ChannelInterface::METADATA_KEY."' missing in metadata of ".
99 1
            "Envelope '{$envelope->getUuid()->toString()}' received on message bus."
100
        );
101 1
        $this->expectExceptionCode(EnvelopeNotAcceptable::CHANNEL_KEY_MISSING);
102
103 1
        $messageBus->receive($envelope);
104
    }
105
}
106