-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathPushNotificationTest.php
372 lines (288 loc) · 10.7 KB
/
PushNotificationTest.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
use PHPUnit\Framework\TestCase;
use Edujugon\PushNotification\PushNotification;
use Illuminate\Support\Arr;
class PushNotificationTest extends TestCase {
/** @test */
public function push_notification_instance_creation_without_argument_set_gcm_as_service()
{
$push = new PushNotification();
$this->assertInstanceOf('Edujugon\PushNotification\Gcm', $push->service);
}
/** @test */
public function assert_send_method_returns_an_stdClass_instance()
{
$push = new PushNotification();
$push->setMessage(['message'=>'Hello World'])
->setApiKey('AIzaSyAjsu5asdf4N9KyCxCB04')
->setDevicesToken(['howoPaqCPp1pvVsBZ6QUHoEtO_S9-Esel4N7nqeUypQ6ah8MKZKo6jl'])
->setConfig(['dry_run' => true]);
$push = $push->send();
$this->assertInstanceOf('stdClass', $push->getFeedback());
}
/** @test */
public function assert_there_is_an_array_key_called_error()
{
$push = new PushNotification();
$push->setMessage(['message'=>'Hello World'])
->setApiKey('XXofYyQx2SJbumNrs_hUS6Rkrv3W8asd')
->setDevicesToken(['d1WaXouhHG34:AaPA91bF2byCOq-gexmHFqdysYX'])
->setConfig(['dry_run' => true])
->send();
$this->assertTrue(isset($push->feedback->error));
}
/** @test */
public function assert_unregistered_device_tokens_is_an_array()
{
$push = new PushNotification();
$push->setApiKey('wefwef23f23fwef')
->setDevicesToken([
'asdfasdfasdfasdfXCXQ9cvvpLMuxkaJ0ySpWPed3cvz0q4fuG1SXt40-oasdf3nhWE5OKDmatFZaaZ',
'asfasdfasdf_96ssdfsWuhabpZO9Basvz0q4fuG1SXt40-oXH4R5dwYk4rQYTeds3nhWE5OKDmatFZaaZ'
])
->setConfig(['dry_run' => true])
->setMessage(['message' =>'hello world'])
->send();
$this->assertIsArray($push->getUnregisteredDeviceTokens());
}
/** @test */
public function set_and_get_service_config()
{
/** GCM */
$push = new PushNotification();
$push->setConfig(['time_to_live' => 3]);
$this->assertArrayHasKey('time_to_live', $push->config);
$this->assertArrayHasKey('priority', $push->config); //default key
$this->assertIsArray($push->config);
/** APNS */
$pushAPN = new PushNotification('apn');
$pushAPN->setConfig(['time_to_live' => 3]);
$this->assertArrayHasKey('time_to_live', $pushAPN->config);
$this->assertArrayHasKey('certificate', $pushAPN->config); //default key
$this->assertIsArray($pushAPN->config);
}
/** @test */
public function set_message_data()
{
$push = new PushNotification();
$push->setMessage(['message' =>'hello world']);
$this->assertArrayHasKey('message', $push->message);
$this->assertEquals('hello world', $push->message['message']);
}
/** @test */
public function send_method_in_apn_service()
{
$push = new PushNotification('apn');
$message = [
'aps' => [
'alert' => [
'title' => '1 Notification test',
'body' => 'Just for testing purposes'
],
'sound' => 'default'
]
];
$push->setMessage($message)
->setDevicesToken([
'507e3adaf433ae3e6234f35c82f8a43ad0d84218bff08f16ea7be0869f066c0312',
'ac566b885e91ee74a8d12482ae4e1dfd2da1e26881105dec262fcbe0e082a358',
'507e3adaf433ae3e6234f35c82f8a43ad0d84218bff08f16ea7be0869f066c0312'
]);
$push = $push->send();
//var_dump($push->getFeedback());
$this->assertInstanceOf('stdClass', $push->getFeedback());
$this->assertIsArray($push->getUnregisteredDeviceTokens());
}
/** @test */
public function apn_without_certificate()
{
$push = new PushNotification('apn');
$push->setConfig(['custom' => 'Custom Value','certificate' => 'MycustomValue']);
$push->send();
$this->assertTrue(isset($push->feedback->error));
$this->assertFalse($push->feedback->success);
}
/** @test */
public function fcm_assert_send_method_returns_an_stdClass_instance()
{
$push = new PushNotification('fcm');
$push->setMessage(['message'=>'Hello World'])
->setApiKey('asdfasdffasdfasdfasdf')
->setDevicesToken(['asdfasefaefwefwerwerwer'])
->setConfig(['dry_run' => false]);
$push = $push->send();
$this->assertEquals('https://fcm.googleapis.com/fcm/send', $push->url);
$this->assertInstanceOf('stdClass', $push->getFeedback());
}
/** @test */
public function if_push_service_as_argument_is_not_valid_user_gcm_as_default()
{
$push = new PushNotification('asdf');
$this->assertInstanceOf('Edujugon\PushNotification\Gcm', $push->service);
}
/** @test */
public function get_available_push_service_list()
{
$push = new PushNotification();
$this->assertCount(3, $push->servicesList);
$this->assertIsArray($push->servicesList);
}
/** @test */
public function if_argument_in_set_service_method_does_not_exist_set_the_service_by_default(){
$push = new PushNotification();
$push->setService('asdf')->send();
$this->assertInstanceOf('Edujugon\PushNotification\Gcm', $push->service);
$push->setService('fcm');
$this->assertInstanceOf('Edujugon\PushNotification\Fcm', $push->service);
}
/** @test */
public function get_feedback_after_sending_a_notification()
{
$push = new PushNotification('fcm');
$response = $push->setMessage(['message'=>'Hello World'])
->setApiKey('asdfasdffasdfasdfasdf')
->setDevicesToken(['asdfasefaefwefwerwerwer'])
->setConfig(['dry_run' => false])
->send()
->getFeedback();
$this->assertInstanceOf('stdClass', $response);
}
/** @test */
public function apn_feedback()
{
$push = new PushNotification('apn');
$message = [
'aps' => [
'alert' => [
'title' => 'New Notification test',
'body' => 'Just for testing purposes'
],
'sound' => 'default'
]
];
$push->setMessage($message)
->setDevicesToken([
'asdfasdf'
]);
$push->send();
$this->assertInstanceOf('stdClass', $push->getFeedback());
$this->assertIsArray($push->getUnregisteredDeviceTokens());
}
/** @test */
public function allow_apikey_from_config_file()
{
$push = new PushNotification();
$response = $push->setMessage(['message'=>'Hello World'])
->setDevicesToken(['asdfasefaefwefwerwerwer'])
->setConfig(['dry_run' => true])
->send()
->getFeedback();
$this->assertInstanceOf('stdClass', $response);
}
/** @test */
public function fake_unregisteredDevicesToken_with_apn_feedback_response_merged_to_our_custom_feedback()
{
$primary = [
'success' => 3,
'failure' => 1,
'tokenFailList' => ['asdf']
];
$array =[
'apnsFeedback' => [
[
'timestamp' => 121212,
'length' => 23,
'devtoken' => '2121221212'
],
[
'timestamp' => 5454545,
'length' => 32,
'devtoken' => '34343434'
]
]
];
$merge = array_merge($primary, $array);
$obj = json_decode(json_encode($merge));
$tokens = [];
if (! empty($obj->tokenFailList)) {
$tokens = $obj->tokenFailList;
}
if (!empty($obj->apnsFeedback)) {
$tokens = array_merge($tokens, Arr::pluck($obj->apnsFeedback, 'devtoken'));
}
$this->assertTrue(true);
}
public function send_a_notification_by_topic_in_fcm()
{
$push = new PushNotification('fcm');
$response = $push->setMessage(['message'=>'Hello World'])
->setApiKey('asdfasdffasdfasdfasdf')
->setConfig(['dry_run' => false])
->sendByTopic('test')
->getFeedback();
$this->assertInstanceOf('stdClass', $response);
}
/** @test */
public function send_a_notification_by_condition_in_fcm()
{
$push = new PushNotification('fcm');
$response = $push->setMessage(['message'=>'Hello World'])
->setApiKey('asdfasdffasdfasdfasdf')
->setConfig(['dry_run' => false])
->sendByTopic("'dogs' in topics || 'cats' in topics", true)
->getFeedback();
$this->assertInstanceOf('stdClass', $response);
}
/** @test */
public function apn_connection_attempts_default()
{
$push = new PushNotification('apn');
$push->setConfig(['dry_run' => true]);
$key = 'connection_attempts';
$this->assertArrayNotHasKey($key, $push->config);
}
/** @test */
public function set_apn_connect_attempts_override_default()
{
$push = new PushNotification('apn');
$expected = 0;
$push->setConfig([
'dry_run' => true,
'connection_attempts' => $expected,
]);
$key = 'connection_attempts';
$this->assertArrayHasKey($key, $push->config);
$this->assertEquals($expected, $push->config[$key]);
}
/** @test */
public function apn_connect_attempts_bailout_badcert()
{
$push = new PushNotification('apn');
$tmp_name = tempnam(sys_get_temp_dir(), 'apn-tmp');
$fh = fopen($tmp_name, 'w');
fwrite($fh, 'badcert');
fclose($fh);
$expected = 0;
// ZZZ: intentional failure use-case so let's not
// waste time attemping to push with a bad cert.
$push->setConfig([
'dry_run' => true,
'connection_attempts' => 1,
'certificate' => $tmp_name,
]);
$message = [
'aps' => [
'alert' => [
'title' => '1 Notification test',
'body' => 'Just for testing purposes'
],
'sound' => 'default'
]
];
$push->setMessage($message)
->setDevicesToken(['507e3adaf433ae3e6234f35c82f8a43ad0d84218bff08f16ea7be0869f066c0312']);
$push = $push->send();
$this->assertInstanceOf('stdClass', $push->getFeedback());
unlink($tmp_name);
}
}