Skip to content

Commit ccc2d78

Browse files
authored
Merge pull request #49 from web-push-libs/integration-tests
Adding Integration tests
2 parents c084912 + b030dd7 commit ccc2d78

File tree

4 files changed

+233
-3
lines changed

4 files changed

+233
-3
lines changed

.travis.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
dist: trusty
12
language: php
23

34
php:
45
- 5.6
56
- hhvm
67
- 7.0
78

9+
env:
10+
- TRAVIS_NODE_VERSION="stable"
11+
12+
before_install:
13+
- nvm install node
14+
15+
install:
16+
- npm install [email protected] -g
17+
818
before_script:
919
- composer install --prefer-source -n --no-dev
20+
- "export DISPLAY=:99.0"
21+
- "sh -e /etc/init.d/xvfb start || echo \"Unable to start virtual display.\""
22+
- sleep 3 # give xvfb some time to start
1023

11-
script: phpunit -c phpunit.travis.xml
24+
script:
25+
- web-push-testing-service start example -p 9012
26+
- phpunit -c phpunit.travis.xml
27+
- web-push-testing-service stop example

src/Encryption.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
6060
$userPublicKeyObject = $generator->getPublicKeyFrom($pointUserPublicKey->getX(), $pointUserPublicKey->getY(), $generator->getOrder());
6161

6262
// get shared secret from user public key and local private key
63-
$sharedSecret = hex2bin($math->decHex((string) $userPublicKeyObject->getPoint()->mul($localPrivateKeyObject->getSecret())->getX()));
63+
$localPrivateSecret = $localPrivateKeyObject->getSecret();
64+
65+
$userPublicX = $userPublicKeyObject->getPoint()
66+
->mul($localPrivateSecret)->getX();
67+
// On HHVM this value is on type GMP and throws if cast to a string.
68+
$userPublicX = gmp_strval($userPublicX);
69+
$sharedSecret = hex2bin($math->decHex($userPublicX));
6470

6571
// generate salt
6672
$salt = openssl_random_pseudo_bytes(16);

tests/PushServiceTest.php

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
}

tests/WebPushTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function checkRequirements()
3232
$this->markTestSkipped('This test does not run on Travis.');
3333
}
3434
}
35-
35+
3636
public static function setUpBeforeClass()
3737
{
3838
self::$endpoints = array(
@@ -111,6 +111,9 @@ public function testFlush()
111111
$this->assertTrue($this->webPush->flush());
112112
}
113113

114+
/**
115+
* @skipIfTravis
116+
*/
114117
public function testSendGCMNotificationWithoutGCMApiKey()
115118
{
116119
$webPush = new WebPush();

0 commit comments

Comments
 (0)