Skip to content

Commit 4cefc22

Browse files
t1gorMinishlink
authored andcommitted
Feature report enhance (#200)
* Add missing error documentation * Add helper methods for report * Rename notifications count method
1 parent 37961a2 commit 4cefc22

File tree

5 files changed

+198
-2
lines changed

5 files changed

+198
-2
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
"homepage": "https://github.com/Minishlink"
1313
}
1414
],
15+
"scripts": {
16+
"test": "./vendor/bin/phpunit --color"
17+
},
1518
"require": {
1619
"php": "^7.1",
20+
"ext-json": "*",
21+
"ext-gmp": "*",
1722
"lib-openssl": "*",
1823
"guzzlehttp/guzzle": "^6.2",
1924
"web-token/jwt-signature": "^1.0",

src/MessageSentReport.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Standardized response from sending a message
1414
*/
15-
class MessageSentReport {
15+
class MessageSentReport implements \JsonSerializable {
1616

1717
/**
1818
* @var boolean
@@ -136,4 +136,31 @@ public function setReason(string $reason): MessageSentReport {
136136
$this->reason = $reason;
137137
return $this;
138138
}
139+
140+
/**
141+
* @return string
142+
*/
143+
public function getRequestPayload(): string {
144+
return $this->request->getBody()->getContents();
145+
}
146+
147+
/**
148+
* @return string
149+
*/
150+
public function getResponseContent(): string {
151+
return $this->response->getBody()->getContents();
152+
}
153+
154+
/**
155+
* @return array|mixed
156+
*/
157+
public function jsonSerialize() {
158+
return [
159+
'success' => $this->isSuccess(),
160+
'expired' => $this->isSubscriptionExpired(),
161+
'reason' => $this->reason,
162+
'endpoint' => $this->getEndpoint(),
163+
'payload' => $this->request->getBody()->getContents(),
164+
];
165+
}
139166
}

src/WebPush.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function sendNotification(Subscription $subscription, ?string $payload =
139139
*/
140140
public function flush(?int $batchSize = null) : iterable
141141
{
142-
if (empty($this->notifications)) {
142+
if (null === $this->notifications || empty($this->notifications)) {
143143
yield from [];
144144
}
145145

@@ -339,4 +339,11 @@ public function setDefaultOptions(array $defaultOptions)
339339
$this->defaultOptions['topic'] = isset($defaultOptions['topic']) ? $defaultOptions['topic'] : null;
340340
$this->defaultOptions['batchSize'] = isset($defaultOptions['batchSize']) ? $defaultOptions['batchSize'] : 1000;
341341
}
342+
343+
/**
344+
* @return int
345+
*/
346+
public function countPendingNotifications(): int {
347+
return null !== $this->notifications ? count($this->notifications) : 0;
348+
}
342349
}

tests/MessageSentReportTest.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* @author Igor Timoshenkov [[email protected]]
4+
* @started: 2018-12-03 11:31
5+
*/
6+
7+
use GuzzleHttp\Psr7\Request;
8+
use \Minishlink\WebPush\MessageSentReport;
9+
use \GuzzleHttp\Psr7\Response;
10+
11+
/**
12+
* @covers \Minishlink\WebPush\MessageSentReport
13+
*/
14+
class MessageSentReportTest extends \PHPUnit\Framework\TestCase {
15+
16+
/**
17+
* @param MessageSentReport $report
18+
* @param bool $expected
19+
* @dataProvider generateReportsWithExpiration
20+
*/
21+
public function testIsSubscriptionExpired(MessageSentReport $report, bool $expected): void {
22+
$this->assertEquals($expected, $report->isSubscriptionExpired());
23+
}
24+
25+
/**
26+
* @return array
27+
*/
28+
public function generateReportsWithExpiration(): array {
29+
return [
30+
[new MessageSentReport(null, new Response(404)), true],
31+
[new MessageSentReport(null, new Response(410)), true],
32+
[new MessageSentReport(null, new Response(500)), false],
33+
[new MessageSentReport(null, new Response(200)), false]
34+
];
35+
}
36+
37+
/**
38+
* @param MessageSentReport $report
39+
* @param string $expected
40+
* @dataProvider generateReportsWithEndpoints
41+
*/
42+
public function testGetEndpoint(MessageSentReport $report, string $expected): void {
43+
$this->assertEquals($expected, $report->getEndpoint());
44+
}
45+
46+
/**
47+
* @return array
48+
*/
49+
public function generateReportsWithEndpoints(): array {
50+
return [
51+
[new MessageSentReport(new Request('POST', 'https://www.example.com')), 'https://www.example.com'],
52+
[new MessageSentReport(new Request('POST', 'https://m.example.com')), 'https://m.example.com'],
53+
[new MessageSentReport(new Request('POST', 'https://test.net')), 'https://test.net'],
54+
];
55+
}
56+
57+
/**
58+
* @param MessageSentReport $report
59+
* @param Request $expected
60+
* @dataProvider generateReportsWithRequests
61+
*/
62+
public function testGetRequest(MessageSentReport $report, Request $expected): void {
63+
$this->assertEquals($expected, $report->getRequest());
64+
}
65+
66+
public function generateReportsWithRequests(): array {
67+
$r1 = new Request('POST', 'https://www.example.com');
68+
$r2 = new Request('PUT', 'https://m.example.com');
69+
$r3 = new Request('GET', 'https://test.net');
70+
71+
return [
72+
[new MessageSentReport($r1), $r1],
73+
[new MessageSentReport($r2), $r2],
74+
[new MessageSentReport($r3), $r3],
75+
];
76+
}
77+
78+
/**
79+
* @param MessageSentReport $report
80+
* @param string $json
81+
* @dataProvider generateReportsWithJson
82+
*/
83+
public function testJsonSerialize(MessageSentReport $report, string $json): void {
84+
$this->assertJsonStringEqualsJsonString($json, json_encode($report));
85+
}
86+
87+
public function generateReportsWithJson(): array {
88+
$request1Body = json_encode(['title' => 'test', 'body' => 'blah', 'data' => []]);
89+
$request1 = new Request('POST', 'https://www.example.com', [], $request1Body);
90+
$response1 = new Response(200, [], 'test');
91+
92+
$request2Body = '';
93+
$request2 = new Request('POST', 'https://www.example.com', [], $request2Body);
94+
$response2 = new Response(410, [], 'Faield to do somthing', '1.1', 'Gone');
95+
96+
return [
97+
[
98+
new MessageSentReport($request1, $response1),
99+
json_encode([
100+
'success' => true,
101+
'expired' => false,
102+
'reason' => 'OK',
103+
'endpoint' => (string) $request1->getUri(),
104+
'payload' => $request1Body,
105+
])
106+
],
107+
[
108+
new MessageSentReport($request2, $response2, false, 'Gone'),
109+
json_encode([
110+
'success' => false,
111+
'expired' => true,
112+
'reason' => 'Gone',
113+
'endpoint' => (string) $request2->getUri(),
114+
'payload' => $request2Body,
115+
])
116+
]
117+
];
118+
}
119+
120+
/**
121+
* @param MessageSentReport $report
122+
* @param bool $expected
123+
* @dataProvider generateReportsWithSuccess
124+
*/
125+
public function testIsSuccess(MessageSentReport $report, bool $expected): void {
126+
$this->assertEquals($expected, $report->isSuccess());
127+
}
128+
129+
/**
130+
* @return array
131+
*/
132+
public function generateReportsWithSuccess(): array {
133+
return [
134+
[new MessageSentReport(), true],
135+
[new MessageSentReport(null, null, true), true],
136+
[new MessageSentReport(null, null, false), false],
137+
];
138+
}
139+
}

tests/WebPushTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ public function testFlush() {
182182
}
183183
}
184184

185+
public function testFlushEmpty(): void {
186+
$this->webPush->flush(300);
187+
}
188+
189+
/**
190+
* @throws ErrorException
191+
*/
192+
public function testCount(): void {
193+
$subscription = new Subscription(self::$endpoints['standard']);
194+
195+
$this->webPush->sendNotification($subscription);
196+
$this->webPush->sendNotification($subscription);
197+
$this->webPush->sendNotification($subscription);
198+
$this->webPush->sendNotification($subscription);
199+
200+
$this->assertCount(4, $this->webPush);
201+
}
202+
185203
/**
186204
* @throws ErrorException
187205
*/

0 commit comments

Comments
 (0)