1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alawar\NginxPushStreamBundle\Tests\Connection; |
4
|
|
|
|
5
|
|
|
use Alawar\NginxPushStreamBundle\Connection; |
6
|
|
|
use Alawar\NginxPushStreamBundle\Filter\Prefix; |
7
|
|
|
use Alawar\NginxPushStreamBundle\Filter\Hash; |
8
|
|
|
use Alawar\NginxPushStreamBundle\IdGenerator\IdGenerator; |
9
|
|
|
|
10
|
|
|
class ConnectionTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
public $pubUrl = 'http://localhost/pub?id={token}'; |
13
|
|
|
|
14
|
|
|
public $subUrls = array( |
15
|
|
|
'polling' => 'http://localhost/sub-p/{tokens}', |
16
|
|
|
'long-polling' => 'http://localhost/sub-lp/{tokens}', |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
public function testFilterPrefix() |
20
|
|
|
{ |
21
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
22
|
|
|
$c->addFilter(new Prefix(array('prefix' => 'pref_'))); |
23
|
|
|
$this->assertEquals('pref_data', $c->filter('data')); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testFilterHash() |
27
|
|
|
{ |
28
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
29
|
|
|
$c->addFilter(new Hash(array('algo' => 'md5', 'secret' => 'x'))); |
30
|
|
|
$this->assertEquals('6570067ba4b07c4bd953bfc37ee6b48b', $c->filter('data')); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testFilterHashPrefix() |
34
|
|
|
{ |
35
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
36
|
|
|
$c->addFilter(new Hash(array('algo' => 'md5', 'secret' => 'x'))); |
37
|
|
|
$c->addFilter(new Prefix(array('prefix' => 'pref_'))); |
38
|
|
|
$this->assertEquals('pref_6570067ba4b07c4bd953bfc37ee6b48b', $c->filter('data')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testFilterTokens() |
42
|
|
|
{ |
43
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
44
|
|
|
$c->addFilter(new Hash(array('algo' => 'md5', 'secret' => 'x'))); |
45
|
|
|
$c->addFilter(new Prefix(array('prefix' => 'pref_'))); |
46
|
|
|
$this->assertEquals( |
47
|
|
|
array( |
48
|
|
|
'pref_f79ec0211abf003e5fb031bfb66a6e6b', |
49
|
|
|
'pref_1b62a7244c5d2b17fe4f03ba864d0911' |
50
|
|
|
), |
51
|
|
|
$c->filterTokens(array('data1', 'data2')) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testPubUrl() |
56
|
|
|
{ |
57
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
58
|
|
|
$value = $c->getPubUrl('token1'); |
59
|
|
|
$this->assertEquals('http://localhost/pub?id=token1', $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testPubUrlWFilters() |
63
|
|
|
{ |
64
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
65
|
|
|
$c->addFilter(new Prefix(array('prefix' => 'pref_'))); |
66
|
|
|
$value = $c->getPubUrl('token1'); |
67
|
|
|
$this->assertEquals('http://localhost/pub?id=pref_token1', $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testSubUrls() |
71
|
|
|
{ |
72
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
73
|
|
|
$value = $c->getSubUrls(array('token1', 'token2')); |
74
|
|
|
$this->assertEquals( |
75
|
|
|
array( |
76
|
|
|
'polling' => 'http://localhost/sub-p/token1/token2', |
77
|
|
|
'long-polling' => 'http://localhost/sub-lp/token1/token2' |
78
|
|
|
), |
79
|
|
|
$value |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testSubUrlsWFilters() |
84
|
|
|
{ |
85
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
86
|
|
|
$c->addFilter(new Prefix(array('prefix' => 'pref_'))); |
87
|
|
|
$value = $c->getSubUrls(array('token1', 'token2')); |
88
|
|
|
$this->assertEquals( |
89
|
|
|
array( |
90
|
|
|
'polling' => 'http://localhost/sub-p/pref_token1/pref_token2', |
91
|
|
|
'long-polling' => 'http://localhost/sub-lp/pref_token1/pref_token2' |
92
|
|
|
), |
93
|
|
|
$value |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testWoSender() |
98
|
|
|
{ |
99
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
100
|
|
|
$return = $c->send('123', array('type' => 'message', 'from' => 's', 'text' => 'Yay!'), 'new_message', 1); |
101
|
|
|
$this->assertFalse($return); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testSending() |
105
|
|
|
{ |
106
|
|
|
$mock = $this->getMock('Alawar\NginxPushStreamBundle\Http\Sender'); |
107
|
|
|
$mock->expects($this->once())->method('send')->with( |
108
|
|
|
'http://localhost/pub?id=123', |
109
|
|
|
'{"token":"123","id":1,"type":"new_message","data":{"type":"message","from":"s","text":"Yay!"}}' . "\r\n", |
110
|
|
|
array( |
111
|
|
|
'Event-ID' => '1', |
112
|
|
|
'Event-Type' => 'new_message', |
113
|
|
|
'Content-Type' => 'application/json' |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
118
|
|
|
$c->setSender($mock); |
119
|
|
|
$c->send('123', array('type' => 'message', 'from' => 's', 'text' => 'Yay!'), 'new_message', 1); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testSendingWoId() |
123
|
|
|
{ |
124
|
|
|
$mock = $this->getMock('Alawar\NginxPushStreamBundle\Http\Sender'); |
125
|
|
|
$mock->expects($this->once())->method('send')->with( |
126
|
|
|
'http://localhost/pub?id=123', |
127
|
|
|
'{"token":"123","type":"new_message","data":{"type":"message","from":"s","text":"Yay!"}}' . "\r\n", |
128
|
|
|
array( |
129
|
|
|
'Event-Type' => 'new_message', |
130
|
|
|
'Content-Type' => 'application/json' |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
135
|
|
|
$c->setSender($mock); |
136
|
|
|
$c->send('123', array('type' => 'message', 'from' => 's', 'text' => 'Yay!'), 'new_message'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testSendingWoIdButWithGenerator() |
140
|
|
|
{ |
141
|
|
|
$generator = new IdGenerator(); |
142
|
|
|
$id = $generator->generate(); |
143
|
|
|
|
144
|
|
|
$generatorMock = $this->getMock('Alawar\NginxPushStreamBundle\IdGenerator\IdGenerator'); |
145
|
|
|
$generatorMock->expects($this->once())->method('generate')->with()->will($this->returnValue($id)); |
146
|
|
|
|
147
|
|
|
$mock = $this->getMock('Alawar\NginxPushStreamBundle\Http\Sender'); |
148
|
|
|
$mock->expects($this->once())->method('send')->with( |
149
|
|
|
'http://localhost/pub?id=123', |
150
|
|
|
'{"token":"123","id":"' . $id . '","type":"new_message","data":{"type":"message","from":"s","text":"Yay!"}}' . "\r\n", |
151
|
|
|
array( |
152
|
|
|
'Event-ID' => $id, |
153
|
|
|
'Event-Type' => 'new_message', |
154
|
|
|
'Content-Type' => 'application/json' |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$c = new Connection($this->pubUrl, $this->subUrls); |
159
|
|
|
$c->setIdGenerator($generatorMock); |
160
|
|
|
$c->setSender($mock); |
161
|
|
|
$c->send('123', array('type' => 'message', 'from' => 's', 'text' => 'Yay!'), 'new_message'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|