1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of graze/queue. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://github.com/graze/queue/blob/master/LICENSE MIT |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/graze/queue |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Graze\Queue\Handler; |
17
|
|
|
|
18
|
|
|
use ArrayIterator; |
19
|
|
|
use Closure; |
20
|
|
|
use Graze\Queue\Adapter\AdapterInterface; |
21
|
|
|
use Graze\Queue\Message\MessageInterface; |
22
|
|
|
use Mockery as m; |
23
|
|
|
use Mockery\MockInterface; |
24
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
25
|
|
|
use RuntimeException; |
26
|
|
|
|
27
|
|
|
class BatchAcknowledgementHandlerTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** @var AdapterInterface|MockInterface */ |
30
|
|
|
private $adapter; |
31
|
|
|
/** @var MessageInterface|MockInterface */ |
32
|
|
|
private $messageA; |
33
|
|
|
/** @var MessageInterface|MockInterface */ |
34
|
|
|
private $messageB; |
35
|
|
|
/** @var MessageInterface|MockInterface */ |
36
|
|
|
private $messageC; |
37
|
|
|
/** @var ArrayIterator */ |
38
|
|
|
private $messages; |
39
|
|
|
/** @var BatchAcknowledgementHandler */ |
40
|
|
|
private $handler; |
41
|
|
|
|
42
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->adapter = m::mock(AdapterInterface::class); |
45
|
|
|
|
46
|
|
|
$this->messageA = $a = m::mock(MessageInterface::class); |
|
|
|
|
47
|
|
|
$this->messageB = $b = m::mock(MessageInterface::class); |
|
|
|
|
48
|
|
|
$this->messageC = $c = m::mock(MessageInterface::class); |
|
|
|
|
49
|
|
|
$this->messages = new ArrayIterator([$a, $b, $c]); |
50
|
|
|
|
51
|
|
|
$this->handler = new BatchAcknowledgementHandler(3); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testHandle() |
55
|
|
|
{ |
56
|
|
|
$handler = $this->handler; |
57
|
|
|
|
58
|
|
|
$this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true); |
|
|
|
|
59
|
|
|
$this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true); |
60
|
|
|
$this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true); |
61
|
|
|
$this->adapter->shouldReceive('acknowledge')->once()->with(iterator_to_array($this->messages)); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$msgs = []; |
64
|
|
|
$handler($this->messages, $this->adapter, function ($msg, Closure $done) use (&$msgs) { |
|
|
|
|
65
|
|
|
$msgs[] = $msg; |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
assertThat($msgs, is(identicalTo(iterator_to_array($this->messages)))); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testHandleInvalidMessage() |
72
|
|
|
{ |
73
|
|
|
$handler = $this->handler; |
74
|
|
|
|
75
|
|
|
$this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true); |
|
|
|
|
76
|
|
|
$this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(false); |
77
|
|
|
$this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true); |
78
|
|
|
$this->adapter->shouldReceive('acknowledge')->once()->with([$this->messageA, $this->messageC]); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$msgs = []; |
81
|
|
|
$handler($this->messages, $this->adapter, function ($msg) use (&$msgs) { |
82
|
|
|
$msgs[] = $msg; |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
assertThat($msgs, is(identicalTo([$this->messageA, $this->messageC]))); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testHandleWorkerWithThrownException() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$handler = $this->handler; |
91
|
|
|
|
92
|
|
|
$this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true); |
|
|
|
|
93
|
|
|
$this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true); |
94
|
|
|
|
95
|
|
|
$this->adapter->shouldReceive('acknowledge')->once()->with([$this->messageA]); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->setExpectedException('RuntimeException', 'foo'); |
98
|
|
|
|
99
|
|
|
$handler($this->messages, $this->adapter, function ($msg) { |
100
|
|
|
if ($msg === $this->messageB) { |
101
|
|
|
throw new RuntimeException('foo'); |
102
|
|
|
} |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.