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\Subscription; |
||
10 | |||
11 | use Daikon\MessageBus\Channel\ChannelInterface; |
||
12 | use Daikon\MessageBus\Channel\Subscription\MessageHandler\MessageHandlerInterface; |
||
13 | use Daikon\MessageBus\Channel\Subscription\MessageHandler\MessageHandlerList; |
||
14 | use Daikon\MessageBus\Channel\Subscription\Subscription; |
||
15 | use Daikon\MessageBus\Channel\Subscription\SubscriptionInterface; |
||
16 | use Daikon\MessageBus\Channel\Subscription\Transport\TransportInterface; |
||
17 | use Daikon\MessageBus\Envelope; |
||
18 | use Daikon\MessageBus\EnvelopeInterface; |
||
19 | use Daikon\MessageBus\Exception\EnvelopeNotAcceptable; |
||
20 | use Daikon\MessageBus\MessageBusInterface; |
||
21 | use Daikon\MessageBus\MessageInterface; |
||
22 | use Daikon\Metadata\Metadata; |
||
23 | use PHPUnit\Framework\TestCase; |
||
24 | |||
25 | final class SubscriptionTest extends TestCase |
||
26 | { |
||
27 | const CHANNEL_NAME = 'test_channel'; |
||
28 | |||
29 | const SUB_NAME = 'test_subscription'; |
||
30 | |||
31 | 1 | public function testGetKey(): void |
|
32 | { |
||
33 | /** @var TransportInterface $transportMock */ |
||
34 | 1 | $transportMock = $this->createMock(TransportInterface::class); |
|
35 | 1 | $subscription = new Subscription(self::SUB_NAME, $transportMock, new MessageHandlerList); |
|
36 | 1 | $this->assertEquals($subscription->getKey(), self::SUB_NAME); |
|
37 | 1 | } |
|
38 | |||
39 | 1 | public function testPublish(): void |
|
40 | { |
||
41 | /** @var MessageInterface $messageMock */ |
||
42 | 1 | $messageMock = $this->createMock(MessageInterface::class); |
|
43 | 1 | $envelope = Envelope::wrap($messageMock); |
|
44 | 1 | $envelopeExpectation = $this->callback(function (EnvelopeInterface $envelope) { |
|
45 | 1 | return self::SUB_NAME === $envelope->getMetadata()->get(SubscriptionInterface::METADATA_KEY); |
|
46 | 1 | }); |
|
47 | /** @var MessageBusInterface $messageBusMock */ |
||
48 | 1 | $messageBusMock = $this->createMock(MessageBusInterface::class); |
|
49 | 1 | $transportMock = $this->createMock(TransportInterface::class); |
|
50 | 1 | $transportMock->expects($this->once()) |
|
51 | 1 | ->method('send') |
|
52 | 1 | ->with($envelopeExpectation, $this->equalTo($messageBusMock)); |
|
53 | /** @var TransportInterface $transportMock */ |
||
54 | 1 | $subscription = new Subscription(self::SUB_NAME, $transportMock, new MessageHandlerList); |
|
55 | 1 | $this->assertNull($subscription->publish($envelope, $messageBusMock)); |
|
0 ignored issues
–
show
|
|||
56 | 1 | } |
|
57 | |||
58 | 1 | public function testReceive(): void |
|
59 | { |
||
60 | /** @var MessageInterface $messageMock */ |
||
61 | 1 | $messageMock = $this->createMock(MessageInterface::class); |
|
62 | 1 | $envelopeExpectation = Envelope::wrap($messageMock, Metadata::fromNative([ |
|
63 | 1 | ChannelInterface::METADATA_KEY => self::CHANNEL_NAME, |
|
64 | 1 | SubscriptionInterface::METADATA_KEY => self::SUB_NAME |
|
65 | ])); |
||
66 | /** @var TransportInterface $transportMock */ |
||
67 | 1 | $transportMock = $this->createMock(TransportInterface::class); |
|
68 | 1 | $messageHandlerMock = $this->createMock(MessageHandlerInterface::class); |
|
69 | 1 | $messageHandlerMock->expects($this->once())->method('handle')->with($envelopeExpectation); |
|
70 | /** @var MessageHandlerInterface $messageHandlerMock */ |
||
71 | 1 | $mockedHandlers = new MessageHandlerList([$messageHandlerMock]); |
|
72 | 1 | $subscription = new Subscription(self::SUB_NAME, $transportMock, $mockedHandlers); |
|
73 | 1 | $this->assertNull($subscription->receive($envelopeExpectation)); |
|
0 ignored issues
–
show
Are you sure the usage of
$subscription->receive($envelopeExpectation) targeting Daikon\MessageBus\Channe...Subscription::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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
74 | 1 | } |
|
75 | |||
76 | 1 | public function testReceiveWithWrongSubscription(): void |
|
77 | { |
||
78 | /** @var MessageInterface $messageMock */ |
||
79 | 1 | $messageMock = $this->createMock(MessageInterface::class); |
|
80 | 1 | $envelope = Envelope::wrap($messageMock, Metadata::fromNative([ |
|
81 | 1 | ChannelInterface::METADATA_KEY => self::CHANNEL_NAME, |
|
82 | 1 | SubscriptionInterface::METADATA_KEY => 'foobar' |
|
83 | ])); |
||
84 | /** @var TransportInterface $transportMock */ |
||
85 | 1 | $transportMock = $this->createMock(TransportInterface::class); |
|
86 | 1 | $subscription = new Subscription(self::SUB_NAME, $transportMock, new MessageHandlerList); |
|
87 | |||
88 | 1 | $this->expectException(EnvelopeNotAcceptable::class); |
|
89 | 1 | $this->expectExceptionMessage( |
|
90 | 1 | "Subscription '".self::SUB_NAME."' inadvertently received Envelope ". |
|
91 | 1 | "'{$envelope->getUuid()->toString()}' for subscription 'foobar'." |
|
92 | ); |
||
93 | 1 | $this->expectExceptionCode(EnvelopeNotAcceptable::SUBSCRIPTION_KEY_UNEXPECTED); |
|
94 | |||
95 | 1 | $subscription->receive($envelope); |
|
96 | } |
||
97 | |||
98 | 1 | public function testReceiveWithMissingSubscription(): void |
|
99 | { |
||
100 | /** @var MessageInterface $messageMock */ |
||
101 | 1 | $messageMock = $this->createMock(MessageInterface::class); |
|
102 | 1 | $envelope = Envelope::wrap($messageMock); |
|
103 | /** @var TransportInterface $transportMock */ |
||
104 | 1 | $transportMock = $this->createMock(TransportInterface::class); |
|
105 | 1 | $subscription = new Subscription(self::SUB_NAME, $transportMock, new MessageHandlerList); |
|
106 | |||
107 | 1 | $this->expectException(EnvelopeNotAcceptable::class); |
|
108 | 1 | $this->expectExceptionMessage( |
|
109 | 1 | "Subscription key '".SubscriptionInterface::METADATA_KEY."' missing in metadata of ". |
|
110 | 1 | "Envelope '{$envelope->getUuid()->toString()}' received by subscription '".self::SUB_NAME."'." |
|
111 | ); |
||
112 | 1 | $this->expectExceptionCode(EnvelopeNotAcceptable::SUBSCRIPTION_KEY_MISSING); |
|
113 | |||
114 | 1 | $subscription->receive($envelope); |
|
115 | } |
||
116 | |||
117 | 1 | public function testPublishPreventedByGuard(): void |
|
118 | { |
||
119 | /** @var MessageInterface $messageMock */ |
||
120 | 1 | $messageMock = $this->createMock(MessageInterface::class); |
|
121 | 1 | $envelope = Envelope::wrap($messageMock); |
|
122 | /** @var MessageBusInterface $messageBusMock */ |
||
123 | 1 | $messageBusMock = $this->createMock(MessageBusInterface::class); |
|
124 | 1 | $transportMock = $this->getMockBuilder(TransportInterface::class)->getMock(); |
|
125 | 1 | $transportMock->expects($this->never())->method('send'); |
|
126 | 1 | $guardNone = function (EnvelopeInterface $e): bool { |
|
127 | 1 | return $e->getUuid()->toString() === 'this envelope is acceptable'; |
|
128 | 1 | }; |
|
129 | /** @var TransportInterface $transportMock */ |
||
130 | 1 | $subscription = new Subscription(self::SUB_NAME, $transportMock, new MessageHandlerList, $guardNone); |
|
131 | 1 | $this->assertNull($subscription->publish($envelope, $messageBusMock)); |
|
0 ignored issues
–
show
Are you sure the usage of
$subscription->publish($...elope, $messageBusMock) targeting Daikon\MessageBus\Channe...Subscription::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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
132 | 1 | } |
|
133 | |||
134 | 1 | public function testPublishAcceptedByGuard(): void |
|
135 | { |
||
136 | /** @var MessageBusInterface $messageBusMock */ |
||
137 | 1 | $messageBusMock = $this->createMock(MessageBusInterface::class); |
|
138 | 1 | $transportMock = $this->getMockBuilder(TransportInterface::class)->getMock(); |
|
139 | 1 | $transportMock->expects($this->once())->method('send'); |
|
140 | 1 | $guardAll = function (EnvelopeInterface $e): bool { |
|
141 | 1 | return $e->getUuid()->toString() !== 'this envelope is acceptable'; |
|
142 | 1 | }; |
|
143 | /** @var TransportInterface $transportMock */ |
||
144 | 1 | $subscription = new Subscription(self::SUB_NAME, $transportMock, new MessageHandlerList, $guardAll); |
|
145 | /** @var MessageInterface $messageMock */ |
||
146 | 1 | $messageMock = $this->createMock(MessageInterface::class); |
|
147 | 1 | $envelope = Envelope::wrap($messageMock); |
|
148 | 1 | $this->assertNull($subscription->publish($envelope, $messageBusMock)); |
|
0 ignored issues
–
show
Are you sure the usage of
$subscription->publish($...elope, $messageBusMock) targeting Daikon\MessageBus\Channe...Subscription::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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
149 | 1 | } |
|
150 | } |
||
151 |
This check looks for function or method calls that always return null and whose return value is used.
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.