Skip to content

Commit 130f8e8

Browse files
authored
Merge pull request #51 from web-push-libs/vapid-test-fix
Fixing up tests to hopefully pass
2 parents 72ea2a3 + 75422a8 commit 130f8e8

File tree

4 files changed

+69
-23
lines changed

4 files changed

+69
-23
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ before_install:
1313
- nvm install node
1414

1515
install:
16-
- npm install web-push-testing-service@0.2.1 -g
16+
- npm install web-push-testing-service@0.3.0 -g
1717

1818
before_script:
1919
- composer install --prefer-source -n --no-dev

CONTRIBUTING.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,4 @@ following commands:
2020
**For a Single Test**
2121
`php phpunit.phar . --filter "/::testPadPayload( .*)?$/"` (regex)
2222

23-
Some tests have a custom decorator @skipIfTravis. The reason is that
24-
there's no way in Travis to update the push subscription, so the endpoint
25-
in my phpunit.travis.xml would ultimately expire
26-
(and require a human modification), and the corresponding tests would fail.
2723
But locally, these tests are handy.

tests/PushServiceTest.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ public function browserProvider()
7777
{
7878
return array(
7979
// Web Push
80-
array('chrome', 'stable', array()),
81-
array('chrome', 'beta', array()),
82-
array('chrome', 'unstable', array()),
8380
array('firefox', 'stable', array()),
8481
array('firefox', 'beta', array()),
8582
array('firefox', 'unstable', array()),
@@ -107,12 +104,37 @@ public function browserProvider()
107104
);
108105
}
109106

107+
/**
108+
* Selenium tests are flakey so add retries.
109+
*/
110+
public function retryTest($retryCount, $test)
111+
{
112+
// just like above without checking the annotation
113+
for ($i = 0; $i < $retryCount; $i++) {
114+
try {
115+
$test();
116+
return;
117+
}
118+
catch (Exception $e) {
119+
// last one thrown below
120+
}
121+
}
122+
if ($e) {
123+
throw $e;
124+
}
125+
}
126+
110127
/**
111128
* @dataProvider browserProvider
112129
* Run integration tests with browsers
113130
*/
114131
public function testBrowsers($browserId, $browserVersion, $options)
115132
{
133+
$this->retryTest(4, $this->createClosureTest($browserId, $browserVersion, $options));
134+
}
135+
136+
protected function createClosureTest($browserId, $browserVersion, $options) {
137+
return function() use ($browserId, $browserVersion, $options) {
116138
$this->webPush = new WebPush($options);
117139
$this->webPush->setAutomaticPadding(false);
118140

@@ -143,14 +165,23 @@ public function testBrowsers($browserId, $browserVersion, $options)
143165
),
144166
CURLOPT_TIMEOUT => 30,
145167
));
146-
147168
$resp = curl_exec($getSubscriptionCurl);
148169

149170
// Close request to clear up some resources
150171
curl_close($getSubscriptionCurl);
151172

152173
$parsedResp = json_decode($resp);
153174

175+
176+
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+
}
184+
154185
$testId = $parsedResp->{'data'}->{'testId'};
155186
$subscription = $parsedResp->{'data'}->{'subscription'};
156187
$endpoint = $subscription->{'endpoint'};
@@ -204,6 +235,7 @@ public function testBrowsers($browserId, $browserVersion, $options)
204235
throw $e;
205236
}
206237
}
238+
};
207239
}
208240

209241
protected function tearDown()

tests/WebPushTest.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,41 @@ protected function checkRequirements()
2424
{
2525
parent::checkRequirements();
2626

27-
if (!array_key_exists('skipIfTravis', $this->getAnnotations()['method'])) {
28-
return;
29-
}
30-
3127
if (getenv('TRAVIS') || getenv('CI')) {
3228
$this->markTestSkipped('This test does not run on Travis.');
3329
}
30+
31+
if (!getenv('STANDARD_ENDPOINT')) {
32+
$this->markTestSkipped('No \'STANDARD_ENDPOINT\' found in env.');
33+
}
34+
35+
if (!getenv('GCM_ENDPOINT')) {
36+
$this->markTestSkipped('No \'GCM_ENDPOINT\' found in env.');
37+
}
38+
39+
if (!getenv('USER_PUBLIC_KEY')) {
40+
$this->markTestSkipped('No \'USER_PUBLIC_KEY\' found in env.');
41+
}
42+
43+
if (!getenv('GCM_USER_PUBLIC_KEY')) {
44+
$this->markTestSkipped('No \'GCM_USER_PUBLIC_KEY\' found in env.');
45+
}
46+
47+
if (!getenv('USER_AUTH_TOKEN')) {
48+
$this->markTestSkipped('No \'USER_PUBLIC_KEY\' found in env.');
49+
}
50+
51+
if (!getenv('GCM_USER_AUTH_TOKEN')) {
52+
$this->markTestSkipped('No \'GCM_USER_AUTH_TOKEN\' found in env.');
53+
}
54+
55+
if (!getenv('VAPID_PUBLIC_KEY')) {
56+
$this->markTestSkipped('No \'VAPID_PUBLIC_KEY\' found in env.');
57+
}
58+
59+
if (!getenv('VAPID_PRIVATE_KEY')) {
60+
$this->markTestSkipped('No \'VAPID_PRIVATE_KEY\' found in env.');
61+
}
3462
}
3563

3664
public static function setUpBeforeClass()
@@ -77,7 +105,6 @@ public function notificationProvider()
77105

78106
/**
79107
* @dataProvider notificationProvider
80-
* @skipIfTravis
81108
*
82109
* @param string $endpoint
83110
* @param string $payload
@@ -103,9 +130,6 @@ public function testSendNotificationWithTooBigPayload()
103130
);
104131
}
105132

106-
/**
107-
* @skipIfTravis
108-
*/
109133
public function testFlush()
110134
{
111135
$this->webPush->sendNotification(self::$endpoints['standard']);
@@ -118,9 +142,6 @@ public function testFlush()
118142
$this->assertTrue($this->webPush->flush());
119143
}
120144

121-
/**
122-
* @skipIfTravis
123-
*/
124145
public function testSendGCMNotificationWithoutGCMApiKey()
125146
{
126147
$webPush = new WebPush();
@@ -129,9 +150,6 @@ public function testSendGCMNotificationWithoutGCMApiKey()
129150
$webPush->sendNotification(self::$endpoints['GCM'], null, null, null, true);
130151
}
131152

132-
/**
133-
* @skipIfTravis
134-
*/
135153
public function testSendGCMNotificationWithWrongGCMApiKey()
136154
{
137155
$webPush = new WebPush(array('GCM' => 'bar'));

0 commit comments

Comments
 (0)