-
Notifications
You must be signed in to change notification settings - Fork 285
/
unit-test.php
executable file
·157 lines (128 loc) · 5.26 KB
/
unit-test.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
require_once('Pubnub.php');
## ---------------------------------------------------------------------------
## USAGE:
## ---------------------------------------------------------------------------
#
# php ./unit-test.php
# php ./unit-test.php [PUB-KEY] [SUB-KEY] [SECRET-KEY] [USE SSL]
#
## Capture Publish and Subscribe Keys from Command Line
$publish_key = isset($argv[1]) ? $argv[1] : 'demo';
$subscribe_key = isset($argv[2]) ? $argv[2] : 'demo';
$secret_key = isset($argv[3]) ? $argv[3] : false;
$ssl_on = isset($argv[4]);
## ---------------------------------------------------------------------------
## Create Pubnub Object
## ---------------------------------------------------------------------------
$pubnub = new Pubnub( $publish_key, $subscribe_key, $secret_key, $ssl_on );
## ---------------------------------------------------------------------------
## Generate Random Channel Name
## ---------------------------------------------------------------------------
$channel = 'unit-test-' . rand( 0, 100000000 ) . rand( 0, 100000000 );
## ---------------------------------------------------------------------------
## Get History Part 1
## ---------------------------------------------------------------------------
$history = $pubnub->history(array(
'channel' => $channel,
'limit' => 1
));
test( count($history), 0, 'Initial Empty History' );
## ---------------------------------------------------------------------------
## PUBLISH
## ---------------------------------------------------------------------------
$pubish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => 'Hi. (顶顅Ȓ)'
));
test( $pubish_success[0], 1, 'Publish First Message' );
## ---------------------------------------------------------------------------
## Get History Part 2
## ---------------------------------------------------------------------------
$history = $pubnub->history(array(
'channel' => $channel,
'limit' => 1
));
test( count($history), 1, 'History With 1 Item' );
test( $history[0], 'Hi. (顶顅Ȓ)', 'History Message Text == "Hi. (顶顅Ȓ)"' );
## ---------------------------------------------------------------------------
## PUBLISH 2
## ---------------------------------------------------------------------------
$pubish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => 'Hi Again.'
));
test( $pubish_success[0], 1, 'Publish Second Message' );
## ---------------------------------------------------------------------------
## Get History Part 3
## ---------------------------------------------------------------------------
$history = $pubnub->history(array(
'channel' => $channel,
'limit' => 1
));
test( count($history), 1, 'History With 2 Items Limit 1' );
test( $history[0], 'Hi Again.', 'History Message is Text == "Hi Again."' );
$history = $pubnub->history(array(
'channel' => $channel,
'limit' => 2
));
test( count($history), 2, 'History With 2 Item Limit 2' );
test( $history[0], 'Hi. (顶顅Ȓ)', 'History Message Text == "Hi. (顶顅Ȓ)"' );
test( $history[1], 'Hi Again.', 'History Message is Text == "Hi Again."' );
## ---------------------------------------------------------------------------
## Test Timestamp API
## ---------------------------------------------------------------------------
$timestamp = $pubnub->time();
test( $timestamp, true, 'Timestamp API Test: ' . $timestamp );
## ---------------------------------------------------------------------------
## PUBLISH Special Characters
## ---------------------------------------------------------------------------
$craziness = 'crazy -> ~!@#$%^&*()_+`-=\\][{}\'";:,./<>?顶頴ģŃՃ';
$pubish_success = $pubnub->publish(array(
'channel' => $craziness,
'message' => $craziness
));
test( $pubish_success[0], 1, 'Publish Crazy Channel/Message' );
$history = $pubnub->history(array(
'channel' => $craziness,
'limit' => 1
));
test( count($history), 1, 'History With 3 Items Limit 1' );
test( $history[0], $craziness, 'History Message is Crazy' );
## ---------------------------------------------------------------------------
## Test Subscribe
## ---------------------------------------------------------------------------
$message = 1234;
## Generate String to Sign
$string_to_sign = implode( '/', array(
$publish_key,
$subscribe_key,
$secret_key,
$channel,
$message
) );
$signature = $secret_key ? md5($string_to_sign) : '0';
$pubsub_url_test = "http://pubsub.pubnub.com/publish/" .
$publish_key . "/" .
$subscribe_key . "/" .
$signature . "/" .
$channel . "/0/" . $message;
echo("\nHIT THIS URL to CONTINUE -> $pubsub_url_test");
echo("\n\nHit CTRL+C to finish.");
echo("\nYou may continue reloading the URL to keep testing.\n\n");
$pubnub->subscribe(array(
'channel' => $channel,
'callback' => function($message) {
test( $message, 1234, 'Subscribe: ' . $message );
return true;
}
));
## ---------------------------------------------------------------------------
## Unit Test Function
## ---------------------------------------------------------------------------
function test( $val1, $val2, $name ) {
if ($val1 == $val2) echo('PASS: ');
else echo('FAIL: ');
echo("$name\n");
}
?>