1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Gendoria\CruftFlake\Server\ZmqServer; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Test the ZMQ server. |
7
|
|
|
* |
8
|
|
|
* @author Tomasz Struczyński <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class ZmqServerTest extends PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
public function testGenerate() |
13
|
|
|
{ |
14
|
|
|
$generator = $this->getMock('\Gendoria\CruftFlake\Generator', array('generate'), array(), '', false); |
15
|
|
|
$generator->expects($this->once()) |
16
|
|
|
->method('generate') |
17
|
|
|
->will($this->returnValue(10)); |
18
|
|
|
|
19
|
|
|
$socket = $this->getMock('ZMQSocket', array('recv', 'send'), array(), '', false); |
20
|
|
|
$socket->expects($this->once()) |
21
|
|
|
->method('recv') |
22
|
|
|
->willReturn('GEN'); |
23
|
|
|
$socket->expects($this->once()) |
24
|
|
|
->method('send') |
25
|
|
|
->with('{"code":200,"message":10}'); |
26
|
|
|
|
27
|
|
|
$server = $this->getMock('\Gendoria\CruftFlake\Zmq\ZmqServer', array('getZmqSocket'), array($generator, 5599, true)); |
28
|
|
|
|
29
|
|
|
$server->expects($this->once()) |
30
|
|
|
->method('getZmqSocket') |
31
|
|
|
->with(5599) |
32
|
|
|
->will($this->returnValue($socket)); |
33
|
|
|
|
34
|
|
|
/* @var $server ZmqServer */ |
35
|
|
|
$server->run(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testStatus() |
39
|
|
|
{ |
40
|
|
|
$generator = $this->getMock('\Gendoria\CruftFlake\Generator', array('status'), array(), '', false); |
41
|
|
|
$generatorStatus = new Gendoria\CruftFlake\GeneratorStatus(1, 1, 1, true); |
42
|
|
|
$generator->expects($this->once()) |
43
|
|
|
->method('status') |
44
|
|
|
->will($this->returnValue($generatorStatus)); |
45
|
|
|
|
46
|
|
|
$socket = $this->getMock('ZMQSocket', array('recv', 'send'), array(), '', false); |
47
|
|
|
$socket->expects($this->once()) |
48
|
|
|
->method('recv') |
49
|
|
|
->willReturn('STATUS'); |
50
|
|
|
$socket->expects($this->once()) |
51
|
|
|
->method('send') |
52
|
|
|
->with('{"code":200,"message":{"machine":1,"lastTime":1,"sequence":1,"is32Bit":true}}'); |
53
|
|
|
|
54
|
|
|
$server = $this->getMock('\Gendoria\CruftFlake\Zmq\ZmqServer', array('getZmqSocket'), array($generator, 5599, true)); |
55
|
|
|
|
56
|
|
|
$server->expects($this->once()) |
57
|
|
|
->method('getZmqSocket') |
58
|
|
|
->with(5599) |
59
|
|
|
->will($this->returnValue($socket)); |
60
|
|
|
|
61
|
|
|
/* @var $server ZmqServer */ |
62
|
|
|
$server->run(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testUnknownCommand() |
66
|
|
|
{ |
67
|
|
|
$generator = $this->getMock('\Gendoria\CruftFlake\Generator', array('status'), array(), '', false); |
68
|
|
|
|
69
|
|
|
$socket = $this->getMock('ZMQSocket', array('recv', 'send'), array(), '', false); |
70
|
|
|
$socket->expects($this->once()) |
71
|
|
|
->method('recv') |
72
|
|
|
->willReturn('DUMMY_UNKNOWN_COMMAND'); |
73
|
|
|
$socket->expects($this->once()) |
74
|
|
|
->method('send') |
75
|
|
|
->with('{"code":404,"message":"UNKNOWN COMMAND"}'); |
76
|
|
|
|
77
|
|
|
$server = $this->getMock('\Gendoria\CruftFlake\Zmq\ZmqServer', array('getZmqSocket'), array($generator, 5599, true)); |
78
|
|
|
|
79
|
|
|
$server->expects($this->once()) |
80
|
|
|
->method('getZmqSocket') |
81
|
|
|
->with(5599) |
82
|
|
|
->will($this->returnValue($socket)); |
83
|
|
|
|
84
|
|
|
/* @var $server ZmqServer */ |
85
|
|
|
$server->run(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|