Skip to content

Commit 339f45e

Browse files
committed
Merge branch 'master' of github.com:web-push-libs/web-push-php
2 parents a30383b + 6e6afca commit 339f45e

File tree

6 files changed

+70
-111
lines changed

6 files changed

+70
-111
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ alias web-push-testing-service='/usr/local/Cellar/node/7.4.0/bin/web-push-testin
3030

3131
After that, please create your own configuration file by copying
3232
`phpunit.dist.xml` to phpunit.xml and filling in the fields you need for
33-
testing (i.e. STANDARD_ENDPOINT, GCM_API_KEY etc.).
33+
testing (i.e. STANDARD_ENDPOINT, etc.).
3434

3535
## Running Tests
3636

.github/ISSUE_TEMPLATE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ helps with diagnosing problems quicker.
4444
# Features Used
4545

4646
- [ ] VAPID Support
47-
- [ ] GCM API Key
4847
- [ ] Sending with Payload
4948

5049
# Example / Reproduce Case

.travis.yml

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
1-
dist: trusty
1+
os: linux
2+
dist: xenial
23
language: php
34

4-
sudo: required
5-
6-
# Downloaded browsers will be cached in selenium-assistant by
7-
# web-push-testing-service. Caching will speed up build time.
5+
addons:
6+
chrome: stable
7+
firefox: latest
8+
apt:
9+
update: true
10+
packages:
11+
# Ubuntu 16+ does not install this dependency by default.
12+
- libgconf-2-4
13+
- chromium-chromedriver
14+
15+
services:
16+
- xvfb
17+
18+
# Downloaded browsers will be cached in selenium-assistant by
19+
# web-push-testing-service. Caching will speed up build time.
820
cache:
921
directories:
1022
- ~/.selenium-assistant
11-
- node_modules # cache modules too
12-
- $HOME/.composer/cache/files # and composer packages
13-
14-
matrix:
15-
allow_failures:
16-
- php: nightly
17-
fast_finish: true
18-
include:
19-
- php: 7.2
20-
- php: 7.3
21-
- php: 7.4
22-
- php: nightly
23+
- $COMPOSER_CACHE_DIR
24+
- $HOME/.composer/cache
25+
- node_modules
26+
- vendor
27+
28+
jobs:
29+
include:
30+
- php: 7.2
31+
- php: 7.3
32+
- php: 7.4
33+
- php: nightly
34+
allow_failures:
35+
- php: nightly
36+
fast_finish: true
2337

2438
env:
25-
- TRAVIS_NODE_VERSION="stable"
39+
global:
40+
- TRAVIS_NODE_VERSION="stable"
2641

2742
before_install:
2843
- nvm install node
2944

3045
install:
31-
- npm install github:GoogleChromeLabs/web-push-testing-service -g
32-
33-
before_script:
46+
- npm i -g "https://github.com/GoogleChromeLabs/web-push-testing-service.git#012508983fa5dd3d25d06f0b937f92c33fc71382"
3447
- composer install --prefer-source -n
35-
- "export DISPLAY=:99.0"
36-
- "sh -e /etc/init.d/xvfb start || echo \"Unable to start virtual display.\""
37-
- sleep 3 # give xvfb some time to start
3848

3949
script:
4050
- web-push-testing-service start example -p 9012

src/Notification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Notification
2424
/** @var array Options : TTL, urgency, topic */
2525
private $options;
2626

27-
/** @var array Auth details : GCM, VAPID */
27+
/** @var array Auth details : VAPID */
2828
private $auth;
2929

3030
/**

src/WebPush.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121

2222
class WebPush
2323
{
24-
public const GCM_URL = 'https://android.googleapis.com/gcm/send';
25-
public const FCM_BASE_URL = 'https://fcm.googleapis.com';
26-
2724
/**
2825
* @var Client
2926
*/
@@ -209,8 +206,8 @@ public function flush(?int $batchSize = null): \Generator
209206
private function prepare(array $notifications): array
210207
{
211208
$requests = [];
212-
/** @var Notification $notification */
213209
foreach ($notifications as $notification) {
210+
\assert($notification instanceof Notification);
214211
$subscription = $notification->getSubscription();
215212
$endpoint = $subscription->getEndpoint();
216213
$userPublicKey = $subscription->getPublicKey();
@@ -262,16 +259,7 @@ private function prepare(array $notifications): array
262259
$headers['Topic'] = $options['topic'];
263260
}
264261

265-
// if GCM
266-
if (substr($endpoint, 0, strlen(self::GCM_URL)) === self::GCM_URL) {
267-
if (array_key_exists('GCM', $auth)) {
268-
$headers['Authorization'] = 'key='.$auth['GCM'];
269-
} else {
270-
throw new \ErrorException('No GCM API Key specified.');
271-
}
272-
}
273-
// if VAPID (GCM doesn't support it but FCM does)
274-
elseif (array_key_exists('VAPID', $auth) && $contentEncoding) {
262+
if (array_key_exists('VAPID', $auth) && $contentEncoding) {
275263
$audience = parse_url($endpoint, PHP_URL_SCHEME).'://'.parse_url($endpoint, PHP_URL_HOST);
276264
if (!parse_url($audience)) {
277265
throw new \ErrorException('Audience "'.$audience.'"" could not be generated.');

tests/PushServiceTest.php

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ final class PushServiceTest extends PHPUnit\Framework\TestCase
2020
private static $portNumber = 9012;
2121
private static $testSuiteId;
2222
private static $testServiceUrl;
23-
private static $gcmSenderId = '759071690750';
24-
private static $gcmApiKey = 'AIzaSyBAU0VfXoskxUSg81K5VgLgwblHbZWe6tA';
2523
private static $vapidKeys = [
2624
'subject' => 'http://test.com',
2725
'publicKey' => 'BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk',
@@ -63,30 +61,10 @@ protected function setUp()
6361
public function browserProvider()
6462
{
6563
return [
66-
// Web Push
67-
['firefox', 'stable', []],
68-
['firefox', 'beta', []],
69-
70-
// Web Push + GCM
71-
['chrome', 'stable', ['GCM' => self::$gcmApiKey]],
72-
['chrome', 'beta', ['GCM' => self::$gcmApiKey]],
73-
74-
['firefox', 'stable', ['GCM' => self::$gcmApiKey]],
75-
['firefox', 'beta', ['GCM' => self::$gcmApiKey]],
76-
77-
// Web Push + VAPID
78-
['chrome', 'stable', ['VAPID' => self::$vapidKeys]],
79-
['chrome', 'beta', ['VAPID' => self::$vapidKeys]],
80-
8164
['firefox', 'stable', ['VAPID' => self::$vapidKeys]],
8265
['firefox', 'beta', ['VAPID' => self::$vapidKeys]],
83-
84-
// Web Push + GCM + VAPID
85-
['chrome', 'stable', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]],
86-
['chrome', 'beta', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]],
87-
88-
['firefox', 'stable', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]],
89-
['firefox', 'beta', ['GCM' => self::$gcmApiKey, 'VAPID' => self::$vapidKeys]],
66+
['chrome', 'stable', ['VAPID' => self::$vapidKeys]],
67+
['chrome', 'beta', ['VAPID' => self::$vapidKeys]],
9068
];
9169
}
9270

@@ -131,10 +109,6 @@ protected function createClosureTest($browserId, $browserVersion, $options)
131109
'browserVersion' => $browserVersion,
132110
];
133111

134-
if (array_key_exists('GCM', $options)) {
135-
$subscriptionParameters['gcmSenderId'] = self::$gcmSenderId;
136-
}
137-
138112
if (array_key_exists('VAPID', $options)) {
139113
$subscriptionParameters['vapidPublicKey'] = self::$vapidKeys['publicKey'];
140114
}
@@ -175,49 +149,37 @@ protected function createClosureTest($browserId, $browserVersion, $options)
175149
}
176150

177151
$subscription = new Subscription($endpoint, $p256dh, $auth, $contentEncoding);
178-
179-
try {
180-
$report = $this->webPush->sendOneNotification($subscription, $payload);
181-
$this->assertInstanceOf(\Minishlink\WebPush\MessageSentReport::class, $report);
182-
$this->assertTrue($report->isSuccess());
183-
184-
$dataString = json_encode([
185-
'testSuiteId' => self::$testSuiteId,
186-
'testId' => $testId,
187-
]);
188-
189-
$getNotificationCurl = curl_init(self::$testServiceUrl.'/api/get-notification-status/');
190-
curl_setopt_array($getNotificationCurl, [
191-
CURLOPT_POST => true,
192-
CURLOPT_POSTFIELDS => $dataString,
193-
CURLOPT_RETURNTRANSFER => true,
194-
CURLOPT_HTTPHEADER => [
195-
'Content-Type: application/json',
196-
'Content-Length: '.strlen($dataString),
197-
],
198-
CURLOPT_TIMEOUT => self::$timeout,
199-
]);
200-
201-
$parsedResp = $this->getResponse($getNotificationCurl);
202-
203-
if (!property_exists($parsedResp->{'data'}, 'messages')) {
204-
throw new Exception('web-push-testing-service error, no messages: '.json_encode($parsedResp));
205-
}
206-
207-
$messages = $parsedResp->{'data'}->{'messages'};
208-
$this->assertEquals(1, count($messages));
209-
$this->assertEquals($payload, $messages[0]);
210-
} catch (Exception $e) {
211-
if (strpos($endpoint, 'https://android.googleapis.com/gcm/send') === 0
212-
&& !array_key_exists('GCM', $options)) {
213-
if ($e->getMessage() !== 'No GCM API Key specified.') {
214-
echo $e;
215-
}
216-
$this->assertEquals($e->getMessage(), 'No GCM API Key specified.');
217-
} else {
218-
throw $e;
219-
}
152+
$report = $this->webPush->sendOneNotification($subscription, $payload);
153+
$this->assertInstanceOf(\Generator::class, $report);
154+
$this->assertInstanceOf(\Minishlink\WebPush\MessageSentReport::class, $report);
155+
$this->assertTrue($report->isSuccess());
156+
157+
$dataString = json_encode([
158+
'testSuiteId' => self::$testSuiteId,
159+
'testId' => $testId,
160+
]);
161+
162+
$getNotificationCurl = curl_init(self::$testServiceUrl.'/api/get-notification-status/');
163+
curl_setopt_array($getNotificationCurl, [
164+
CURLOPT_POST => true,
165+
CURLOPT_POSTFIELDS => $dataString,
166+
CURLOPT_RETURNTRANSFER => true,
167+
CURLOPT_HTTPHEADER => [
168+
'Content-Type: application/json',
169+
'Content-Length: '.strlen($dataString),
170+
],
171+
CURLOPT_TIMEOUT => self::$timeout,
172+
]);
173+
174+
$parsedResp = $this->getResponse($getNotificationCurl);
175+
176+
if (!property_exists($parsedResp->{'data'}, 'messages')) {
177+
throw new Exception('web-push-testing-service error, no messages: '.json_encode($parsedResp));
220178
}
179+
180+
$messages = $parsedResp->{'data'}->{'messages'};
181+
$this->assertEquals(1, count($messages));
182+
$this->assertEquals($payload, $messages[0]);
221183
}
222184
};
223185
}

0 commit comments

Comments
 (0)