From a2a43dfee7218fea37e7349c47301a1e102cf354 Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Tue, 21 Apr 2020 03:09:08 +0400 Subject: [PATCH 1/2] Noissue: Fix Travis tests --- .github/CONTRIBUTING.md | 2 +- .github/ISSUE_TEMPLATE.md | 1 - .travis.yml | 59 ++++++++++++--------- src/Notification.php | 2 +- src/WebPush.php | 16 +----- tests/PushServiceTest.php | 106 ++++++++++++-------------------------- 6 files changed, 72 insertions(+), 114 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 88823f47..e21762e3 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -18,7 +18,7 @@ alias web-push-testing-service='/usr/local/Cellar/node/7.4.0/bin/web-push-testin After that, please create your own configuration file by copying `phpunit.dist.xml` to phpunit.xml and filling in the fields you need for -testing (i.e. STANDARD_ENDPOINT, GCM_API_KEY etc.). +testing (i.e. STANDARD_ENDPOINT, etc.). ## Running Tests diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 127ceb63..d8106462 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -44,7 +44,6 @@ helps with diagnosing problems quicker. # Features Used - [ ] VAPID Support -- [ ] GCM API Key - [ ] Sending with Payload # Example / Reproduce Case diff --git a/.travis.yml b/.travis.yml index a4e1ce2b..5f5f440f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,40 +1,51 @@ -dist: trusty +os: linux +dist: xenial language: php -sudo: required - - # Downloaded browsers will be cached in selenium-assistant by - # web-push-testing-service. Caching will speed up build time. +addons: + chrome: stable + firefox: latest + apt: + update: true + packages: + # Ubuntu 16+ does not install this dependency by default. + - libgconf-2-4 + - chromium-chromedriver + +services: + - xvfb + +# Downloaded browsers will be cached in selenium-assistant by +# web-push-testing-service. Caching will speed up build time. cache: directories: - ~/.selenium-assistant - - node_modules # cache modules too - - $HOME/.composer/cache/files # and composer packages - -matrix: - allow_failures: - - php: nightly - fast_finish: true - include: - - php: 7.1 - - php: 7.2 - - php: 7.3 - - php: nightly + - $COMPOSER_CACHE_DIR + - $HOME/.composer/cache + - node_modules + - vendor + +jobs: + include: + - php: 7.1 + - php: 7.2 + - php: 7.3 + - php: 7.4 + - php: nightly + allow_failures: + - php: nightly + fast_finish: true env: - - TRAVIS_NODE_VERSION="stable" + global: + - TRAVIS_NODE_VERSION="stable" before_install: - nvm install node install: - - npm install github:GoogleChromeLabs/web-push-testing-service -g - -before_script: + - npm i -g "https://github.com/GoogleChromeLabs/web-push-testing-service.git#012508983fa5dd3d25d06f0b937f92c33fc71382" - composer install --prefer-source -n - - "export DISPLAY=:99.0" - - "sh -e /etc/init.d/xvfb start || echo \"Unable to start virtual display.\"" - - sleep 3 # give xvfb some time to start script: - web-push-testing-service start example -p 9012 diff --git a/src/Notification.php b/src/Notification.php index 02a897b5..158bdb31 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -24,7 +24,7 @@ class Notification /** @var array Options : TTL, urgency, topic */ private $options; - /** @var array Auth details : GCM, VAPID */ + /** @var array Auth details : VAPID */ private $auth; /** diff --git a/src/WebPush.php b/src/WebPush.php index 116f789c..ddf04b3a 100644 --- a/src/WebPush.php +++ b/src/WebPush.php @@ -21,9 +21,6 @@ class WebPush { - public const GCM_URL = 'https://android.googleapis.com/gcm/send'; - public const FCM_BASE_URL = 'https://fcm.googleapis.com'; - /** * @var Client */ @@ -201,8 +198,8 @@ public function flush(?int $batchSize = null): \Generator private function prepare(array $notifications): array { $requests = []; - /** @var Notification $notification */ foreach ($notifications as $notification) { + \assert($notification instanceof Notification); $subscription = $notification->getSubscription(); $endpoint = $subscription->getEndpoint(); $userPublicKey = $subscription->getPublicKey(); @@ -254,16 +251,7 @@ private function prepare(array $notifications): array $headers['Topic'] = $options['topic']; } - // if GCM - if (substr($endpoint, 0, strlen(self::GCM_URL)) === self::GCM_URL) { - if (array_key_exists('GCM', $auth)) { - $headers['Authorization'] = 'key='.$auth['GCM']; - } else { - throw new \ErrorException('No GCM API Key specified.'); - } - } - // if VAPID (GCM doesn't support it but FCM does) - elseif (array_key_exists('VAPID', $auth) && $contentEncoding) { + if (array_key_exists('VAPID', $auth) && $contentEncoding) { $audience = parse_url($endpoint, PHP_URL_SCHEME).'://'.parse_url($endpoint, PHP_URL_HOST); if (!parse_url($audience)) { throw new \ErrorException('Audience "'.$audience.'"" could not be generated.'); diff --git a/tests/PushServiceTest.php b/tests/PushServiceTest.php index 57be00c3..9e425a90 100644 --- a/tests/PushServiceTest.php +++ b/tests/PushServiceTest.php @@ -20,8 +20,6 @@ final class PushServiceTest extends PHPUnit\Framework\TestCase private static $portNumber = 9012; private static $testSuiteId; private static $testServiceUrl; - private static $gcmSenderId = '759071690750'; - private static $gcmApiKey = 'AIzaSyBAU0VfXoskxUSg81K5VgLgwblHbZWe6tA'; private static $vapidKeys = [ 'subject' => 'http://test.com', 'publicKey' => 'BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk', @@ -63,30 +61,10 @@ protected function setUp() public function browserProvider() { return [ - // Web Push - ['firefox', 'stable', []], - ['firefox', 'beta', []], - - // Web Push + GCM - ['chrome', 'stable', ['GCM' => self::$gcmApiKey]], - ['chrome', 'beta', ['GCM' => self::$gcmApiKey]], - - ['firefox', 'stable', ['GCM' => self::$gcmApiKey]], - ['firefox', 'beta', ['GCM' => self::$gcmApiKey]], - - // Web Push + VAPID - ['chrome', 'stable', ['VAPID' => self::$vapidKeys]], - ['chrome', 'beta', ['VAPID' => self::$vapidKeys]], - ['firefox', 'stable', ['VAPID' => self::$vapidKeys]], ['firefox', 'beta', ['VAPID' => self::$vapidKeys]], - - // Web Push + GCM + VAPID - ['chrome', 'stable', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]], - ['chrome', 'beta', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]], - - ['firefox', 'stable', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]], - ['firefox', 'beta', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]], + ['chrome', 'stable', ['VAPID' => self::$vapidKeys]], + ['chrome', 'beta', ['VAPID' => self::$vapidKeys]], ]; } @@ -131,10 +109,6 @@ protected function createClosureTest($browserId, $browserVersion, $options) 'browserVersion' => $browserVersion, ]; - if (array_key_exists('GCM', $options)) { - $subscriptionParameters['gcmSenderId'] = self::$gcmSenderId; - } - if (array_key_exists('VAPID', $options)) { $subscriptionParameters['vapidPublicKey'] = self::$vapidKeys['publicKey']; } @@ -175,53 +149,39 @@ protected function createClosureTest($browserId, $browserVersion, $options) } $subscription = new Subscription($endpoint, $p256dh, $auth, $contentEncoding); + $sendResp = $this->webPush->sendNotification($subscription, $payload, true); + $this->assertInstanceOf(\Generator::class, $sendResp); - try { - $sendResp = $this->webPush->sendNotification($subscription, $payload, true); - $this->assertInstanceOf(\Generator::class, $sendResp); - - /** @var \Minishlink\WebPush\MessageSentReport $report */ - foreach ($sendResp as $report) { - $this->assertTrue($report->isSuccess()); - } - - $dataString = json_encode([ - 'testSuiteId' => self::$testSuiteId, - 'testId' => $testId, - ]); - - $getNotificationCurl = curl_init(self::$testServiceUrl.'/api/get-notification-status/'); - curl_setopt_array($getNotificationCurl, [ - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => $dataString, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => [ - 'Content-Type: application/json', - 'Content-Length: '.strlen($dataString), - ], - CURLOPT_TIMEOUT => self::$timeout, - ]); - - $parsedResp = $this->getResponse($getNotificationCurl); - - if (!property_exists($parsedResp->{'data'}, 'messages')) { - throw new Exception('web-push-testing-service error, no messages: '.json_encode($parsedResp)); - } - - $messages = $parsedResp->{'data'}->{'messages'}; - $this->assertEquals(1, count($messages)); - $this->assertEquals($payload, $messages[0]); - } catch (Exception $e) { - if (strpos($endpoint, 'https://android.googleapis.com/gcm/send') === 0 - && !array_key_exists('GCM', $options)) { - if ($e->getMessage() !== 'No GCM API Key specified.') { - echo $e; - } - $this->assertEquals($e->getMessage(), 'No GCM API Key specified.'); - } else { - throw $e; - } + /** @var \Minishlink\WebPush\MessageSentReport $report */ + foreach ($sendResp as $report) { + $this->assertTrue($report->isSuccess()); } + + $dataString = json_encode([ + 'testSuiteId' => self::$testSuiteId, + 'testId' => $testId, + ]); + + $getNotificationCurl = curl_init(self::$testServiceUrl.'/api/get-notification-status/'); + curl_setopt_array($getNotificationCurl, [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $dataString, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + 'Content-Length: '.strlen($dataString), + ], + CURLOPT_TIMEOUT => self::$timeout, + ]); + + $parsedResp = $this->getResponse($getNotificationCurl); + + if (!property_exists($parsedResp->data, 'messages')) { + throw new Exception('web-push-testing-service error, no messages: '.json_encode($parsedResp)); + } + + $this->assertEquals(1, count($parsedResp->data->messages)); + $this->assertEquals($payload, $parsedResp->data->messages[0]); } }; } From a7cf1f36b7e697a5cff7e43c7053ad13b0660918 Mon Sep 17 00:00:00 2001 From: Louis Lagrange Date: Sun, 2 Aug 2020 17:59:34 +0800 Subject: [PATCH 2/2] Fix merge --- tests/PushServiceTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/PushServiceTest.php b/tests/PushServiceTest.php index 066537f2..ed722859 100644 --- a/tests/PushServiceTest.php +++ b/tests/PushServiceTest.php @@ -149,10 +149,8 @@ protected function createClosureTest($browserId, $browserVersion, $options) } $subscription = new Subscription($endpoint, $p256dh, $auth, $contentEncoding); - $sendResp = $this->webPush->sendNotification($subscription, $payload, true); - $this->assertInstanceOf(\Generator::class, $sendResp); - $report = $this->webPush->sendOneNotification($subscription, $payload); + $this->assertInstanceOf(\Generator::class, $report); $this->assertInstanceOf(\Minishlink\WebPush\MessageSentReport::class, $report); $this->assertTrue($report->isSuccess());