13
13
14
14
class WebPushTest extends PHPUnit_Framework_TestCase
15
15
{
16
- private $ endpoints ;
17
- private $ keys ;
18
- private $ tokens ;
16
+ private static $ endpoints ;
17
+ private static $ keys ;
18
+ private static $ tokens ;
19
19
20
20
/** @var WebPush WebPush with correct api keys */
21
21
private $ webPush ;
22
-
23
- public function setUp ()
22
+
23
+ public static function setUpBeforeClass ()
24
24
{
25
- $ this -> endpoints = array (
25
+ self :: $ endpoints = array (
26
26
'standard ' => getenv ('STANDARD_ENDPOINT ' ),
27
27
'GCM ' => getenv ('GCM_ENDPOINT ' ),
28
28
);
29
29
30
- $ this -> keys = array (
30
+ self :: $ keys = array (
31
31
'standard ' => getenv ('USER_PUBLIC_KEY ' ),
32
- 'GCM ' => getenv ('GCM_API_KEY ' ),
32
+ 'GCM ' => getenv ('GCM_USER_PUBLIC_KEY ' ),
33
33
);
34
34
35
- $ this -> tokens = array (
35
+ self :: $ tokens = array (
36
36
'standard ' => getenv ('USER_AUTH_TOKEN ' ),
37
+ 'GCM ' => getenv ('GCM_USER_AUTH_TOKEN ' ),
37
38
);
38
-
39
- $ this ->webPush = new WebPush ($ this ->keys );
40
- $ this ->webPush ->setAutomaticPadding (false ); // disable automatic padding in tests to speed these up
41
39
}
42
40
43
- public function testSendNotification ()
41
+ public function setUp ()
44
42
{
45
- $ res = $ this ->webPush ->sendNotification ($ this ->endpoints ['standard ' ], null , null , null , true );
46
-
47
- $ this ->assertEquals ($ res , true );
43
+ $ this ->webPush = new WebPush (array ('GCM ' => getenv ('GCM_API_KEY ' )));
44
+ $ this ->webPush ->setAutomaticPadding (false ); // disable automatic padding in tests to speed these up
48
45
}
49
46
50
- public function testSendNotificationWithPayload ()
47
+ public function notificationProvider ()
51
48
{
52
- $ res = $ this ->webPush ->sendNotification (
53
- $ this ->endpoints ['standard ' ],
54
- 'test ' ,
55
- $ this ->keys ['standard ' ],
56
- $ this ->tokens ['standard ' ],
57
- true
49
+ self ::setUpBeforeClass (); // dirty hack of PHPUnit limitation
50
+ return array (
51
+ array (self ::$ endpoints ['standard ' ], null , null , null ),
52
+ array (self ::$ endpoints ['standard ' ], '{message: "Plop", tag: "general"} ' , self ::$ keys ['standard ' ], self ::$ tokens ['standard ' ]),
53
+ array (self ::$ endpoints ['standard ' ], '{message: "Plop", tag: "general"} ' , self ::$ keys ['standard ' ], null ),
54
+ array (self ::$ endpoints ['GCM ' ], null , null , null ),
55
+ array (self ::$ endpoints ['GCM ' ], '{message: "Plop", tag: "general"} ' , self ::$ keys ['GCM ' ], self ::$ tokens ['GCM ' ]),
56
+ array (self ::$ endpoints ['GCM ' ], '{message: "Plop", tag: "general"} ' , self ::$ keys ['GCM ' ], null ),
58
57
);
59
-
60
- $ this ->assertTrue ($ res );
61
58
}
62
59
63
- public function testSendNotificationWithPayloadWithoutAuthToken ()
60
+ /**
61
+ * @dataProvider notificationProvider
62
+ *
63
+ * @param string $endpoint
64
+ * @param string $payload
65
+ * @param string $userPublicKey
66
+ * @param string $userAuthKey
67
+ */
68
+ public function testSendNotification ($ endpoint , $ payload , $ userPublicKey , $ userAuthKey )
64
69
{
65
- $ res = $ this ->webPush ->sendNotification (
66
- $ this ->endpoints ['standard ' ],
67
- '{message: "Plop", tag: "general"} ' ,
68
- $ this ->keys ['standard ' ],
69
- null ,
70
- true
71
- );
70
+ $ res = $ this ->webPush ->sendNotification ($ endpoint , $ payload , $ userPublicKey , $ userAuthKey , true );
72
71
73
72
$ this ->assertTrue ($ res );
74
73
}
@@ -77,9 +76,9 @@ public function testSendNotificationWithOldAPI()
77
76
{
78
77
$ this ->setExpectedException ('ErrorException ' , 'The API has changed: sendNotification now takes the optional user auth token as parameter. ' );
79
78
$ this ->webPush ->sendNotification (
80
- $ this -> endpoints ['standard ' ],
79
+ self :: $ endpoints ['standard ' ],
81
80
'test ' ,
82
- $ this -> keys ['standard ' ],
81
+ self :: $ keys ['standard ' ],
83
82
true
84
83
);
85
84
}
@@ -88,54 +87,43 @@ public function testSendNotificationWithTooBigPayload()
88
87
{
89
88
$ this ->setExpectedException ('ErrorException ' , 'Size of payload must not be greater than 4078 octets. ' );
90
89
$ this ->webPush ->sendNotification (
91
- $ this -> endpoints ['standard ' ],
90
+ self :: $ endpoints ['standard ' ],
92
91
str_repeat ('test ' , 1020 ),
93
- $ this -> keys ['standard ' ],
92
+ self :: $ keys ['standard ' ],
94
93
null ,
95
94
true
96
95
);
97
96
}
98
97
99
- public function testSendNotifications ()
100
- {
101
- foreach ($ this ->endpoints as $ endpoint ) {
102
- $ this ->webPush ->sendNotification ($ endpoint );
103
- }
104
-
105
- $ res = $ this ->webPush ->flush ();
106
-
107
- $ this ->assertEquals (true , $ res );
108
- }
109
-
110
98
public function testFlush ()
111
99
{
112
- $ this ->webPush ->sendNotification ($ this -> endpoints ['standard ' ]);
113
- $ this ->assertEquals ( true , $ this ->webPush ->flush ());
100
+ $ this ->webPush ->sendNotification (self :: $ endpoints ['standard ' ]);
101
+ $ this ->assertTrue ( $ this ->webPush ->flush ());
114
102
115
103
// queue has been reset
116
- $ this ->assertEquals ( false , $ this ->webPush ->flush ());
104
+ $ this ->assertFalse ( $ this ->webPush ->flush ());
117
105
118
- $ this ->webPush ->sendNotification ($ this -> endpoints ['standard ' ]);
119
- $ this ->assertEquals ( true , $ this ->webPush ->flush ());
106
+ $ this ->webPush ->sendNotification (self :: $ endpoints ['standard ' ]);
107
+ $ this ->assertTrue ( $ this ->webPush ->flush ());
120
108
}
121
109
122
110
public function testSendGCMNotificationWithoutGCMApiKey ()
123
111
{
124
112
$ webPush = new WebPush ();
125
113
126
114
$ this ->setExpectedException ('ErrorException ' , 'No GCM API Key specified. ' );
127
- $ webPush ->sendNotification ($ this -> endpoints ['GCM ' ], null , null , null , true );
115
+ $ webPush ->sendNotification (self :: $ endpoints ['GCM ' ], null , null , null , true );
128
116
}
129
117
130
118
public function testSendGCMNotificationWithWrongGCMApiKey ()
131
119
{
132
120
$ webPush = new WebPush (array ('GCM ' => 'bar ' ));
133
121
134
- $ res = $ webPush ->sendNotification ($ this -> endpoints ['GCM ' ], null , null , null , true );
122
+ $ res = $ webPush ->sendNotification (self :: $ endpoints ['GCM ' ], null , null , null , true );
135
123
136
124
$ this ->assertTrue (is_array ($ res )); // there has been an error
137
125
$ this ->assertArrayHasKey ('success ' , $ res );
138
- $ this ->assertEquals ( false , $ res ['success ' ]);
126
+ $ this ->assertFalse ( $ res ['success ' ]);
139
127
140
128
$ this ->assertArrayHasKey ('statusCode ' , $ res );
141
129
$ this ->assertEquals (401 , $ res ['statusCode ' ]);
0 commit comments