-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathFsContextTest.php
223 lines (151 loc) · 6.9 KB
/
FsContextTest.php
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace Enqueue\Fs\Tests;
use Enqueue\Fs\FsConsumer;
use Enqueue\Fs\FsContext;
use Enqueue\Fs\FsDestination;
use Enqueue\Fs\FsMessage;
use Enqueue\Fs\FsProducer;
use Enqueue\Null\NullQueue;
use Enqueue\Test\ClassExtensionTrait;
use Enqueue\Test\ReadAttributeTrait;
use Interop\Queue\Context;
use Interop\Queue\Exception\InvalidDestinationException;
use Makasim\File\TempFile;
class FsContextTest extends \PHPUnit\Framework\TestCase
{
use ClassExtensionTrait;
use ReadAttributeTrait;
public function testShouldImplementContextInterface()
{
$this->assertClassImplements(Context::class, FsContext::class);
}
public function testShouldAllowCreateEmptyMessage()
{
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$message = $context->createMessage();
$this->assertInstanceOf(FsMessage::class, $message);
$this->assertSame('', $message->getBody());
$this->assertSame([], $message->getProperties());
$this->assertSame([], $message->getHeaders());
}
public function testShouldAllowCreateCustomMessage()
{
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$message = $context->createMessage('theBody', ['aProp' => 'aPropVal'], ['aHeader' => 'aHeaderVal']);
$this->assertInstanceOf(FsMessage::class, $message);
$this->assertSame('theBody', $message->getBody());
$this->assertSame(['aProp' => 'aPropVal'], $message->getProperties());
$this->assertSame(['aHeader' => 'aHeaderVal'], $message->getHeaders());
}
public function testShouldCreateQueue()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$queue = $context->createQueue($tmpFile->getFilename());
$this->assertInstanceOf(FsDestination::class, $queue);
$this->assertInstanceOf(\SplFileInfo::class, $queue->getFileInfo());
$this->assertSame((string) $tmpFile, (string) $queue->getFileInfo());
$this->assertSame($tmpFile->getFilename(), $queue->getTopicName());
}
public function testShouldAllowCreateTopic()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$topic = $context->createTopic($tmpFile->getFilename());
$this->assertInstanceOf(FsDestination::class, $topic);
$this->assertInstanceOf(\SplFileInfo::class, $topic->getFileInfo());
$this->assertSame((string) $tmpFile, (string) $topic->getFileInfo());
$this->assertSame($tmpFile->getFilename(), $topic->getTopicName());
}
public function testShouldAllowCreateTmpQueue()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$queue = $context->createTemporaryQueue();
$this->assertInstanceOf(FsDestination::class, $queue);
$this->assertInstanceOf(TempFile::class, $queue->getFileInfo());
$this->assertNotEmpty($queue->getQueueName());
}
public function testShouldCreateProducer()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$producer = $context->createProducer();
$this->assertInstanceOf(FsProducer::class, $producer);
}
public function testShouldThrowIfNotFsDestinationGivenOnCreateConsumer()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$this->expectException(InvalidDestinationException::class);
$this->expectExceptionMessage('The destination must be an instance of Enqueue\Fs\FsDestination but got Enqueue\Null\NullQueue.');
$consumer = $context->createConsumer(new NullQueue('aQueue'));
$this->assertInstanceOf(FsConsumer::class, $consumer);
}
public function testShouldCreateConsumer()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$queue = $context->createQueue($tmpFile->getFilename());
$this->assertSame(
100,
$context->createConsumer($queue)->getPollingInterval()
);
}
public function testShouldPropagatePreFetchCountToCreatedConsumer()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 123, 0666, 100);
$queue = $context->createQueue($tmpFile->getFilename());
$consumer = $context->createConsumer($queue);
// guard
$this->assertInstanceOf(FsConsumer::class, $consumer);
$this->assertAttributeSame(123, 'preFetchCount', $consumer);
}
public function testShouldAllowGetPreFetchCountSetInConstructor()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 123, 0666, 100);
$this->assertSame(123, $context->getPreFetchCount());
}
public function testShouldAllowGetPreviouslySetPreFetchCount()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$context->setPreFetchCount(456);
$this->assertSame(456, $context->getPreFetchCount());
}
public function testShouldAllowPurgeMessagesFromQueue()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
file_put_contents($tmpFile, 'foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$queue = $context->createQueue($tmpFile->getFilename());
$context->purgeQueue($queue);
$this->assertEmpty(file_get_contents($tmpFile));
}
public function testShouldCreateFileOnFilesystemIfNotExistOnDeclareDestination()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/'.uniqid());
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 100);
$queue = $context->createQueue($tmpFile->getFilename());
$this->assertFileDoesNotExist((string) $tmpFile);
$context->declareDestination($queue);
$this->assertFileExists((string) $tmpFile);
$this->assertTrue(is_readable($tmpFile));
$this->assertTrue(is_writable($tmpFile));
// do nothing if file already exists
$context->declareDestination($queue);
$this->assertFileExists((string) $tmpFile);
unlink($tmpFile);
}
public function testShouldCreateMessageConsumerAndSetPollingInterval()
{
$tmpFile = new TempFile(sys_get_temp_dir().'/foo');
$context = new FsContext(sys_get_temp_dir(), 1, 0666, 123456);
$queue = $context->createQueue($tmpFile->getFilename());
$consumer = $context->createConsumer($queue);
$this->assertInstanceOf(FsConsumer::class, $consumer);
$this->assertEquals(123456, $consumer->getPollingInterval());
}
}