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

ChannelTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 94
dl 0
loc 166
rs 10
c 0
b 0
f 0
ccs 105
cts 105
cp 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testPublish() 0 18 1
A testPublishAcceptedByGuard() 0 16 1
A testReceiveWithMissingChannel() 0 17 1
A testReceiveWithExistingSubscription() 0 21 1
A testGetKey() 0 4 1
A testReceive() 0 14 1
A testReceiveWithMissingSubscription() 0 19 1
A testPublishPreventedByGuard() 0 16 1
A testReceiveWithWrongChannel() 0 19 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\Channel;
10
11
use Daikon\MessageBus\Channel\Channel;
12
use Daikon\MessageBus\Channel\ChannelInterface;
13
use Daikon\MessageBus\Channel\Subscription\SubscriptionInterface;
14
use Daikon\MessageBus\Channel\Subscription\SubscriptionMap;
15
use Daikon\MessageBus\Envelope;
16
use Daikon\MessageBus\EnvelopeInterface;
17
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
18
use Daikon\MessageBus\Error\SubscriptionUnknown;
19
use Daikon\MessageBus\MessageBusInterface;
20
use Daikon\MessageBus\MessageInterface;
21
use Daikon\Metadata\Metadata;
22
use PHPUnit\Framework\TestCase;
23
24
final class ChannelTest extends TestCase
25
{
26
    const CHANNEL_NAME = 'test_channel';
27
28
    const SUB_NAME = 'test_subscription';
29
30 1
    public function testGetKey(): void
31
    {
32 1
        $channel = new Channel(self::CHANNEL_NAME, new SubscriptionMap);
33 1
        $this->assertEquals($channel->getKey(), self::CHANNEL_NAME);
34 1
    }
35
36 1
    public function testPublish(): void
37
    {
38
        /** @var MessageInterface $messageMock */
39 1
        $messageMock = $this->createMock(MessageInterface::class);
40 1
        $envelope = Envelope::wrap($messageMock);
41
        $envelopeExpectation = $this->callback(function (EnvelopeInterface $envelope) {
42 1
            return self::CHANNEL_NAME === $envelope->getMetadata()->get(ChannelInterface::METADATA_KEY);
43 1
        });
44
        /** @var MessageBusInterface $messageBusMock */
45 1
        $messageBusMock = $this->createMock(MessageBusInterface::class);
46 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
47 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn('mock');
48 1
        $subscriptionMock->expects($this->once())
49 1
            ->method('publish')
50 1
            ->with($envelopeExpectation, $this->equalTo($messageBusMock));
51
        /** @var SubscriptionInterface $subscriptionMock */
52 1
        $channel = new Channel(self::CHANNEL_NAME, new SubscriptionMap([$subscriptionMock]));
53 1
        $this->assertNull($channel->publish($envelope, $messageBusMock));
54 1
    }
55
56 1
    public function testPublishPreventedByGuard(): void
57
    {
58
        /** @var MessageInterface $messageMock */
59 1
        $messageMock = $this->createMock(MessageInterface::class);
60 1
        $envelope = Envelope::wrap($messageMock);
61
        /** @var MessageBusInterface $messageBusMock */
62 1
        $messageBusMock = $this->createMock(MessageBusInterface::class);
63 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
64 1
        $subscriptionMock->expects($this->never())->method('publish');
65 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn('mock');
66
        $guard = function (EnvelopeInterface $e): bool {
67 1
            return $e->getUuid()->toString() === 'this envelope is acceptable';
68 1
        };
69
        /** @var SubscriptionInterface $subscriptionMock */
70 1
        $channel = new Channel('foo', new SubscriptionMap([$subscriptionMock]), $guard);
71 1
        $this->assertNull($channel->publish($envelope, $messageBusMock));
72 1
    }
73
74 1
    public function testPublishAcceptedByGuard(): void
75
    {
76
        /** @var MessageInterface $messageMock */
77 1
        $messageMock = $this->createMock(MessageInterface::class);
78 1
        $envelope = Envelope::wrap($messageMock);
79
        /** @var MessageBusInterface $messageBusMock */
80 1
        $messageBusMock = $this->createMock(MessageBusInterface::class);
81 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
82 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn('mock');
83 1
        $subscriptionMock->expects($this->once())->method('publish');
84
        $guard = function (EnvelopeInterface $e): bool {
85 1
            return $e->getUuid()->toString() !== 'this envelope is inacceptable';
86 1
        };
87
        /** @var SubscriptionInterface $subscriptionMock */
88 1
        $channel = new Channel('foo', new SubscriptionMap([$subscriptionMock]), $guard);
89 1
        $this->assertNull($channel->publish($envelope, $messageBusMock));
90 1
    }
91
92 1
    public function testReceive(): void
93
    {
94
        /** @var MessageInterface $messageMock */
95 1
        $messageMock = $this->createMock(MessageInterface::class);
96 1
        $envelopeExpectation = Envelope::wrap($messageMock, Metadata::fromNative([
97 1
            ChannelInterface::METADATA_KEY => self::CHANNEL_NAME,
98 1
            SubscriptionInterface::METADATA_KEY => self::SUB_NAME
99
        ]));
100 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
101 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn(self::SUB_NAME);
102 1
        $subscriptionMock->expects($this->once())->method('receive')->with($envelopeExpectation);
103
        /** @var SubscriptionInterface $subscriptionMock */
104 1
        $channel = new Channel(self::CHANNEL_NAME, new SubscriptionMap([$subscriptionMock]));
105 1
        $this->assertNull($channel->receive($envelopeExpectation));
106 1
    }
107
108 1
    public function testReceiveWithExistingSubscription(): void
109
    {
110
        /** @var MessageInterface $messageMock */
111 1
        $messageMock = $this->createMock(MessageInterface::class);
112 1
        $envelope = Envelope::wrap($messageMock, Metadata::fromNative([
113 1
            ChannelInterface::METADATA_KEY => self::CHANNEL_NAME,
114 1
            SubscriptionInterface::METADATA_KEY => 'foobar'
115
        ]));
116 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
117 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn(self::SUB_NAME);
118
        /** @psalm-suppress InvalidArgument */
119 1
        $channel = new Channel(self::CHANNEL_NAME, new SubscriptionMap([$subscriptionMock]));
120
121 1
        $this->expectException(SubscriptionUnknown::class);
122 1
        $this->expectExceptionMessage(
123 1
            "Channel '".self::CHANNEL_NAME."' has no subscription 'foobar' and thus ".
124 1
            "Envelope '{$envelope->getUuid()->toString()}' cannot be handled."
125
        );
126 1
        $this->expectExceptionCode(0);
127
128 1
        $channel->receive($envelope);
129
    } // @codeCoverageIgnore
130
131 1
    public function testReceiveWithMissingChannel(): void
132
    {
133
        /** @var MessageInterface $messageMock */
134 1
        $messageMock = $this->createMock(MessageInterface::class);
135 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
136 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn('mock');
137 1
        $envelope = Envelope::wrap($messageMock);
138 1
        $channel = new Channel(self::CHANNEL_NAME, new SubscriptionMap([$subscriptionMock]));
139
140 1
        $this->expectException(EnvelopeNotAcceptable::class);
141 1
        $this->expectExceptionMessage(
142 1
            "Channel key '".ChannelInterface::METADATA_KEY."' missing in metadata of Envelope '".
143 1
            "{$envelope->getUuid()->toString()}' received on channel '".self::CHANNEL_NAME."'."
144
        );
145 1
        $this->expectExceptionCode(EnvelopeNotAcceptable::CHANNEL_KEY_MISSING);
146
147 1
        $channel->receive($envelope);
148
    } // @codeCoverageIgnore
149
150 1
    public function testReceiveWithWrongChannel(): void
151
    {
152
        /** @var MessageInterface $messageMock */
153 1
        $messageMock = $this->createMock(MessageInterface::class);
154 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
155 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn('mock');
156 1
        $envelope = Envelope::wrap($messageMock, Metadata::fromNative([
157 1
            ChannelInterface::METADATA_KEY => 'foobar'
158
        ]));
159 1
        $channel = new Channel(self::CHANNEL_NAME, new SubscriptionMap([$subscriptionMock]));
160
161 1
        $this->expectException(EnvelopeNotAcceptable::class);
162 1
        $this->expectExceptionMessage(
163 1
            "Channel '".self::CHANNEL_NAME."' inadvertently received ".
164 1
            "Envelope '{$envelope->getUuid()->toString()}' for channel 'foobar'."
165
        );
166 1
        $this->expectExceptionCode(EnvelopeNotAcceptable::CHANNEL_KEY_UNEXPECTED);
167
168 1
        $channel->receive($envelope);
169
    } // @codeCoverageIgnore
170
171 1
    public function testReceiveWithMissingSubscription(): void
172
    {
173
        /** @var MessageInterface $messageMock */
174 1
        $messageMock = $this->createMock(MessageInterface::class);
175 1
        $subscriptionMock = $this->createMock(SubscriptionInterface::class);
176 1
        $subscriptionMock->expects($this->once())->method('getKey')->willReturn('mock');
177 1
        $envelope = Envelope::wrap($messageMock, Metadata::fromNative([
178 1
            ChannelInterface::METADATA_KEY => self::CHANNEL_NAME
179
        ]));
180 1
        $channel = new Channel(self::CHANNEL_NAME, new SubscriptionMap([$subscriptionMock]));
181
182 1
        $this->expectException(EnvelopeNotAcceptable::class);
183 1
        $this->expectExceptionMessage(
184 1
            "Subscription key '".SubscriptionInterface::METADATA_KEY."' missing in metadata of ".
185 1
            "Envelope '{$envelope->getUuid()->toString()}' received on channel '".self::CHANNEL_NAME."'."
186
        );
187 1
        $this->expectExceptionCode(EnvelopeNotAcceptable::SUBSCRIPTION_KEY_MISSING);
188
189 1
        $channel->receive($envelope);
190
    } // @codeCoverageIgnore
191
}
192