Skip to content

Remove GCM support + trying to fix Travis tests #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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

Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ helps with diagnosing problems quicker.
# Features Used

- [ ] VAPID Support
- [ ] GCM API Key
- [ ] Sending with Payload

# Example / Reproduce Case
Expand Down
58 changes: 34 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
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.2
- php: 7.3
- php: 7.4
- php: nightly
- $COMPOSER_CACHE_DIR
- $HOME/.composer/cache
- node_modules
- vendor

jobs:
include:
- 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
Expand Down
2 changes: 1 addition & 1 deletion src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
16 changes: 2 additions & 14 deletions src/WebPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -209,8 +206,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();
Expand Down Expand Up @@ -262,16 +259,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.');
Expand Down
102 changes: 32 additions & 70 deletions tests/PushServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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]],
];
}

Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -175,49 +149,37 @@ protected function createClosureTest($browserId, $browserVersion, $options)
}

$subscription = new Subscription($endpoint, $p256dh, $auth, $contentEncoding);

try {
$report = $this->webPush->sendOneNotification($subscription, $payload);
$this->assertInstanceOf(\Minishlink\WebPush\MessageSentReport::class, $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;
}
$report = $this->webPush->sendOneNotification($subscription, $payload);
$this->assertInstanceOf(\Generator::class, $report);
$this->assertInstanceOf(\Minishlink\WebPush\MessageSentReport::class, $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]);
}
};
}
Expand Down