Repository navigation
Expand file tree
/
Copy pathBufferedStompClientTest.php
More file actions
188 lines (142 loc) · 5.61 KB
/
Copy pathBufferedStompClientTest.php
File metadata and controls
188 lines (142 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Enqueue\Stomp\Tests;
use Enqueue\Stomp\BufferedStompClient;
use Enqueue\Test\ClassExtensionTrait;
use Enqueue\Test\WriteAttributeTrait;
use Stomp\Client;
use Stomp\Network\Connection;
use Stomp\Transport\Frame;
class BufferedStompClientTest extends \PHPUnit\Framework\TestCase
{
use ClassExtensionTrait;
use WriteAttributeTrait;
public function testShouldExtendLibStompClient()
{
$this->assertClassExtends(Client::class, BufferedStompClient::class);
}
public function testCouldBeConstructedWithRequiredArguments()
{
new BufferedStompClient('tcp://localhost:12345');
}
public function testCouldGetBufferSizeValue()
{
$client = new BufferedStompClient('tcp://localhost:12345', 12345);
$this->assertSame(12345, $client->getBufferSize());
}
public function testShouldCleanBufferOnDisconnect()
{
$client = new BufferedStompClient('tcp://localhost:12345', 12345);
$this->assertObjectHasAttribute('buffer', $client);
$this->assertObjectHasAttribute('currentBufferSize', $client);
$this->writeAttribute($client, 'buffer', [1, 2, 3]);
$this->writeAttribute($client, 'currentBufferSize', 12345);
$client->disconnect();
$this->assertSame([], $this->readAttribute($client, 'buffer'));
$this->assertSame(0, $this->readAttribute($client, 'currentBufferSize'));
}
public function testShouldReturnFrameFromTheBufferIfExists()
{
$frame = new Frame();
$client = new BufferedStompClient('tcp://localhost:12345');
$this->writeAttribute($client, 'buffer', ['subscription-id' => [$frame]]);
$this->writeAttribute($client, 'currentBufferSize', 100);
$resultFrame = $client->readMessageFrame('subscription-id', 0);
$this->assertSame($resultFrame, $frame);
$this->assertEquals(99, $this->readAttribute($client, 'currentBufferSize'));
}
public function testShouldThrowExceptionIfFrameIsNotMessageFrame()
{
$frame = new Frame('NOT-MESSAGE-FRAME');
$connection = $this->createStompConnectionMock();
$connection
->expects($this->once())
->method('readFrame')
->willReturn($frame)
;
$client = new BufferedStompClient($connection);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unexpected frame was received: "NOT-MESSAGE-FRAME"');
$client->readMessageFrame('subscription-id', 0);
}
public function testShouldThrowExceptionIfFrameHasNoSubscriptionHeader()
{
$frame = new Frame('MESSAGE');
$connection = $this->createStompConnectionMock();
$connection
->expects($this->once())
->method('readFrame')
->willReturn($frame)
;
$client = new BufferedStompClient($connection);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Got message frame with missing subscription header');
$client->readMessageFrame('subscription-id', 0);
}
public function testShouldReturnReceivedFrameIfSubscriptionIdIsEqual()
{
$frame = new Frame('MESSAGE');
$frame['subscription'] = 'subscription-id';
$connection = $this->createStompConnectionMock();
$connection
->expects($this->once())
->method('readFrame')
->willReturn($frame)
;
$client = new BufferedStompClient($connection);
$resultFrame = $client->readMessageFrame('subscription-id', 0);
$this->assertSame($resultFrame, $frame);
}
public function testShouldAddFrameToBufferIfSubscriptionIdIsNotEqual()
{
$frame = new Frame('MESSAGE');
$frame['subscription'] = 'subscription-id-2';
$connection = $this->createStompConnectionMock();
$connection
->expects($this->at(1))
->method('readFrame')
->willReturn($frame)
;
$connection
->expects($this->at(2))
->method('readFrame')
->willReturn(false)
;
$client = new BufferedStompClient($connection);
$returnValue = $client->readMessageFrame('subscription-id-1', 0);
$buffer = $this->readAttribute($client, 'buffer');
$bufferedFrame = $buffer['subscription-id-2'][0];
$this->assertSame($frame, $bufferedFrame);
$this->assertNull($returnValue);
}
public function testShouldAddFirstFrameToBufferIfSubscriptionNotEqualAndReturnSecondFrameIfSubscriptionIsEqual()
{
$frame1 = new Frame('MESSAGE');
$frame1['subscription'] = 'subscription-id-1';
$frame2 = new Frame('MESSAGE');
$frame2['subscription'] = 'subscription-id-2';
$connection = $this->createStompConnectionMock();
$connection
->expects($this->at(1))
->method('readFrame')
->willReturn($frame1)
;
$connection
->expects($this->at(3))
->method('readFrame')
->willReturn($frame2)
;
$client = new BufferedStompClient($connection);
$returnFrame = $client->readMessageFrame('subscription-id-2', 1);
$buffer = $this->readAttribute($client, 'buffer');
$bufferedFrame = $buffer['subscription-id-1'][0];
$this->assertSame($frame1, $bufferedFrame);
$this->assertSame($frame2, $returnFrame);
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|Connection
*/
private function createStompConnectionMock()
{
return $this->createMock(Connection::class);
}
}