Skip to content

Commit 0316038

Browse files
authored
Chore tests (#363)
* Code style: `php-cs-fixer` * Migrate tests to php 8.0 * add type hints * optimize imports * Add `tests/` to `php-cs-fixer` test * Add script for cs fix * Fix typos * Change `json_encode` throw error
1 parent 1f58b27 commit 0316038

File tree

9 files changed

+208
-190
lines changed

9 files changed

+208
-190
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ jobs:
6565

6666
- name: Run php-cs-fixer
6767
if: ${{ matrix.php != '8.2' }} # Not supported yet.
68-
run: composer test:syntax
68+
run: |
69+
composer test:syntax
70+
composer test:syntax_tests

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
}
1414
],
1515
"scripts": {
16+
"fix:syntax": "./vendor/bin/php-cs-fixer fix ./src --using-cache=no",
17+
"fix:syntax_tests": "./vendor/bin/php-cs-fixer fix ./tests --using-cache=no",
1618
"test:unit": "./vendor/bin/phpunit --color",
1719
"test:typing": "./vendor/bin/phpstan analyse",
18-
"test:syntax": "./vendor/bin/php-cs-fixer fix ./src --dry-run --stop-on-violation --using-cache=no"
20+
"test:syntax": "./vendor/bin/php-cs-fixer fix ./src --dry-run --stop-on-violation --using-cache=no",
21+
"test:syntax_tests": "./vendor/bin/php-cs-fixer fix ./tests --dry-run --stop-on-violation --using-cache=no"
1922
},
2023
"require": {
2124
"php": ">=8.0",
@@ -39,8 +42,8 @@
3942
"friendsofphp/php-cs-fixer": "^v3.13.2"
4043
},
4144
"autoload": {
42-
"psr-4" : {
43-
"Minishlink\\WebPush\\" : "src"
45+
"psr-4": {
46+
"Minishlink\\WebPush\\": "src"
4447
}
4548
}
4649
}

tests/EncryptionTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313

1414
use Base64Url\Base64Url;
1515
use Jose\Component\Core\JWK;
16-
use Jose\Component\Core\Util\Ecc\NistCurve;
17-
use Jose\Component\Core\Util\Ecc\PrivateKey;
18-
use Jose\Component\KeyManagement\JWKFactory;
1916
use Minishlink\WebPush\Encryption;
2017
use Minishlink\WebPush\Utils;
2118

2219
final class EncryptionTest extends PHPUnit\Framework\TestCase
2320
{
24-
public function testDeterministicEncrypt()
21+
public function testDeterministicEncrypt(): void
2522
{
2623
$contentEncoding = "aes128gcm";
2724
$plaintext = 'When I grow up, I want to be a watermelon';
@@ -65,7 +62,7 @@ public function testDeterministicEncrypt()
6562
$this->assertEquals($expected, $result);
6663
}
6764

68-
public function testGetContentCodingHeader()
65+
public function testGetContentCodingHeader(): void
6966
{
7067
$localPublicKey = Base64Url::decode('BP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A8');
7168
$salt = Base64Url::decode('DGv6ra1nlYgDCS1FRnbzlw');
@@ -82,7 +79,7 @@ public function testGetContentCodingHeader()
8279
*
8380
* @throws ErrorException
8481
*/
85-
public function testPadPayload(string $payload, int $maxLengthToPad, int $expectedResLength)
82+
public function testPadPayload(string $payload, int $maxLengthToPad, int $expectedResLength): void
8683
{
8784
$res = Encryption::padPayload($payload, $maxLengthToPad, "aesgcm");
8885

tests/MessageSentReportTest.php

Lines changed: 108 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,118 +5,129 @@
55
*/
66

77
use GuzzleHttp\Psr7\Request;
8-
use \Minishlink\WebPush\MessageSentReport;
9-
use \GuzzleHttp\Psr7\Response;
8+
use GuzzleHttp\Psr7\Response;
9+
use Minishlink\WebPush\MessageSentReport;
10+
use PHPUnit\Framework\TestCase;
1011

1112
/**
1213
* @covers \Minishlink\WebPush\MessageSentReport
1314
*/
14-
class MessageSentReportTest extends \PHPUnit\Framework\TestCase {
15+
class MessageSentReportTest extends TestCase
16+
{
17+
/**
18+
* @dataProvider generateReportsWithExpiration
19+
*/
20+
public function testIsSubscriptionExpired(MessageSentReport $report, bool $expected): void
21+
{
22+
$this->assertEquals($expected, $report->isSubscriptionExpired());
23+
}
1524

16-
/**
17-
* @dataProvider generateReportsWithExpiration
18-
*/
19-
public function testIsSubscriptionExpired(MessageSentReport $report, bool $expected): void {
20-
$this->assertEquals($expected, $report->isSubscriptionExpired());
21-
}
22-
23-
public function generateReportsWithExpiration(): array {
24-
$request = new Request('POST', 'https://example.com');
25-
return [
26-
[new MessageSentReport($request, new Response(404)), true],
27-
[new MessageSentReport($request, new Response(410)), true],
28-
[new MessageSentReport($request, new Response(500)), false],
29-
[new MessageSentReport($request, new Response(200)), false]
30-
];
31-
}
25+
public function generateReportsWithExpiration(): array
26+
{
27+
$request = new Request('POST', 'https://example.com');
28+
return [
29+
[new MessageSentReport($request, new Response(404)), true],
30+
[new MessageSentReport($request, new Response(410)), true],
31+
[new MessageSentReport($request, new Response(500)), false],
32+
[new MessageSentReport($request, new Response(200)), false]
33+
];
34+
}
3235

33-
/**
34-
* @dataProvider generateReportsWithEndpoints
35-
*/
36-
public function testGetEndpoint(MessageSentReport $report, string $expected): void {
37-
$this->assertEquals($expected, $report->getEndpoint());
38-
}
36+
/**
37+
* @dataProvider generateReportsWithEndpoints
38+
*/
39+
public function testGetEndpoint(MessageSentReport $report, string $expected): void
40+
{
41+
$this->assertEquals($expected, $report->getEndpoint());
42+
}
3943

40-
public function generateReportsWithEndpoints(): array {
41-
return [
42-
[new MessageSentReport(new Request('POST', 'https://www.example.com')), 'https://www.example.com'],
43-
[new MessageSentReport(new Request('POST', 'https://m.example.com')), 'https://m.example.com'],
44-
[new MessageSentReport(new Request('POST', 'https://test.net')), 'https://test.net'],
45-
];
46-
}
44+
public function generateReportsWithEndpoints(): array
45+
{
46+
return [
47+
[new MessageSentReport(new Request('POST', 'https://www.example.com')), 'https://www.example.com'],
48+
[new MessageSentReport(new Request('POST', 'https://m.example.com')), 'https://m.example.com'],
49+
[new MessageSentReport(new Request('POST', 'https://test.net')), 'https://test.net'],
50+
];
51+
}
4752

48-
/**
49-
* @dataProvider generateReportsWithRequests
50-
*/
51-
public function testGetRequest(MessageSentReport $report, Request $expected): void {
52-
$this->assertEquals($expected, $report->getRequest());
53-
}
53+
/**
54+
* @dataProvider generateReportsWithRequests
55+
*/
56+
public function testGetRequest(MessageSentReport $report, Request $expected): void
57+
{
58+
$this->assertEquals($expected, $report->getRequest());
59+
}
5460

55-
public function generateReportsWithRequests(): array {
56-
$r1 = new Request('POST', 'https://www.example.com');
57-
$r2 = new Request('PUT', 'https://m.example.com');
58-
$r3 = new Request('GET', 'https://test.net');
61+
public function generateReportsWithRequests(): array
62+
{
63+
$r1 = new Request('POST', 'https://www.example.com');
64+
$r2 = new Request('PUT', 'https://m.example.com');
65+
$r3 = new Request('GET', 'https://test.net');
5966

60-
return [
61-
[new MessageSentReport($r1), $r1],
62-
[new MessageSentReport($r2), $r2],
63-
[new MessageSentReport($r3), $r3],
64-
];
65-
}
67+
return [
68+
[new MessageSentReport($r1), $r1],
69+
[new MessageSentReport($r2), $r2],
70+
[new MessageSentReport($r3), $r3],
71+
];
72+
}
6673

67-
/**
68-
* @dataProvider generateReportsWithJson
69-
*/
70-
public function testJsonSerialize(MessageSentReport $report, string $json): void {
71-
$this->assertJsonStringEqualsJsonString($json, json_encode($report, JSON_THROW_ON_ERROR));
72-
}
74+
/**
75+
* @dataProvider generateReportsWithJson
76+
*/
77+
public function testJsonSerialize(MessageSentReport $report, string $json): void
78+
{
79+
$this->assertJsonStringEqualsJsonString($json, json_encode($report, JSON_THROW_ON_ERROR));
80+
}
7381

74-
public function generateReportsWithJson(): array {
75-
$request1Body = json_encode(['title' => 'test', 'body' => 'blah', 'data' => []]);
76-
$request1 = new Request('POST', 'https://www.example.com', [], $request1Body);
77-
$response1 = new Response(200, [], 'test');
82+
public function generateReportsWithJson(): array
83+
{
84+
$request1Body = json_encode(['title' => 'test', 'body' => 'blah', 'data' => []], JSON_THROW_ON_ERROR);
85+
$request1 = new Request('POST', 'https://www.example.com', [], $request1Body);
86+
$response1 = new Response(200, [], 'test');
7887

79-
$request2Body = '';
80-
$request2 = new Request('POST', 'https://www.example.com', [], $request2Body);
81-
$response2 = new Response(410, [], 'Faield to do somthing', '1.1', 'Gone');
88+
$request2Body = '';
89+
$request2 = new Request('POST', 'https://www.example.com', [], $request2Body);
90+
$response2 = new Response(410, [], 'Failed to do something', '1.1', 'Gone');
8291

83-
return [
84-
[
85-
new MessageSentReport($request1, $response1),
86-
json_encode([
87-
'success' => true,
88-
'expired' => false,
89-
'reason' => 'OK',
90-
'endpoint' => (string) $request1->getUri(),
91-
'payload' => $request1Body,
92-
], JSON_THROW_ON_ERROR)
93-
],
94-
[
95-
new MessageSentReport($request2, $response2, false, 'Gone'),
96-
json_encode([
97-
'success' => false,
98-
'expired' => true,
99-
'reason' => 'Gone',
100-
'endpoint' => (string) $request2->getUri(),
101-
'payload' => $request2Body,
102-
], JSON_THROW_ON_ERROR)
103-
]
104-
];
105-
}
92+
return [
93+
[
94+
new MessageSentReport($request1, $response1),
95+
json_encode([
96+
'success' => true,
97+
'expired' => false,
98+
'reason' => 'OK',
99+
'endpoint' => (string) $request1->getUri(),
100+
'payload' => $request1Body,
101+
], JSON_THROW_ON_ERROR)
102+
],
103+
[
104+
new MessageSentReport($request2, $response2, false, 'Gone'),
105+
json_encode([
106+
'success' => false,
107+
'expired' => true,
108+
'reason' => 'Gone',
109+
'endpoint' => (string) $request2->getUri(),
110+
'payload' => $request2Body,
111+
], JSON_THROW_ON_ERROR)
112+
]
113+
];
114+
}
106115

107-
/**
108-
* @dataProvider generateReportsWithSuccess
109-
*/
110-
public function testIsSuccess(MessageSentReport $report, bool $expected): void {
111-
$this->assertEquals($expected, $report->isSuccess());
112-
}
116+
/**
117+
* @dataProvider generateReportsWithSuccess
118+
*/
119+
public function testIsSuccess(MessageSentReport $report, bool $expected): void
120+
{
121+
$this->assertEquals($expected, $report->isSuccess());
122+
}
113123

114-
public function generateReportsWithSuccess(): array {
124+
public function generateReportsWithSuccess(): array
125+
{
115126
$request = new Request('POST', 'https://example.com');
116-
return [
117-
[new MessageSentReport($request), true],
118-
[new MessageSentReport($request, null, true), true],
119-
[new MessageSentReport($request, null, false), false],
120-
];
121-
}
127+
return [
128+
[new MessageSentReport($request), true],
129+
[new MessageSentReport($request, null, true), true],
130+
[new MessageSentReport($request, null, false), false],
131+
];
132+
}
122133
}

0 commit comments

Comments
 (0)