Skip to content

Commit 51f0f44

Browse files
committed
add GCM with payload test
1 parent 00e6985 commit 51f0f44

File tree

2 files changed

+48
-57
lines changed

2 files changed

+48
-57
lines changed

phpunit.dist.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
</testsuites>
1717
<php>
1818
<env name="STANDARD_ENDPOINT" value="" />
19-
<env name="GCM_ENDPOINT" value="" />
2019
<env name="USER_PUBLIC_KEY" value="" />
2120
<env name="USER_AUTH_TOKEN" value="" />
21+
22+
<env name="GCM_ENDPOINT" value="" />
23+
<env name="GCM_USER_PUBLIC_KEY" value="" />
24+
<env name="GCM_USER_AUTH_TOKEN" value="" />
2225
<env name="GCM_API_KEY" value="" />
2326
</php>
2427
</phpunit>

tests/WebPushTest.php

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,61 @@
1313

1414
class WebPushTest extends PHPUnit_Framework_TestCase
1515
{
16-
private $endpoints;
17-
private $keys;
18-
private $tokens;
16+
private static $endpoints;
17+
private static $keys;
18+
private static $tokens;
1919

2020
/** @var WebPush WebPush with correct api keys */
2121
private $webPush;
22-
23-
public function setUp()
22+
23+
public static function setUpBeforeClass()
2424
{
25-
$this->endpoints = array(
25+
self::$endpoints = array(
2626
'standard' => getenv('STANDARD_ENDPOINT'),
2727
'GCM' => getenv('GCM_ENDPOINT'),
2828
);
2929

30-
$this->keys = array(
30+
self::$keys = array(
3131
'standard' => getenv('USER_PUBLIC_KEY'),
32-
'GCM' => getenv('GCM_API_KEY'),
32+
'GCM' => getenv('GCM_USER_PUBLIC_KEY'),
3333
);
3434

35-
$this->tokens = array(
35+
self::$tokens = array(
3636
'standard' => getenv('USER_AUTH_TOKEN'),
37+
'GCM' => getenv('GCM_USER_AUTH_TOKEN'),
3738
);
38-
39-
$this->webPush = new WebPush($this->keys);
40-
$this->webPush->setAutomaticPadding(false); // disable automatic padding in tests to speed these up
4139
}
4240

43-
public function testSendNotification()
41+
public function setUp()
4442
{
45-
$res = $this->webPush->sendNotification($this->endpoints['standard'], null, null, null, true);
46-
47-
$this->assertEquals($res, true);
43+
$this->webPush = new WebPush(array('GCM' => getenv('GCM_API_KEY')));
44+
$this->webPush->setAutomaticPadding(false); // disable automatic padding in tests to speed these up
4845
}
4946

50-
public function testSendNotificationWithPayload()
47+
public function notificationProvider()
5148
{
52-
$res = $this->webPush->sendNotification(
53-
$this->endpoints['standard'],
54-
'test',
55-
$this->keys['standard'],
56-
$this->tokens['standard'],
57-
true
49+
self::setUpBeforeClass(); // dirty hack of PHPUnit limitation
50+
return array(
51+
array(self::$endpoints['standard'], null, null, null),
52+
array(self::$endpoints['standard'], '{message: "Plop", tag: "general"}', self::$keys['standard'], self::$tokens['standard']),
53+
array(self::$endpoints['standard'], '{message: "Plop", tag: "general"}', self::$keys['standard'], null),
54+
array(self::$endpoints['GCM'], null, null, null),
55+
array(self::$endpoints['GCM'], '{message: "Plop", tag: "general"}', self::$keys['GCM'], self::$tokens['GCM']),
56+
array(self::$endpoints['GCM'], '{message: "Plop", tag: "general"}', self::$keys['GCM'], null),
5857
);
59-
60-
$this->assertTrue($res);
6158
}
6259

63-
public function testSendNotificationWithPayloadWithoutAuthToken()
60+
/**
61+
* @dataProvider notificationProvider
62+
*
63+
* @param string $endpoint
64+
* @param string $payload
65+
* @param string $userPublicKey
66+
* @param string $userAuthKey
67+
*/
68+
public function testSendNotification($endpoint, $payload, $userPublicKey, $userAuthKey)
6469
{
65-
$res = $this->webPush->sendNotification(
66-
$this->endpoints['standard'],
67-
'{message: "Plop", tag: "general"}',
68-
$this->keys['standard'],
69-
null,
70-
true
71-
);
70+
$res = $this->webPush->sendNotification($endpoint, $payload, $userPublicKey, $userAuthKey, true);
7271

7372
$this->assertTrue($res);
7473
}
@@ -77,9 +76,9 @@ public function testSendNotificationWithOldAPI()
7776
{
7877
$this->setExpectedException('ErrorException', 'The API has changed: sendNotification now takes the optional user auth token as parameter.');
7978
$this->webPush->sendNotification(
80-
$this->endpoints['standard'],
79+
self::$endpoints['standard'],
8180
'test',
82-
$this->keys['standard'],
81+
self::$keys['standard'],
8382
true
8483
);
8584
}
@@ -88,54 +87,43 @@ public function testSendNotificationWithTooBigPayload()
8887
{
8988
$this->setExpectedException('ErrorException', 'Size of payload must not be greater than 4078 octets.');
9089
$this->webPush->sendNotification(
91-
$this->endpoints['standard'],
90+
self::$endpoints['standard'],
9291
str_repeat('test', 1020),
93-
$this->keys['standard'],
92+
self::$keys['standard'],
9493
null,
9594
true
9695
);
9796
}
9897

99-
public function testSendNotifications()
100-
{
101-
foreach($this->endpoints as $endpoint) {
102-
$this->webPush->sendNotification($endpoint);
103-
}
104-
105-
$res = $this->webPush->flush();
106-
107-
$this->assertEquals(true, $res);
108-
}
109-
11098
public function testFlush()
11199
{
112-
$this->webPush->sendNotification($this->endpoints['standard']);
113-
$this->assertEquals(true, $this->webPush->flush());
100+
$this->webPush->sendNotification(self::$endpoints['standard']);
101+
$this->assertTrue($this->webPush->flush());
114102

115103
// queue has been reset
116-
$this->assertEquals(false, $this->webPush->flush());
104+
$this->assertFalse($this->webPush->flush());
117105

118-
$this->webPush->sendNotification($this->endpoints['standard']);
119-
$this->assertEquals(true, $this->webPush->flush());
106+
$this->webPush->sendNotification(self::$endpoints['standard']);
107+
$this->assertTrue($this->webPush->flush());
120108
}
121109

122110
public function testSendGCMNotificationWithoutGCMApiKey()
123111
{
124112
$webPush = new WebPush();
125113

126114
$this->setExpectedException('ErrorException', 'No GCM API Key specified.');
127-
$webPush->sendNotification($this->endpoints['GCM'], null, null, null, true);
115+
$webPush->sendNotification(self::$endpoints['GCM'], null, null, null, true);
128116
}
129117

130118
public function testSendGCMNotificationWithWrongGCMApiKey()
131119
{
132120
$webPush = new WebPush(array('GCM' => 'bar'));
133121

134-
$res = $webPush->sendNotification($this->endpoints['GCM'], null, null, null, true);
122+
$res = $webPush->sendNotification(self::$endpoints['GCM'], null, null, null, true);
135123

136124
$this->assertTrue(is_array($res)); // there has been an error
137125
$this->assertArrayHasKey('success', $res);
138-
$this->assertEquals(false, $res['success']);
126+
$this->assertFalse($res['success']);
139127

140128
$this->assertArrayHasKey('statusCode', $res);
141129
$this->assertEquals(401, $res['statusCode']);

0 commit comments

Comments
 (0)