Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore tests #363

Merged
merged 6 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ jobs:

- name: Run php-cs-fixer
if: ${{ matrix.php != '8.2' }} # Not supported yet.
run: composer test:syntax
run: |
composer test:syntax
composer test:syntax_tests
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
}
],
"scripts": {
"fix:syntax": "./vendor/bin/php-cs-fixer fix ./src --using-cache=no",
"fix:syntax_tests": "./vendor/bin/php-cs-fixer fix ./tests --using-cache=no",
"test:unit": "./vendor/bin/phpunit --color",
"test:typing": "./vendor/bin/phpstan analyse",
"test:syntax": "./vendor/bin/php-cs-fixer fix ./src --dry-run --stop-on-violation --using-cache=no"
"test:syntax": "./vendor/bin/php-cs-fixer fix ./src --dry-run --stop-on-violation --using-cache=no",
"test:syntax_tests": "./vendor/bin/php-cs-fixer fix ./tests --dry-run --stop-on-violation --using-cache=no"
},
"require": {
"php": ">=8.0",
Expand All @@ -39,8 +42,8 @@
"friendsofphp/php-cs-fixer": "^v3.13.2"
},
"autoload": {
"psr-4" : {
"Minishlink\\WebPush\\" : "src"
"psr-4": {
"Minishlink\\WebPush\\": "src"
}
}
}
9 changes: 3 additions & 6 deletions tests/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@

use Base64Url\Base64Url;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Ecc\NistCurve;
use Jose\Component\Core\Util\Ecc\PrivateKey;
use Jose\Component\KeyManagement\JWKFactory;
use Minishlink\WebPush\Encryption;
use Minishlink\WebPush\Utils;

final class EncryptionTest extends PHPUnit\Framework\TestCase
{
public function testDeterministicEncrypt()
public function testDeterministicEncrypt(): void
{
$contentEncoding = "aes128gcm";
$plaintext = 'When I grow up, I want to be a watermelon';
Expand Down Expand Up @@ -65,7 +62,7 @@ public function testDeterministicEncrypt()
$this->assertEquals($expected, $result);
}

public function testGetContentCodingHeader()
public function testGetContentCodingHeader(): void
{
$localPublicKey = Base64Url::decode('BP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A8');
$salt = Base64Url::decode('DGv6ra1nlYgDCS1FRnbzlw');
Expand All @@ -82,7 +79,7 @@ public function testGetContentCodingHeader()
*
* @throws ErrorException
*/
public function testPadPayload(string $payload, int $maxLengthToPad, int $expectedResLength)
public function testPadPayload(string $payload, int $maxLengthToPad, int $expectedResLength): void
{
$res = Encryption::padPayload($payload, $maxLengthToPad, "aesgcm");

Expand Down
205 changes: 108 additions & 97 deletions tests/MessageSentReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,118 +5,129 @@
*/

use GuzzleHttp\Psr7\Request;
use \Minishlink\WebPush\MessageSentReport;
use \GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Response;
use Minishlink\WebPush\MessageSentReport;
use PHPUnit\Framework\TestCase;

/**
* @covers \Minishlink\WebPush\MessageSentReport
*/
class MessageSentReportTest extends \PHPUnit\Framework\TestCase {
class MessageSentReportTest extends TestCase
{
/**
* @dataProvider generateReportsWithExpiration
*/
public function testIsSubscriptionExpired(MessageSentReport $report, bool $expected): void
{
$this->assertEquals($expected, $report->isSubscriptionExpired());
}

/**
* @dataProvider generateReportsWithExpiration
*/
public function testIsSubscriptionExpired(MessageSentReport $report, bool $expected): void {
$this->assertEquals($expected, $report->isSubscriptionExpired());
}

public function generateReportsWithExpiration(): array {
$request = new Request('POST', 'https://example.com');
return [
[new MessageSentReport($request, new Response(404)), true],
[new MessageSentReport($request, new Response(410)), true],
[new MessageSentReport($request, new Response(500)), false],
[new MessageSentReport($request, new Response(200)), false]
];
}
public function generateReportsWithExpiration(): array
{
$request = new Request('POST', 'https://example.com');
return [
[new MessageSentReport($request, new Response(404)), true],
[new MessageSentReport($request, new Response(410)), true],
[new MessageSentReport($request, new Response(500)), false],
[new MessageSentReport($request, new Response(200)), false]
];
}

/**
* @dataProvider generateReportsWithEndpoints
*/
public function testGetEndpoint(MessageSentReport $report, string $expected): void {
$this->assertEquals($expected, $report->getEndpoint());
}
/**
* @dataProvider generateReportsWithEndpoints
*/
public function testGetEndpoint(MessageSentReport $report, string $expected): void
{
$this->assertEquals($expected, $report->getEndpoint());
}

public function generateReportsWithEndpoints(): array {
return [
[new MessageSentReport(new Request('POST', 'https://www.example.com')), 'https://www.example.com'],
[new MessageSentReport(new Request('POST', 'https://m.example.com')), 'https://m.example.com'],
[new MessageSentReport(new Request('POST', 'https://test.net')), 'https://test.net'],
];
}
public function generateReportsWithEndpoints(): array
{
return [
[new MessageSentReport(new Request('POST', 'https://www.example.com')), 'https://www.example.com'],
[new MessageSentReport(new Request('POST', 'https://m.example.com')), 'https://m.example.com'],
[new MessageSentReport(new Request('POST', 'https://test.net')), 'https://test.net'],
];
}

/**
* @dataProvider generateReportsWithRequests
*/
public function testGetRequest(MessageSentReport $report, Request $expected): void {
$this->assertEquals($expected, $report->getRequest());
}
/**
* @dataProvider generateReportsWithRequests
*/
public function testGetRequest(MessageSentReport $report, Request $expected): void
{
$this->assertEquals($expected, $report->getRequest());
}

public function generateReportsWithRequests(): array {
$r1 = new Request('POST', 'https://www.example.com');
$r2 = new Request('PUT', 'https://m.example.com');
$r3 = new Request('GET', 'https://test.net');
public function generateReportsWithRequests(): array
{
$r1 = new Request('POST', 'https://www.example.com');
$r2 = new Request('PUT', 'https://m.example.com');
$r3 = new Request('GET', 'https://test.net');

return [
[new MessageSentReport($r1), $r1],
[new MessageSentReport($r2), $r2],
[new MessageSentReport($r3), $r3],
];
}
return [
[new MessageSentReport($r1), $r1],
[new MessageSentReport($r2), $r2],
[new MessageSentReport($r3), $r3],
];
}

/**
* @dataProvider generateReportsWithJson
*/
public function testJsonSerialize(MessageSentReport $report, string $json): void {
$this->assertJsonStringEqualsJsonString($json, json_encode($report, JSON_THROW_ON_ERROR));
}
/**
* @dataProvider generateReportsWithJson
*/
public function testJsonSerialize(MessageSentReport $report, string $json): void
{
$this->assertJsonStringEqualsJsonString($json, json_encode($report, JSON_THROW_ON_ERROR));
}

public function generateReportsWithJson(): array {
$request1Body = json_encode(['title' => 'test', 'body' => 'blah', 'data' => []]);
$request1 = new Request('POST', 'https://www.example.com', [], $request1Body);
$response1 = new Response(200, [], 'test');
public function generateReportsWithJson(): array
{
$request1Body = json_encode(['title' => 'test', 'body' => 'blah', 'data' => []], JSON_THROW_ON_ERROR);
$request1 = new Request('POST', 'https://www.example.com', [], $request1Body);
$response1 = new Response(200, [], 'test');

$request2Body = '';
$request2 = new Request('POST', 'https://www.example.com', [], $request2Body);
$response2 = new Response(410, [], 'Faield to do somthing', '1.1', 'Gone');
$request2Body = '';
$request2 = new Request('POST', 'https://www.example.com', [], $request2Body);
$response2 = new Response(410, [], 'Failed to do something', '1.1', 'Gone');

return [
[
new MessageSentReport($request1, $response1),
json_encode([
'success' => true,
'expired' => false,
'reason' => 'OK',
'endpoint' => (string) $request1->getUri(),
'payload' => $request1Body,
], JSON_THROW_ON_ERROR)
],
[
new MessageSentReport($request2, $response2, false, 'Gone'),
json_encode([
'success' => false,
'expired' => true,
'reason' => 'Gone',
'endpoint' => (string) $request2->getUri(),
'payload' => $request2Body,
], JSON_THROW_ON_ERROR)
]
];
}
return [
[
new MessageSentReport($request1, $response1),
json_encode([
'success' => true,
'expired' => false,
'reason' => 'OK',
'endpoint' => (string) $request1->getUri(),
'payload' => $request1Body,
], JSON_THROW_ON_ERROR)
],
[
new MessageSentReport($request2, $response2, false, 'Gone'),
json_encode([
'success' => false,
'expired' => true,
'reason' => 'Gone',
'endpoint' => (string) $request2->getUri(),
'payload' => $request2Body,
], JSON_THROW_ON_ERROR)
]
];
}

/**
* @dataProvider generateReportsWithSuccess
*/
public function testIsSuccess(MessageSentReport $report, bool $expected): void {
$this->assertEquals($expected, $report->isSuccess());
}
/**
* @dataProvider generateReportsWithSuccess
*/
public function testIsSuccess(MessageSentReport $report, bool $expected): void
{
$this->assertEquals($expected, $report->isSuccess());
}

public function generateReportsWithSuccess(): array {
public function generateReportsWithSuccess(): array
{
$request = new Request('POST', 'https://example.com');
return [
[new MessageSentReport($request), true],
[new MessageSentReport($request, null, true), true],
[new MessageSentReport($request, null, false), false],
];
}
return [
[new MessageSentReport($request), true],
[new MessageSentReport($request, null, true), true],
[new MessageSentReport($request, null, false), false],
];
}
}
Loading