Repository navigation
Expand file tree
/
Copy pathFsConsumerTest.php
More file actions
143 lines (109 loc) · 4.14 KB
/
Copy pathFsConsumerTest.php
File metadata and controls
143 lines (109 loc) · 4.14 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
<?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\Test\ClassExtensionTrait;
use Interop\Queue\PsrConsumer;
use Makasim\File\TempFile;
class FsConsumerTest extends \PHPUnit\Framework\TestCase
{
use ClassExtensionTrait;
public function testShouldImplementConsumerInterface()
{
$this->assertClassImplements(PsrConsumer::class, FsConsumer::class);
}
public function testCouldBeConstructedWithContextAndDestinationAndPreFetchCountAsArguments()
{
new FsConsumer($this->createContextMock(), new FsDestination(TempFile::generate()), 1);
}
public function testShouldReturnDestinationSetInConstructorOnGetQueue()
{
$destination = new FsDestination(TempFile::generate());
$consumer = new FsConsumer($this->createContextMock(), $destination, 1);
$this->assertSame($destination, $consumer->getQueue());
}
public function testShouldAllowGetPreFetchCountSetInConstructor()
{
$consumer = new FsConsumer($this->createContextMock(), new FsDestination(TempFile::generate()), 123);
$this->assertSame(123, $consumer->getPreFetchCount());
}
public function testShouldAllowGetPreviouslySetPreFetchCount()
{
$consumer = new FsConsumer($this->createContextMock(), new FsDestination(TempFile::generate()), 123);
$consumer->setPreFetchCount(456);
$this->assertSame(456, $consumer->getPreFetchCount());
}
public function testShouldDoNothingOnAcknowledge()
{
$consumer = new FsConsumer($this->createContextMock(), new FsDestination(TempFile::generate()), 123);
$consumer->acknowledge(new FsMessage());
}
public function testShouldDoNothingOnReject()
{
$consumer = new FsConsumer($this->createContextMock(), new FsDestination(TempFile::generate()), 123);
$consumer->reject(new FsMessage());
}
public function testShouldSendSameMessageToDestinationOnReQueue()
{
$message = new FsMessage();
$destination = new FsDestination(TempFile::generate());
$producerMock = $this->createProducerMock();
$producerMock
->expects($this->once())
->method('send')
->with($this->identicalTo($destination), $this->identicalTo($message))
;
$contextMock = $this->createContextMock();
$contextMock
->expects($this->once())
->method('createProducer')
->willReturn($producerMock)
;
$consumer = new FsConsumer($contextMock, $destination, 123);
$consumer->reject($message, true);
}
public function testShouldCallContextWorkWithFileAndCallbackToItOnReceiveNoWait()
{
$destination = new FsDestination(TempFile::generate());
$contextMock = $this->createContextMock();
$contextMock
->expects($this->once())
->method('workWithFile')
->with($this->identicalTo($destination), 'c+', $this->isInstanceOf(\Closure::class))
;
$consumer = new FsConsumer($contextMock, $destination, 1);
$consumer->receiveNoWait();
}
public function testShouldWaitTwoSecondsForMessageAndExitOnReceive()
{
$destination = new FsDestination(TempFile::generate());
$contextMock = $this->createContextMock();
$contextMock
->expects($this->atLeastOnce())
->method('workWithFile')
;
$consumer = new FsConsumer($contextMock, $destination, 1);
$start = microtime(true);
$consumer->receive(2000);
$end = microtime(true);
$this->assertGreaterThan(1.5, $end - $start);
$this->assertLessThan(3.5, $end - $start);
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|FsProducer
*/
private function createProducerMock()
{
return $this->createMock(FsProducer::class);
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|FsContext
*/
private function createContextMock()
{
return $this->createMock(FsContext::class);
}
}