Skip to content
This repository was archived by the owner on Mar 15, 2023. It is now read-only.

Commit 5db3ee5

Browse files
committed
wip on tests
1 parent 1099efd commit 5db3ee5

File tree

6 files changed

+187
-12
lines changed

6 files changed

+187
-12
lines changed

tests/MyTest.php renamed to tests/PusherTest.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace tests;
44

55
use machour\yii2\wpn\exceptions\InvalidApplication;
6+
use machour\yii2\wpn\models\WpnSubscription;
67
use machour\yii2\wpn\Module;
78
use Minishlink\WebPush\WebPush;
89
use tests\fixtures\WpnAppFixture;
910
use tests\fixtures\WpnCampaignFixture;
11+
use tests\fixtures\WpnSubscriptionFixture;
1012

11-
class MyTest extends \Codeception\Test\Unit
13+
class PusherTest extends \Codeception\Test\Unit
1214
{
1315
use \Codeception\AssertThrows;
1416

@@ -24,29 +26,45 @@ protected function _before()
2426
$this->tester->haveFixtures([
2527
'app' => [
2628
'class' => WpnAppFixture::class,
27-
'dataFile' => codecept_data_dir() . 'wpn_app.php',
2829
],
2930
'campaign' => [
3031
'class' => WpnCampaignFixture::class,
3132
],
33+
'subscription' => [
34+
'class' => WpnSubscriptionFixture::class,
35+
],
3236
]);
3337

34-
$module = Module::getInstance();
35-
$this->pusher = $module->get('pusher');
38+
$stub = $this->make(WebPush::class, [
39+
'queueNotification' => function() {
3640

37-
}
41+
},
42+
'flush' => function() {
43+
return [1];
44+
}
45+
]);
46+
\Yii::$container->set(WebPush::class, $stub);
3847

39-
protected function _after()
40-
{
48+
49+
$module = Module::getInstance();
50+
$this->pusher = $module->get('pusher');
4151

4252
}
4353

4454
// tests
4555
public function testRefusingDisabledApplication()
4656
{
47-
$invalidApplication = $this->tester->grabFixture('campaign', 1);
48-
$this->assertThrows(InvalidApplication::class, function() use ($invalidApplication) {
49-
$this->pusher->sendPush($invalidApplication);
57+
$campaign = $this->tester->grabFixture('campaign', 1);
58+
$this->assertThrows(InvalidApplication::class, function() use ($campaign) {
59+
$this->pusher->sendPush($campaign);
5060
});
5161
}
62+
63+
64+
public function testMock()
65+
{
66+
$campaign = $this->tester->grabFixture('campaign', 0);
67+
68+
$this->pusher->sendPush($campaign);
69+
}
5270
}

tests/WebPushMock.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace tests;
4+
5+
use GuzzleHttp\Exception\RequestException;
6+
use Minishlink\WebPush\MessageSentReport;
7+
use Minishlink\WebPush\Notification;
8+
use Minishlink\WebPush\SubscriptionInterface;
9+
use Minishlink\WebPush\WebPush;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
class WebPushMock extends WebPush
13+
{
14+
public $notifications;
15+
16+
public function queueNotification(SubscriptionInterface $subscription, ?string $payload = null, array $options = [], array $auth = []): void
17+
{
18+
$this->notifications[] = new Notification($subscription, $payload, $options, $auth);
19+
}
20+
21+
public function flush(?int $batchSize = null): \Generator
22+
{
23+
if (null === $this->notifications || empty($this->notifications)) {
24+
yield from [];
25+
return;
26+
}
27+
28+
if (null === $batchSize) {
29+
$batchSize = $this->defaultOptions['batchSize'];
30+
}
31+
32+
$batches = array_chunk($this->notifications, $batchSize);
33+
34+
// reset queue
35+
$this->notifications = [];
36+
37+
foreach ($batches as $batch) {
38+
// for each endpoint server type
39+
$requests = $this->prepare($batch);
40+
41+
$promises = [];
42+
43+
foreach ($requests as $request) {
44+
$promises[] = $this->client->sendAsync($request)
45+
->then(function ($response) use ($request) {
46+
/** @var ResponseInterface $response * */
47+
return new MessageSentReport($request, $response);
48+
})
49+
->otherwise(function ($reason) {
50+
/** @var RequestException $reason **/
51+
if (method_exists($reason, 'getResponse')) {
52+
$response = $reason->getResponse();
53+
} else {
54+
$response = null;
55+
}
56+
return new MessageSentReport($reason->getRequest(), $response, false, $reason->getMessage());
57+
});
58+
}
59+
60+
foreach ($promises as $promise) {
61+
yield $promise->wait();
62+
}
63+
}
64+
65+
if ($this->reuseVAPIDHeaders) {
66+
$this->vapidHeaders = [];
67+
}
68+
}
69+
70+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace tests\fixtures;
4+
5+
use machour\yii2\wpn\models\WpnSubscription;
6+
use yii\test\ActiveFixture;
7+
8+
class WpnSubscriptionFixture extends ActiveFixture
9+
{
10+
public $modelClass = WpnSubscription::class;
11+
}

tests/fixtures/data/wpn_app.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'id' => 1,
66
'name' => 'Valid App',
77
'host' => 'localhost',
8-
'subject' => 'mailto:example@localhost.com',
8+
'subject' => 'mailto:contact@example.com',
99
'public_key' => 'BPNOgWF76BVzSYpRWDWV3SVc9wvZgfvImhhSQNLYGRs_Zfy5rEXb5NITVoUzbEoe9E85oO2830ftuZfwjmHr0FE',
1010
'private_key' => 'KRbzYHPtD54HPyXP6bkqavNUiTGKls8C0rLyAk7fCoU',
1111
'enabled' => true,
@@ -16,11 +16,23 @@
1616
'id' => 2,
1717
'name' => 'Disabled App',
1818
'host' => 'localhost2',
19-
'subject' => 'mailto:example@localhost.com',
19+
'subject' => 'mailto:contact@example.com',
2020
'public_key' => 'BPNOgWF76BVzSYpRWDWV3SVc9wvZgfvImhhSQNLYGRs_Zfy5rEXb5NITVoUzbEoe9E85oO2830ftuZfwjmHr0FE',
2121
'private_key' => 'KRbzYHPtD54HPyXP6bkqavNUiTGKls8C0rLyAk7fCoU',
2222
'enabled' => false,
2323
'created_at' => date('Y-m-d H:i:s'),
2424
'updated_at' => date('Y-m-d H:i:s'),
2525
],
26+
[
27+
'id' => 3,
28+
'name' => 'Valid App with icon',
29+
'host' => 'localhost3',
30+
'subject' => 'mailto:contact@example.com',
31+
'public_key' => 'BPNOgWF76BVzSYpRWDWV3SVc9wvZgfvImhhSQNLYGRs_Zfy5rEXb5NITVoUzbEoe9E85oO2830ftuZfwjmHr0FE',
32+
'private_key' => 'KRbzYHPtD54HPyXP6bkqavNUiTGKls8C0rLyAk7fCoU',
33+
'icon' => 'https://www.yiiframework.com/image/design/logo/yii3_sign.png',
34+
'enabled' => false,
35+
'created_at' => date('Y-m-d H:i:s'),
36+
'updated_at' => date('Y-m-d H:i:s'),
37+
],
2638
];

tests/fixtures/data/wpn_campaign.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,24 @@
2121
'scheduled_at' => date('Y-m-d H:i:s'),
2222
'updated_at' => date('Y-m-d H:i:s'),
2323
],
24+
[
25+
'id' => 3,
26+
'app_id' => 3,
27+
'title' => 'Push on application with icon',
28+
'tag' => 'push-3',
29+
'body' => 'My push body',
30+
'created_at' => date('Y-m-d H:i:s'),
31+
'scheduled_at' => date('Y-m-d H:i:s'),
32+
'updated_at' => date('Y-m-d H:i:s'),
33+
],
34+
[
35+
'id' => 4,
36+
'app_id' => 1,
37+
'title' => 'Push for test users only',
38+
'tag' => 'push-test',
39+
'body' => 'My test push body',
40+
'created_at' => date('Y-m-d H:i:s'),
41+
'scheduled_at' => date('Y-m-d H:i:s'),
42+
'updated_at' => date('Y-m-d H:i:s'),
43+
],
2444
];
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
return [
4+
[
5+
'id' => 1,
6+
'endpoint' => 'endpoint-subscribed-1',
7+
'auth' => 'auth-1',
8+
'public_key' => 'public-1',
9+
'content_encoding' => 'encoding-1',
10+
'subscribed' => 1,
11+
'test_user' => 0,
12+
'app_id' => 1,
13+
'ip' => '127.0.0.1',
14+
'created_at' => date('Y-m-d H:i:s'),
15+
'updated_at' => date('Y-m-d H:i:s'),
16+
],
17+
[
18+
'id' => 2,
19+
'endpoint' => 'endpoint-subscribed-and-test-2',
20+
'auth' => 'auth-2',
21+
'public_key' => 'public-2',
22+
'content_encoding' => 'encoding-2',
23+
'subscribed' => 1,
24+
'test_user' => 1,
25+
'app_id' => 1,
26+
'ip' => '127.0.0.1',
27+
'created_at' => date('Y-m-d H:i:s'),
28+
'updated_at' => date('Y-m-d H:i:s'),
29+
],
30+
[
31+
'id' => 3,
32+
'endpoint' => 'endpoint-not-subscribed-3',
33+
'auth' => 'auth-3',
34+
'public_key' => 'public-3',
35+
'content_encoding' => 'encoding-3',
36+
'subscribed' => 0,
37+
'test_user' => 0,
38+
'app_id' => 1,
39+
'ip' => '127.0.0.1',
40+
'created_at' => date('Y-m-d H:i:s'),
41+
'updated_at' => date('Y-m-d H:i:s'),
42+
],
43+
44+
];

0 commit comments

Comments
 (0)