1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dekalee\RedisSwarrot\Tests\Unit\MessageProvider; |
4
|
|
|
|
5
|
|
|
use Dekalee\RedisSwarrot\MessageProvider\RedisMessageProvider; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class RedisMessageProviderTest |
9
|
|
|
*/ |
10
|
|
|
class RedisMessageProviderTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var RedisMessageProvider |
14
|
|
|
*/ |
15
|
|
|
private $provider; |
16
|
|
|
|
17
|
|
|
private $redis; |
18
|
|
|
private $queueName; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Set up the test. |
22
|
|
|
*/ |
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->redis = $this->prophesize('\Redis'); |
26
|
|
|
$this->queueName = 'foo'; |
27
|
|
|
|
28
|
|
|
$this->provider = new RedisMessageProvider($this->redis->reveal(), $this->queueName); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Test instance |
33
|
|
|
*/ |
34
|
|
|
public function testInstance() |
35
|
|
|
{ |
36
|
|
|
$this->assertInstanceOf('Swarrot\Broker\MessageProvider\MessageProviderInterface', $this->provider); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Test get method |
41
|
|
|
*/ |
42
|
|
|
public function testGet() |
43
|
|
|
{ |
44
|
|
|
$data = json_encode(['bar' => 'baz']); |
45
|
|
|
$this->redis->lLen('foo')->willReturn(1); |
46
|
|
|
$this->redis->brPop('foo', 0) |
47
|
|
|
->shouldBeCalled() |
48
|
|
|
->willReturn( |
49
|
|
|
[ |
50
|
|
|
'foo', |
51
|
|
|
$data, |
52
|
|
|
] |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$message = $this->provider->get(); |
56
|
|
|
|
57
|
|
|
$this->assertInstanceOf('Swarrot\Broker\Message', $message); |
58
|
|
|
$this->assertSame($data, $message->getBody()); |
59
|
|
|
$this->assertSame(['channel' => 'foo'], $message->getProperties()); |
60
|
|
|
$this->assertNull($message->getId()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test nack with requeue |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function testNack() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$data = json_encode(['bar' => 'baz']); |
69
|
|
|
$message = $this->prophesize('Swarrot\Broker\Message'); |
70
|
|
|
$message->getBody()->willReturn($data); |
71
|
|
|
|
72
|
|
|
$this->redis->lPush($this->queueName, $data)->shouldBeCalled(); |
73
|
|
|
|
74
|
|
|
$this->provider->nack($message->reveal(), true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test nack with requeue |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function testNackWithNoRequeue() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$data = json_encode(['bar' => 'baz']); |
83
|
|
|
$message = $this->prophesize('Swarrot\Broker\Message'); |
84
|
|
|
$message->getBody()->willReturn($data); |
85
|
|
|
|
86
|
|
|
$this->redis->lPush($this->queueName, $data)->shouldNotBeCalled(); |
87
|
|
|
|
88
|
|
|
$this->provider->nack($message->reveal(), false); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.