1313
1414class PushServiceTest extends PHPUnit_Framework_TestCase
1515{
16+ private static $ timeout = 30 ;
1617 private static $ portNumber = 9012 ;
1718 private static $ testSuiteId ;
1819 private static $ testServiceUrl ;
@@ -54,23 +55,11 @@ protected function setUp()
5455 CURLOPT_POST => true ,
5556 CURLOPT_POSTFIELDS => array (),
5657 CURLOPT_RETURNTRANSFER => true ,
57- CURLOPT_TIMEOUT => 30 ,
58+ CURLOPT_TIMEOUT => self :: $ timeout ,
5859 ));
5960
60- $ resp = curl_exec ($ startApiCurl );
61-
62- if ($ resp ) {
63- $ parsedResp = json_decode ($ resp );
64- self ::$ testSuiteId = $ parsedResp ->{'data ' }->{'testSuiteId ' };
65- } else {
66- echo 'Curl error: ' ;
67- echo curl_error ($ startApiCurl );
68-
69- throw new Exception ('Unable to get a test suite from the ' .
70- 'web-push-testing-service ' );
71- }
72-
73- curl_close ($ startApiCurl );
61+ $ parsedResp = $ this ->getResponse ($ startApiCurl );
62+ self ::$ testSuiteId = $ parsedResp ->{'data ' }->{'testSuiteId ' };
7463 }
7564
7665 public function browserProvider ()
@@ -109,19 +98,19 @@ public function browserProvider()
10998 */
11099 public function retryTest ($ retryCount , $ test )
111100 {
112- // just like above without checking the annotation
113- for ($ i = 0 ; $ i < $ retryCount ; $ i ++) {
114- try {
115- $ test ();
116- return ;
101+ // just like above without checking the annotation
102+ for ($ i = 0 ; $ i < $ retryCount ; $ i ++) {
103+ try {
104+ $ test ();
105+
106+ return ;
107+ } catch (Exception $ e ) {
108+ // last one thrown below
109+ }
117110 }
118- catch ( Exception $ e ) {
119- // last one thrown below
111+ if ( isset ( $ e ) ) {
112+ throw $ e ;
120113 }
121- }
122- if ($ e ) {
123- throw $ e ;
124- }
125114 }
126115
127116 /**
@@ -133,109 +122,90 @@ public function testBrowsers($browserId, $browserVersion, $options)
133122 $ this ->retryTest (4 , $ this ->createClosureTest ($ browserId , $ browserVersion , $ options ));
134123 }
135124
136- protected function createClosureTest ($ browserId , $ browserVersion , $ options ) {
137- return function () use ($ browserId , $ browserVersion , $ options ) {
138- $ this ->webPush = new WebPush ($ options );
139- $ this ->webPush ->setAutomaticPadding (false );
140-
141- $ subscriptionParameters = array (
142- 'testSuiteId ' => self ::$ testSuiteId ,
143- 'browserName ' => $ browserId ,
144- 'browserVersion ' => $ browserVersion ,
145- );
146-
147- if (array_key_exists ('GCM ' , $ options )) {
148- $ subscriptionParameters ['gcmSenderId ' ] = self ::$ gcmSenderId ;
149- }
150-
151- if (array_key_exists ('VAPID ' , $ options )) {
152- $ subscriptionParameters ['vapidPublicKey ' ] = self ::$ vapidKeys ['publicKey ' ];
153- }
154-
155- $ subscriptionParameters = json_encode ($ subscriptionParameters );
156-
157- $ getSubscriptionCurl = curl_init (self ::$ testServiceUrl .'/api/get-subscription/ ' );
158- curl_setopt_array ($ getSubscriptionCurl , array (
159- CURLOPT_POST => true ,
160- CURLOPT_POSTFIELDS => $ subscriptionParameters ,
161- CURLOPT_RETURNTRANSFER => true ,
162- CURLOPT_HTTPHEADER => array (
163- 'Content-Type: application/json ' ,
164- 'Content-Length: ' .strlen ($ subscriptionParameters ),
165- ),
166- CURLOPT_TIMEOUT => 30 ,
167- ));
168- $ resp = curl_exec ($ getSubscriptionCurl );
169-
170- // Close request to clear up some resources
171- curl_close ($ getSubscriptionCurl );
172-
173- $ parsedResp = json_decode ($ resp );
174-
175-
125+ protected function createClosureTest ($ browserId , $ browserVersion , $ options )
126+ {
127+ return function () use ($ browserId , $ browserVersion , $ options ) {
128+ $ this ->webPush = new WebPush ($ options );
129+ $ this ->webPush ->setAutomaticPadding (false );
130+
131+ $ subscriptionParameters = array (
132+ 'testSuiteId ' => self ::$ testSuiteId ,
133+ 'browserName ' => $ browserId ,
134+ 'browserVersion ' => $ browserVersion ,
135+ );
136+
137+ if (array_key_exists ('GCM ' , $ options )) {
138+ $ subscriptionParameters ['gcmSenderId ' ] = self ::$ gcmSenderId ;
139+ }
176140
177- if (!array_key_exists ('data ' , $ parsedResp )) {
178- echo "\n\n" ;
179- echo "Unexpected response from web-push-testing-service............. " ;
180- var_dump ($ resp );
181- echo "\n\n" ;
182- throw new Error ('Unexpected response from web-push-testing-service. ' );
183- }
141+ if (array_key_exists ('VAPID ' , $ options )) {
142+ $ subscriptionParameters ['vapidPublicKey ' ] = self ::$ vapidKeys ['publicKey ' ];
143+ }
184144
185- $ testId = $ parsedResp ->{'data ' }->{'testId ' };
186- $ subscription = $ parsedResp ->{'data ' }->{'subscription ' };
187- $ endpoint = $ subscription ->{'endpoint ' };
188- $ keys = $ subscription ->{'keys ' };
189- $ auth = $ keys ->{'auth ' };
190- $ p256dh = $ keys ->{'p256dh ' };
191-
192- $ payload = 'hello ' ;
193- $ getNotificationCurl = null ;
194- try {
195- $ sendResp = $ this ->webPush ->sendNotification ($ endpoint , $ payload , $ p256dh , $ auth , true );
196- $ this ->assertTrue ($ sendResp );
197-
198- $ dataString = json_encode (array (
199- 'testSuiteId ' => self ::$ testSuiteId ,
200- 'testId ' => $ testId ,
201- ));
202-
203- $ getNotificationCurl = curl_init (self ::$ testServiceUrl .'/api/get-notification-status/ ' );
204- curl_setopt_array ($ getNotificationCurl , array (
205- CURLOPT_POST => true ,
206- CURLOPT_POSTFIELDS => $ dataString ,
207- CURLOPT_RETURNTRANSFER => true ,
208- CURLOPT_HTTPHEADER => array (
209- 'Content-Type: application/json ' ,
210- 'Content-Length: ' .strlen ($ dataString ),
211- ),
212- CURLOPT_TIMEOUT => 30 ,
213- ));
214- $ resp = curl_exec ($ getNotificationCurl );
215-
216- $ parsedResp = json_decode ($ resp );
217-
218- $ messages = $ parsedResp ->{'data ' }->{'messages ' };
219- $ this ->assertEquals (count ($ messages ), 1 );
220- $ this ->assertEquals ($ messages [0 ], $ payload );
221- } catch (Exception $ e ) {
222- if (
223- strpos ($ endpoint , 'https://android.googleapis.com/gcm/send ' ) === 0 &&
224- !array_key_exists ('GCM ' , $ options )
225- ) {
226- if ($ e ->getMessage () !== 'No GCM API Key specified. ' ) {
227- echo $ e ;
145+ $ subscriptionParameters = json_encode ($ subscriptionParameters );
146+
147+ $ getSubscriptionCurl = curl_init (self ::$ testServiceUrl .'/api/get-subscription/ ' );
148+ curl_setopt_array ($ getSubscriptionCurl , array (
149+ CURLOPT_POST => true ,
150+ CURLOPT_POSTFIELDS => $ subscriptionParameters ,
151+ CURLOPT_RETURNTRANSFER => true ,
152+ CURLOPT_HTTPHEADER => array (
153+ 'Content-Type: application/json ' ,
154+ 'Content-Length: ' .strlen ($ subscriptionParameters ),
155+ ),
156+ CURLOPT_TIMEOUT => self ::$ timeout ,
157+ ));
158+
159+ $ parsedResp = $ this ->getResponse ($ getSubscriptionCurl );
160+ $ testId = $ parsedResp ->{'data ' }->{'testId ' };
161+ $ subscription = $ parsedResp ->{'data ' }->{'subscription ' };
162+ $ endpoint = $ subscription ->{'endpoint ' };
163+ $ keys = $ subscription ->{'keys ' };
164+ $ auth = $ keys ->{'auth ' };
165+ $ p256dh = $ keys ->{'p256dh ' };
166+
167+ $ payload = 'hello ' ;
168+ $ getNotificationCurl = null ;
169+ try {
170+ $ sendResp = $ this ->webPush ->sendNotification ($ endpoint , $ payload , $ p256dh , $ auth , true );
171+ $ this ->assertTrue ($ sendResp );
172+
173+ $ dataString = json_encode (array (
174+ 'testSuiteId ' => self ::$ testSuiteId ,
175+ 'testId ' => $ testId ,
176+ ));
177+
178+ $ getNotificationCurl = curl_init (self ::$ testServiceUrl .'/api/get-notification-status/ ' );
179+ curl_setopt_array ($ getNotificationCurl , array (
180+ CURLOPT_POST => true ,
181+ CURLOPT_POSTFIELDS => $ dataString ,
182+ CURLOPT_RETURNTRANSFER => true ,
183+ CURLOPT_HTTPHEADER => array (
184+ 'Content-Type: application/json ' ,
185+ 'Content-Length: ' .strlen ($ dataString ),
186+ ),
187+ CURLOPT_TIMEOUT => self ::$ timeout ,
188+ ));
189+
190+ $ parsedResp = $ this ->getResponse ($ getSubscriptionCurl );
191+
192+ if (!property_exists ($ parsedResp ->{'data ' }, 'messages ' )) {
193+ throw new Exception ('web-push-testing-service error, no messages: ' .json_encode ($ parsedResp ));
228194 }
229- $ this ->assertEquals ($ e ->getMessage (), 'No GCM API Key specified. ' );
230- } else {
231- if ($ getNotificationCurl ) {
232- echo 'Curl error: ' ;
233- echo curl_error ($ getNotificationCurl );
195+
196+ $ messages = $ parsedResp ->{'data ' }->{'messages ' };
197+ $ this ->assertEquals (count ($ messages ), 1 );
198+ $ this ->assertEquals ($ messages [0 ], $ payload );
199+ } catch (Exception $ e ) {
200+ if (strpos ($ endpoint , 'https://android.googleapis.com/gcm/send ' ) === 0
201+ && !array_key_exists ('GCM ' , $ options )) {
202+ if ($ e ->getMessage () !== 'No GCM API Key specified. ' ) {
203+ echo $ e ;
204+ }
205+ $ this ->assertEquals ($ e ->getMessage (), 'No GCM API Key specified. ' );
234206 }
235- throw $ e ;
236207 }
237- }
238- };
208+ };
239209 }
240210
241211 protected function tearDown ()
@@ -247,22 +217,39 @@ protected function tearDown()
247217 CURLOPT_POSTFIELDS => $ dataString ,
248218 CURLOPT_RETURNTRANSFER => true ,
249219 CURLOPT_HTTPHEADER => array (
250- 'Content-Type: application/json ' ,
251- 'Content-Length: ' .strlen ($ dataString ),
220+ 'Content-Type: application/json ' ,
221+ 'Content-Length: ' .strlen ($ dataString ),
252222 ),
253- CURLOPT_TIMEOUT => 30 ,
223+ CURLOPT_TIMEOUT => self :: $ timeout ,
254224 ));
255- $ resp = curl_exec ($ curl );
256- $ parsedResp = json_decode ($ resp );
257-
225+ $ this ->getResponse ($ curl );
258226 self ::$ testSuiteId = null ;
259- // Close request to clear up some resources
260- curl_close ($ curl );
261227 }
262228
263229 public static function tearDownAfterClass ()
264230 {
265- $ testingServiceResult = exec (
266- 'web-push-testing-service stop phpunit ' );
231+ exec ('web-push-testing-service stop phpunit ' );
232+ }
233+
234+ private function getResponse ($ ch )
235+ {
236+ $ resp = curl_exec ($ ch );
237+
238+ if (!$ resp ) {
239+ $ error = 'Curl error: n ' .curl_errno ($ ch ).' - ' .curl_error ($ ch );
240+ curl_close ($ ch );
241+ throw new Exception ($ error );
242+ }
243+
244+ $ parsedResp = json_decode ($ resp );
245+
246+ if (!property_exists ($ parsedResp , 'data ' )) {
247+ throw new Exception ('web-push-testing-service error: ' .$ resp );
248+ }
249+
250+ // Close request to clear up some resources
251+ curl_close ($ ch );
252+
253+ return $ parsedResp ;
267254 }
268255}
0 commit comments