forked from clue/redis-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractSerializerTest.php
More file actions
140 lines (119 loc) · 5.39 KB
/
Copy pathAbstractSerializerTest.php
File metadata and controls
140 lines (119 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
use Clue\Redis\Protocol\Serializer\SerializerInterface;
abstract class AbstractSerializerTest extends TestCase
{
/** @var SerializerInterface */
private $serializer;
/**
* @return SerializerInterface
*/
abstract protected function createSerializer();
/**
* @before
*/
public function setUpSerializer()
{
$this->serializer = $this->createSerializer();
}
public function testIntegerReply()
{
$model = $this->serializer->createReplyModel(0);
$this->assertInstanceOf('Clue\Redis\Protocol\Model\IntegerReply', $model);
$this->assertEquals(0, $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(0));
}
public function testFloatCastIntegerReply()
{
$model = $this->serializer->createReplyModel(-12.99);
$this->assertInstanceOf('Clue\Redis\Protocol\Model\IntegerReply', $model);
$this->assertEquals(-12, $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(-12.99));
$model = $this->serializer->createReplyModel(14.99);
$this->assertInstanceOf('Clue\Redis\Protocol\Model\IntegerReply', $model);
$this->assertEquals(14, $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(14.99));
}
public function testBooleanCastIntegerReply()
{
$model = $this->serializer->createReplyModel(true);
$this->assertInstanceOf('Clue\Redis\Protocol\Model\IntegerReply', $model);
$this->assertEquals(1, $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(true));
$model = $this->serializer->createReplyModel(false);
$this->assertInstanceOf('Clue\Redis\Protocol\Model\IntegerReply', $model);
$this->assertEquals(0, $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(false));
}
public function testStringReply()
{
$model = $this->serializer->createReplyModel('test');
$this->assertInstanceOf('Clue\Redis\Protocol\Model\BulkReply', $model);
$this->assertEquals('test', $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage('test'));
}
public function testNullCastNullBulkReply()
{
$model = $this->serializer->createReplyModel(null);
$this->assertInstanceOf('Clue\Redis\Protocol\Model\BulkReply', $model);
$this->assertEquals(null, $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(null));
}
public function testEmptyArrayMultiBulkReply()
{
$model = $this->serializer->createReplyModel(array());
$this->assertInstanceOf('Clue\Redis\Protocol\Model\MultiBulkReply', $model);
$this->assertEquals(array(), $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(array()));
}
public function testArrayMultiBulkReply()
{
$model = $this->serializer->createReplyModel(array('test', 123));
$this->assertInstanceOf('Clue\Redis\Protocol\Model\MultiBulkReply', $model);
$this->assertEquals(array('test', 123), $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(array('test', 123)));
}
public function testErrorReply()
{
$model = $this->serializer->createReplyModel(new Exception('ERR failure'));
$this->assertInstanceOf('Clue\Redis\Protocol\Model\ErrorReply', $model);
$this->assertEquals('ERR failure', $model->getValueNative());
$this->assertEquals($model->getMessageSerialized($this->serializer), $this->serializer->getReplyMessage(new Exception('ERR failure')));
}
public function testInvalidArgument()
{
$this->setExpectedException('InvalidArgumentException');
$this->serializer->createReplyModel((object)array());
}
public function testInvalidReplyData()
{
$this->setExpectedException('InvalidArgumentException');
$this->serializer->getReplyMessage((object)array());
}
/**
*
* @param array $data
* @dataProvider provideRequestMessage
*/
public function testRequestMessage($command, $args)
{
// the model is already unit-tested, so just compare against its message
$model = $this->serializer->createRequestModel($command, $args);
$message = $this->serializer->getRequestMessage($command, $args);
$this->assertEquals($model->getMessageSerialized($this->serializer), $message);
}
public function provideRequestMessage()
{
return array(
array('PING', array()),
array('GET', array('a')),
array('SET', array('a', 'b')),
array('SET', array('empty', ''))
);
}
// public function testBenchCreateRequest()
// {
// for ($i = 0; $i < 100000; ++$i) {
// $this->serializer->createReplyModel(array('a', 'b', 'c'));
// }
// }
}