WorkerTest::ワーカーを停止させる()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
namespace Tavii\SQSJobQueue\Worker;
3
4
use Phake;
5
use Tavii\SQSJobQueue\Queue\QueueName;
6
use Tavii\SQSJobQueue\Storage\EntityInterface;
7
use Tavii\SQSJobQueue\Storage\EntityJobNameTrait;
8
use Tavii\SQSJobQueue\Storage\StorageInterface;
9
10
class WorkerTest extends \PHPUnit_Framework_TestCase
11
{
12
13
    /**
14
     * @test
15
     */
16
    public function ワーカーを実行する()
17
    {
18
        $name = new QueueName('test');
19
20
        $queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue');
21
        $storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage');
22
        $message = Phake::mock('Tavii\SQSJobQueue\Message\Message');
23
        $job = Phake::mock('Tavii\SQSJobQueue\Job\Job');
24
25
        Phake::when($queue)->receive($name)
26
            ->thenReturn($message);
27
28
        Phake::when($message)->getJob()
29
            ->thenReturn($job);
30
31
        Phake::when($job)->execute()
32
            ->thenReturn(true);
33
34
        $worker = new Worker($queue, $storage);
35
        $actual = $worker->run($name);
36
37
        Phake::verify($queue)->receive ($name);
38
        Phake::verify($message)->getJob();
39
        Phake::verify($job)->execute();
40
        Phake::verify($queue)->delete($message);
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function ワーカーを実行させる()
47
    {
48
        $name = new QueueName("test");
49
        $queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue');
50
        $storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage');
51
52
        $worker = new Worker($queue, $storage);
53
        $worker->start($name);
54
55
        Phake::verify($storage)->set($name, "test.com", 234);
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function ワーカーを停止させる()
62
    {
63
64
65
        $name = new QueueName("test");
66
        $entity = new TestEntity($name, 'test.com', 1234);
67
68
        $queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue');
69
        $storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage');
70
        Phake::when($storage)->find($name, 'test.com', null)
71
            ->thenReturn(array(
72
                $entity
73
            ));
74
75
        $worker = new Worker($queue, $storage);
76
        $worker->stop($name);
77
78
        Phake::verify($storage)->find($name, "test.com", null);
79
        Phake::verify($storage)->remove($this->isInstanceOf(QueueName::class), "test.com", 1234);
80
81
    }
82
83
    /**
84
     */
85
    public function ワーカーを強制的に停止させる()
86
    {
87
        $name = "test";
88
        $entity = new TestEntity($name, 'test.com', 1234);
89
90
        $queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue');
91
        $storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage');
92
        Phake::when($storage)->find($name, 'test.com', null)
93
            ->thenReturn(array(
94
                $entity
95
            ));
96
97
        $worker = new Worker($queue, $storage);
98
        $worker->stop($name, null, true);
99
100
        Phake::verify($storage)->find($name, "test.com", null);
101
        Phake::verify($storage)->remove($name, "test.com", 1234);
102
        Phake::verify($storage)->removeForce($name, "test.com");
103
    }
104
105
}
106
107
108
function pcntl_fork() {
109
    return 234;
110
}
111
112
function gethostname() {
113
    return 'test.com';
114
}
115
116
function posix_kill($pid, $num) {
117
    return true;
118
}
119
120
class TestStorage implements StorageInterface
121
{
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function all()
126
    {
127
        // TODO: Implement all() method.
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function find(QueueName $queueName, $server = null, $procId = null)
134
    {
135
        // TODO: Implement find() method.
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function set(QueueName $queueName, $server, $procId)
142
    {
143
        // TODO: Implement set() method.
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function get(QueueName $queueName, $server, $procId)
150
    {
151
        // TODO: Implement get() method.
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function remove(QueueName $queueName, $server, $procId)
158
    {
159
        // TODO: Implement remove() method.
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function removeForce(QueueName $queueName, $server) {}
166
167
168
169
}
170
171
class TestEntity implements EntityInterface
172
{
173
    use EntityJobNameTrait;
174
175
    private $queue;
176
177
    private $server;
178
179
    private $procId;
180
181
    private $prefix;
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function __construct($queue, $server, $procId)
187
    {
188
        $this->queue = $queue;
189
        $this->server = $server;
190
        $this->procId = $procId;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function getQueue()
197
    {
198
        return $this->queue;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function getServer()
205
    {
206
        return $this->server;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function getProcId()
213
    {
214
        return $this->procId;
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function getPrefix()
221
    {
222
        return $this->prefix;
223
    }
224
225
226
}