Skip to content

Commit 4cddd2c

Browse files
authored
Merge pull request #215 from jakubkulhan/0.6.x-add-mock-client-and-channel-objects
[0.6.x] Add mock client and channel objects
2 parents 63dd06d + 82aeafc commit 4cddd2c

2 files changed

Lines changed: 220 additions & 0 deletions

File tree

test/MockChannelInterface.php

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bunny\Test;
6+
7+
8+
use Bunny\ChannelInterface;
9+
use Bunny\ChannelMode;
10+
use Bunny\ClientInterface;
11+
use Bunny\Message;
12+
use Bunny\Protocol\MethodBasicCancelOkFrame;
13+
use Bunny\Protocol\MethodBasicConsumeOkFrame;
14+
use Bunny\Protocol\MethodBasicQosOkFrame;
15+
use Bunny\Protocol\MethodBasicRecoverOkFrame;
16+
use Bunny\Protocol\MethodConfirmSelectOkFrame;
17+
use Bunny\Protocol\MethodExchangeBindOkFrame;
18+
use Bunny\Protocol\MethodExchangeDeclareOkFrame;
19+
use Bunny\Protocol\MethodExchangeDeleteOkFrame;
20+
use Bunny\Protocol\MethodExchangeUnbindOkFrame;
21+
use Bunny\Protocol\MethodQueueBindOkFrame;
22+
use Bunny\Protocol\MethodQueueDeclareOkFrame;
23+
use Bunny\Protocol\MethodQueueDeleteOkFrame;
24+
use Bunny\Protocol\MethodQueuePurgeOkFrame;
25+
use Bunny\Protocol\MethodQueueUnbindOkFrame;
26+
use Bunny\Protocol\MethodTxCommitOkFrame;
27+
use Bunny\Protocol\MethodTxRollbackOkFrame;
28+
use Bunny\Protocol\MethodTxSelectOkFrame;
29+
use Evenement\EventEmitterTrait;
30+
31+
final class MockChannelInterface implements ChannelInterface
32+
{
33+
use EventEmitterTrait;
34+
35+
public function getMode(): ChannelMode
36+
{
37+
return ChannelMode::Regular;
38+
}
39+
40+
public function addReturnListener(callable $callback): ChannelInterface
41+
{
42+
return $this;
43+
}
44+
45+
public function removeReturnListener(callable $callback): ChannelInterface
46+
{
47+
return $this;
48+
}
49+
50+
public function addAckListener(callable $callback): ChannelInterface
51+
{
52+
return $this;
53+
}
54+
55+
public function removeAckListener(callable $callback): ChannelInterface
56+
{
57+
return $this;
58+
}
59+
60+
public function close(int $replyCode = 0, string $replyText = '', bool $connectionStatus = ClientInterface::RAW_CONNECTION_ACTIVE): void
61+
{
62+
}
63+
64+
public function consume(callable $callback, string $queue = '', string $consumerTag = '', bool $noLocal = false, bool $noAck = false, bool $exclusive = false, bool $nowait = false, array $arguments = [], int $concurrency = 1): MethodBasicConsumeOkFrame
65+
{
66+
return new MethodBasicConsumeOkFrame();
67+
}
68+
69+
public function ack(Message $message, bool $multiple = false): bool
70+
{
71+
return false;
72+
}
73+
74+
public function nack(Message $message, bool $multiple = false, bool $requeue = true): bool
75+
{
76+
return false;
77+
}
78+
79+
public function reject(Message $message, bool $requeue = true): bool
80+
{
81+
return false;
82+
}
83+
84+
public function get(string $queue = '', bool $noAck = false): Message|null
85+
{
86+
return null;
87+
}
88+
89+
public function publish(string $body, array $headers = [], string $exchange = '', string $routingKey = '', bool $mandatory = false, bool $immediate = false): int|bool
90+
{
91+
return false;
92+
}
93+
94+
public function cancel(string $consumerTag, bool $nowait = false): MethodBasicCancelOkFrame|bool
95+
{
96+
return false;
97+
}
98+
99+
public function txSelect(): MethodTxSelectOkFrame
100+
{
101+
return new MethodTxSelectOkFrame();
102+
}
103+
104+
public function txCommit(): MethodTxCommitOkFrame
105+
{
106+
return new MethodTxCommitOkFrame();
107+
}
108+
109+
public function txRollback(): MethodTxRollbackOkFrame
110+
{
111+
return new MethodTxRollbackOkFrame();
112+
}
113+
114+
public function confirmSelect(?callable $callback = null, bool $nowait = false): MethodConfirmSelectOkFrame|bool
115+
{
116+
return false;
117+
}
118+
119+
public function qos(int $prefetchSize = 0, int $prefetchCount = 0, bool $global = false): MethodBasicQosOkFrame
120+
{
121+
return new MethodBasicQosOkFrame();
122+
}
123+
124+
public function queueDeclare(string $queue = '', bool $passive = false, bool $durable = false, bool $exclusive = false, bool $autoDelete = false, bool $nowait = false, array $arguments = []): MethodQueueDeclareOkFrame|bool
125+
{
126+
return false;
127+
}
128+
129+
public function queueBind(string $exchange, string $queue = '', string $routingKey = '', bool $nowait = false, array $arguments = []): MethodQueueBindOkFrame|bool
130+
{
131+
return false;
132+
}
133+
134+
public function queuePurge(string $queue = '', bool $nowait = false): MethodQueuePurgeOkFrame|bool
135+
{
136+
return false;
137+
}
138+
139+
public function queueDelete(string $queue = '', bool $ifUnused = false, bool $ifEmpty = false, bool $nowait = false): MethodQueueDeleteOkFrame|bool
140+
{
141+
return false;
142+
}
143+
144+
public function queueUnbind(string $exchange, string $queue = '', string $routingKey = '', array $arguments = []): MethodQueueUnbindOkFrame
145+
{
146+
return new MethodQueueUnbindOkFrame();
147+
}
148+
149+
public function exchangeDeclare(string $exchange, string $exchangeType = 'direct', bool $passive = false, bool $durable = false, bool $autoDelete = false, bool $internal = false, bool $nowait = false, array $arguments = []): MethodExchangeDeclareOkFrame|bool
150+
{
151+
return false;
152+
}
153+
154+
public function exchangeDelete(string $exchange, bool $ifUnused = false, bool $nowait = false): MethodExchangeDeleteOkFrame|bool
155+
{
156+
return false;
157+
}
158+
159+
public function exchangeBind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): MethodExchangeBindOkFrame|bool
160+
{
161+
return false;
162+
}
163+
164+
public function exchangeUnbind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): MethodExchangeUnbindOkFrame|bool
165+
{
166+
return false;
167+
}
168+
169+
public function recoverAsync(bool $requeue = false): bool
170+
{
171+
return false;
172+
}
173+
174+
public function recover(bool $requeue = false): MethodBasicRecoverOkFrame
175+
{
176+
return new MethodBasicRecoverOkFrame();
177+
}
178+
179+
}

test/MockClientInterface.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bunny\Test;
6+
7+
8+
use Bunny\ChannelInterface;
9+
use Bunny\ClientInterface;
10+
11+
final class MockClientInterface implements ClientInterface
12+
{
13+
private int $disconnectCount = 0;
14+
private bool $isConnected;
15+
16+
public function __construct(bool $isConnected)
17+
{
18+
$this->isConnected = $isConnected;
19+
}
20+
21+
22+
public function channel(): ChannelInterface
23+
{
24+
return new MockChannelInterface();
25+
}
26+
27+
public function disconnect(int $replyCode = 0, string $replyText = '', bool $connectionStatus = ClientInterface::RAW_CONNECTION_ACTIVE): void
28+
{
29+
$this->disconnectCount++;
30+
}
31+
32+
public function isConnected(): bool
33+
{
34+
return $this->isConnected;
35+
}
36+
37+
public function getDisconnectCount(): int
38+
{
39+
return $this->disconnectCount;
40+
}
41+
}

0 commit comments

Comments
 (0)