|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the WebPush library. |
| 5 | + * |
| 6 | + * (c) Louis Lagrange <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use Minishlink\WebPush\WebPush; |
| 13 | + |
| 14 | +class PushServiceTest extends PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + private static $portNumber = 9012; |
| 17 | + private static $testSuiteId; |
| 18 | + private static $testServiceUrl; |
| 19 | + private static $gcmSenderId = "759071690750"; |
| 20 | + private static $gcmApiKey = "AIzaSyBAU0VfXoskxUSg81K5VgLgwblHbZWe6tA"; |
| 21 | + |
| 22 | + /** @var WebPush WebPush with correct api keys */ |
| 23 | + private $webPush; |
| 24 | + |
| 25 | + /** |
| 26 | + * This check forces these tests to only run on Travis. |
| 27 | + * If we can reliably start and stop web-push-testing-service and |
| 28 | + * detect current OS, we can probably run this automatically |
| 29 | + * for Linux and OS X at a later date. |
| 30 | + */ |
| 31 | + protected function checkRequirements() |
| 32 | + { |
| 33 | + parent::checkRequirements(); |
| 34 | + |
| 35 | + if (!(getenv('TRAVIS') || getenv('CI'))) { |
| 36 | + $this->markTestSkipped('This test does not run on Travis.'); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static function setUpBeforeClass() |
| 41 | + { |
| 42 | + self::$testServiceUrl = "http://localhost:".self::$portNumber; |
| 43 | + } |
| 44 | + |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + $startApiCurl = curl_init(self::$testServiceUrl."/api/start-test-suite/"); |
| 48 | + curl_setopt_array($startApiCurl, array( |
| 49 | + CURLOPT_POST => true, |
| 50 | + CURLOPT_POSTFIELDS => array(), |
| 51 | + CURLOPT_RETURNTRANSFER => true, |
| 52 | + )); |
| 53 | + |
| 54 | + $resp = curl_exec($startApiCurl); |
| 55 | + |
| 56 | + if ($resp) { |
| 57 | + $parsedResp = json_decode($resp); |
| 58 | + self::$testSuiteId = $parsedResp->{'data'}->{'testSuiteId'}; |
| 59 | + } else { |
| 60 | + echo "Curl error: "; |
| 61 | + echo curl_error($startApiCurl); |
| 62 | + |
| 63 | + throw new Exception('Unable to get a test suite from the '. |
| 64 | + 'web-push-testing-service'); |
| 65 | + } |
| 66 | + |
| 67 | + curl_close($startApiCurl); |
| 68 | + } |
| 69 | + |
| 70 | + public function browserProvider() |
| 71 | + { |
| 72 | + return array( |
| 73 | + // Web Push |
| 74 | + array("chrome", "stable", array()), |
| 75 | + array("chrome", "beta", array()), |
| 76 | + array("chrome", "unstable", array()), |
| 77 | + array("firefox", "stable", array()), |
| 78 | + array("firefox", "beta", array()), |
| 79 | + array("firefox", "unstable", array()), |
| 80 | + // Web Push + GCM |
| 81 | + array("chrome", "stable", array('GCM' => self::$gcmApiKey)), |
| 82 | + array("chrome", "beta", array('GCM' => self::$gcmApiKey)), |
| 83 | + array("chrome", "unstable", array('GCM' => self::$gcmApiKey)), |
| 84 | + array("firefox", "stable", array('GCM' => self::$gcmApiKey)), |
| 85 | + array("firefox", "beta", array('GCM' => self::$gcmApiKey)), |
| 86 | + array("firefox", "unstable", array('GCM' => self::$gcmApiKey)), |
| 87 | + // Web Push + VAPID |
| 88 | + // Web Push + GCM + VAPID |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @dataProvider browserProvider |
| 94 | + * Run integration tests with browsers |
| 95 | + */ |
| 96 | + public function testBrowsers($browserId, $browserVersion, $options) |
| 97 | + { |
| 98 | + $this->webPush = new WebPush($options); |
| 99 | + $this->webPush->setAutomaticPadding(false); |
| 100 | + |
| 101 | + $dataString = json_encode(array( |
| 102 | + "testSuiteId" => self::$testSuiteId, |
| 103 | + "browserName" => $browserId, |
| 104 | + "browserVersion" => $browserVersion, |
| 105 | + "gcmSenderId" => self::$gcmSenderId |
| 106 | + )); |
| 107 | + $getSubscriptionCurl = curl_init(self::$testServiceUrl."/api/get-subscription/"); |
| 108 | + curl_setopt_array($getSubscriptionCurl, array( |
| 109 | + CURLOPT_POST => true, |
| 110 | + CURLOPT_POSTFIELDS => $dataString, |
| 111 | + CURLOPT_RETURNTRANSFER => true, |
| 112 | + CURLOPT_HTTPHEADER => array( |
| 113 | + "Content-Type: application/json", |
| 114 | + "Content-Length: " . strlen($dataString) |
| 115 | + ) |
| 116 | + )); |
| 117 | + |
| 118 | + $resp = curl_exec($getSubscriptionCurl); |
| 119 | + |
| 120 | + // Close request to clear up some resources |
| 121 | + curl_close($getSubscriptionCurl); |
| 122 | + |
| 123 | + $parsedResp = json_decode($resp); |
| 124 | + |
| 125 | + $testId = $parsedResp->{'data'}->{'testId'}; |
| 126 | + $subscription = $parsedResp->{'data'}->{'subscription'}; |
| 127 | + $endpoint = $subscription->{'endpoint'}; |
| 128 | + $keys = $subscription->{'keys'}; |
| 129 | + $auth = $keys->{'auth'}; |
| 130 | + $p256dh = $keys->{'p256dh'}; |
| 131 | + |
| 132 | + $payload = 'hello'; |
| 133 | + $getNotificationCurl = null; |
| 134 | + try { |
| 135 | + $sendResp = $this->webPush->sendNotification($endpoint, $payload, $p256dh, $auth, true); |
| 136 | + $this->assertTrue($sendResp); |
| 137 | + |
| 138 | + $dataString = json_encode(array( |
| 139 | + "testSuiteId" => self::$testSuiteId, |
| 140 | + "testId" => $testId |
| 141 | + )); |
| 142 | + |
| 143 | + $getNotificationCurl = curl_init(self::$testServiceUrl."/api/get-notification-status/"); |
| 144 | + curl_setopt_array($getNotificationCurl, array( |
| 145 | + CURLOPT_POST => true, |
| 146 | + CURLOPT_POSTFIELDS => $dataString, |
| 147 | + CURLOPT_RETURNTRANSFER => true, |
| 148 | + CURLOPT_HTTPHEADER => array( |
| 149 | + "Content-Type: application/json", |
| 150 | + "Content-Length: " . strlen($dataString) |
| 151 | + ) |
| 152 | + )); |
| 153 | + $resp = curl_exec($getNotificationCurl); |
| 154 | + |
| 155 | + $parsedResp = json_decode($resp); |
| 156 | + |
| 157 | + $messages = $parsedResp->{'data'}->{'messages'}; |
| 158 | + $this->assertEquals(count($messages), 1); |
| 159 | + $this->assertEquals($messages[0], $payload); |
| 160 | + } catch (Exception $e) { |
| 161 | + if ( |
| 162 | + strpos($endpoint, 'https://android.googleapis.com/gcm/send') === 0 && |
| 163 | + !array_key_exists('GCM', $options) |
| 164 | + ) { |
| 165 | + if ($e->getMessage() !== 'No GCM API Key specified.') { |
| 166 | + echo $e; |
| 167 | + } |
| 168 | + $this->assertEquals($e->getMessage(), 'No GCM API Key specified.'); |
| 169 | + } else { |
| 170 | + if ($getNotificationCurl) { |
| 171 | + echo "Curl error: "; |
| 172 | + echo curl_error($getNotificationCurl); |
| 173 | + } |
| 174 | + throw $e; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + protected function tearDown() |
| 180 | + { |
| 181 | + $dataString = "{ \"testSuiteId\": " . self::$testSuiteId . " }"; |
| 182 | + $curl = curl_init(self::$testServiceUrl."/api/end-test-suite/"); |
| 183 | + curl_setopt_array($curl, array( |
| 184 | + CURLOPT_POST => true, |
| 185 | + CURLOPT_POSTFIELDS => $dataString, |
| 186 | + CURLOPT_RETURNTRANSFER => true, |
| 187 | + CURLOPT_HTTPHEADER => array( |
| 188 | + "Content-Type: application/json", |
| 189 | + "Content-Length: " . strlen($dataString) |
| 190 | + ) |
| 191 | + )); |
| 192 | + $resp = curl_exec($curl); |
| 193 | + $parsedResp = json_decode($resp); |
| 194 | + |
| 195 | + self::$testSuiteId = null; |
| 196 | + // Close request to clear up some resources |
| 197 | + curl_close($curl); |
| 198 | + } |
| 199 | + |
| 200 | + public static function tearDownAfterClass() |
| 201 | + { |
| 202 | + $testingServiceResult = exec( |
| 203 | + "web-push-testing-service stop phpunit"); |
| 204 | + } |
| 205 | +} |
0 commit comments