|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tavii\SQSJobQueue\Queue; |
|
3
|
|
|
|
|
4
|
|
|
use Phake; |
|
5
|
|
|
use Aws\Sqs\SqsClient; |
|
6
|
|
|
use Tavii\SQSJobQueue\Job\Job; |
|
7
|
|
|
|
|
8
|
|
|
class QueueTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
private $client; |
|
11
|
|
|
|
|
12
|
|
|
public function setUp() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->client = Phake::mock('Aws\Sqs\SqsClient'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @test |
|
19
|
|
|
*/ |
|
20
|
|
|
public function キューに保存に保存することができる() |
|
21
|
|
|
{ |
|
22
|
|
|
$name = 'test'; |
|
23
|
|
|
$job = Phake::mock('Tavii\SQSJobQueue\Job\Job'); |
|
24
|
|
|
Phake::when($job)->getName() |
|
25
|
|
|
->thenReturn($name); |
|
26
|
|
|
Phake::when($job)->getArgs() |
|
27
|
|
|
->thenReturn(array('test','teset2')); |
|
28
|
|
|
|
|
29
|
|
|
Phake::when($this->client)->getQueueUrl(array('QueueName' => $name)) |
|
30
|
|
|
->thenReturn(array( |
|
31
|
|
|
'QueueUrl' => 'test' |
|
32
|
|
|
)); |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$queue = new Queue($this->client); |
|
36
|
|
|
$queue->send($job); |
|
37
|
|
|
|
|
38
|
|
|
Phake::verify($this->client)->getQueueUrl(array('QueueName' => $name)); |
|
39
|
|
|
Phake::verify($this->client)->sendMessage($this->isType('array')); |
|
40
|
|
|
Phake::verify($job)->getName(); |
|
41
|
|
|
Phake::verify($job)->getArgs(); |
|
42
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @test |
|
47
|
|
|
*/ |
|
48
|
|
|
public function キューからジョブを取り出すことができる() |
|
49
|
|
|
{ |
|
50
|
|
|
$name = "test_job"; |
|
51
|
|
|
$args = array( |
|
52
|
|
|
'Body' => json_encode(array( |
|
53
|
|
|
'className' => 'Tavii\SQSJobQueue\Queue\DummyJob', |
|
54
|
|
|
'args' => array('a' => '1' , 'b' => 2) |
|
55
|
|
|
)) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$collection = Phake::mock('Guzzle\Common\Collection'); |
|
59
|
|
|
Phake::when($collection)->getPath('Messages/*') |
|
60
|
|
|
->thenReturn($args); |
|
61
|
|
|
|
|
62
|
|
|
Phake::when($this->client)->getQueueUrl(array('QueueName' => $name)) |
|
63
|
|
|
->thenReturn(array( |
|
64
|
|
|
'QueueUrl' => 'test' |
|
65
|
|
|
)); |
|
66
|
|
|
|
|
67
|
|
|
Phake::when($this->client)->receiveMessage(array( |
|
68
|
|
|
'QueueUrl' => 'test' |
|
69
|
|
|
))->thenReturn($collection); |
|
70
|
|
|
|
|
71
|
|
|
$queue = new Queue($this->client); |
|
72
|
|
|
$job = $queue->receive($name); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertInstanceOf('Tavii\SQSJobQueue\Message\MessageInterface', $job); |
|
75
|
|
|
Phake::verify($collection)->getPath('Messages/*'); |
|
76
|
|
|
Phake::verify($this->client)->getQueueUrl(array('QueueName' => $name)); |
|
77
|
|
|
Phake::verify($this->client)->receiveMessage(array('QueueUrl' => 'test')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @test |
|
82
|
|
|
*/ |
|
83
|
|
|
public function キューを削除する() |
|
84
|
|
|
{ |
|
85
|
|
|
$message = Phake::mock('Tavii\SQSJobQueue\Message\Message'); |
|
86
|
|
|
Phake::when($message)->getMessage() |
|
87
|
|
|
->thenReturn(array( |
|
88
|
|
|
'QueueUrl' => '/hoge/fuga', |
|
89
|
|
|
'ReceiptHandle' => 'Receipt' |
|
90
|
|
|
)); |
|
91
|
|
|
|
|
92
|
|
|
Phake::when($this->client)->deleteMessage(array( |
|
93
|
|
|
'QueueUrl' => '/hoge/fuga', |
|
94
|
|
|
'ReceiptHandle' => 'Receipt', |
|
95
|
|
|
)); |
|
96
|
|
|
|
|
97
|
|
|
$queue = new Queue($this->client); |
|
98
|
|
|
$queue->delete($message); |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
class DummyJob extends Job |
|
104
|
|
|
{ |
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $args |
|
107
|
|
|
* @return booelan |
|
|
|
|
|
|
108
|
|
|
*/ |
|
109
|
|
|
protected function run() |
|
110
|
|
|
{ |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getPrefix() |
|
115
|
|
|
{ |
|
116
|
|
|
return 'queue_test'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getName() |
|
124
|
|
|
{ |
|
125
|
|
|
return 'test_job'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.