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

Commit ffb1da3

Browse files
committed
Fix tests
1 parent d5348bd commit ffb1da3

16 files changed

+160
-28
lines changed

codeception.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace: machour\yii2\wpn\tests
1+
namespace: tests
22
bootstrap: _bootstrap.php
33
suites:
44
unit:
@@ -20,5 +20,5 @@ paths:
2020
tests: tests
2121
output: tests/_output
2222
support: tests/_support
23-
data: tests/_data
23+
data: tests/fixtures/data
2424

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"codeception/codeception": "^4.1",
2525
"codeception/module-yii2": "^1.1",
2626
"codeception/module-phpbrowser": "^1.0.0",
27-
"codeception/module-asserts": "^1.0.0"
27+
"codeception/module-asserts": "^1.0.0",
28+
"codeception/assert-throws": "^1.1"
2829
},
2930
"autoload": {
3031
"psr-4": {
@@ -33,7 +34,7 @@
3334
},
3435
"autoload-dev": {
3536
"psr-4": {
36-
"machour\\yii2\\wpn\\tests\\": "tests"
37+
"tests\\": "tests"
3738
}
3839
},
3940
"repositories": [

src/helpers/WebPushNotifications.php renamed to src/components/Pusher.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
<?php
22

3-
namespace machour\yii2\wpn\helpers;
3+
namespace machour\yii2\wpn\components;
44

5+
use machour\yii2\wpn\exceptions\InvalidApplication;
56
use machour\yii2\wpn\models\WpnCampaign;
67
use machour\yii2\wpn\models\WpnSubscription;
78
use Minishlink\WebPush\WebPush;
89
use Yii;
10+
use yii\base\BaseObject;
911
use yii\helpers\ArrayHelper;
1012
use yii\helpers\Json;
1113

1214
/**
1315
* @see https://autopush.readthedocs.io/en/latest/http.html#error-codes
1416
*/
15-
class WebPushNotifications
17+
class Pusher extends BaseObject
1618
{
19+
private $wp;
20+
21+
public function __construct(WebPush $wp, $config = [])
22+
{
23+
$this->wp = $wp;
24+
parent::__construct($config);
25+
}
26+
27+
public function test()
28+
{
29+
return $this->wp;
30+
}
31+
1732
/**
1833
* @throws \yii\db\Exception
1934
* @throws \ErrorException
2035
*/
21-
public static function sendPush(WpnCampaign $campaign)
36+
public function sendPush(WpnCampaign $campaign)
2237
{
38+
if (!$campaign->app->enabled) {
39+
throw new InvalidApplication();
40+
}
41+
2342
$auth = [
2443
'VAPID' => [
2544
'subject' => $campaign->app->subject,
@@ -28,16 +47,14 @@ public static function sendPush(WpnCampaign $campaign)
2847
],
2948
];
3049

31-
$webPush = new WebPush($auth);
32-
3350
$payload = Json::encode($campaign->options);
3451

3552
/** @var WpnSubscription[] $subscriptions */
3653
$subscriptions = ArrayHelper::map(WpnSubscription::find()->where(['subscribed' => true])->all(), 'endpoint', 'self');
3754

3855
foreach ($subscriptions as $subscription) {
3956
try {
40-
$webPush->queueNotification($subscription, $payload);
57+
$this->wp->queueNotification($subscription, $payload, [], $auth);
4158
} catch (\Exception $e) {
4259
$subscription->last_error = $e->getMessage();
4360
$subscription->save();
@@ -48,7 +65,7 @@ public static function sendPush(WpnCampaign $campaign)
4865

4966
$reports = [];
5067

51-
foreach ($webPush->flush() as $report) {
68+
foreach ($this->wp->flush() as $report) {
5269
$endpoint = $report->getRequest()->getUri()->__toString();
5370
$subscription = $subscriptions[$endpoint];
5471
$spParams = [

src/controllers/DefaultController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace machour\yii2\wpn\controllers;
44

55
use machour\yii2\wpn\exceptions\SubscriptionNotFound;
6-
use machour\yii2\wpn\helpers\WebPushNotifications;
6+
use machour\yii2\wpn\helpers\Pusher;
77
use machour\yii2\wpn\models\WpnCampaign;
88
use machour\yii2\wpn\models\WpnSubscription;
99
use machour\yii2\wpn\models\WpnReport;
@@ -156,6 +156,6 @@ public function actionServiceWorker(): string
156156

157157
private function actionPush($id)
158158
{
159-
WebPushNotifications::sendPush(WpnCampaign::findOne($id));
159+
Pusher::sendPush(WpnCampaign::findOne($id));
160160
}
161161
}

src/exceptions/InvalidApplication.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace machour\yii2\wpn\exceptions;
4+
5+
use yii\base\Exception;
6+
7+
class InvalidApplication extends Exception
8+
{
9+
public $message = 'Application disabled';
10+
}

tests/MyTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
11
<?php
2-
namespace machour\yii2\wpn\tests;
32

4-
use machour\yii2\wpn\tests\fixtures\WpnAppFixture;
3+
namespace tests;
4+
5+
use machour\yii2\wpn\exceptions\InvalidApplication;
6+
use machour\yii2\wpn\Module;
7+
use Minishlink\WebPush\WebPush;
8+
use tests\fixtures\WpnAppFixture;
9+
use tests\fixtures\WpnCampaignFixture;
510

611
class MyTest extends \Codeception\Test\Unit
712
{
13+
use \Codeception\AssertThrows;
14+
815
/**
9-
* @var \machour\yii2\wpn\tests\UnitTester
16+
* @var UnitTester
1017
*/
1118
protected $tester;
1219

20+
protected $pusher;
21+
1322
protected function _before()
1423
{
1524
$this->tester->haveFixtures([
16-
'user' => [
25+
'app' => [
1726
'class' => WpnAppFixture::class,
18-
'dataFile' => codecept_data_dir() . 'wpn_app.php'
19-
]
27+
'dataFile' => codecept_data_dir() . 'wpn_app.php',
28+
],
29+
'campaign' => [
30+
'class' => WpnCampaignFixture::class,
31+
],
2032
]);
33+
34+
$module = Module::getInstance();
35+
$this->pusher = $module->get('pusher');
36+
2137
}
2238

2339
protected function _after()
2440
{
41+
2542
}
2643

2744
// tests
28-
public function testSomeFeature()
45+
public function testRefusingDisabledApplication()
2946
{
30-
47+
$invalidApplication = $this->tester->grabFixture('campaign', 1);
48+
$this->assertThrows(InvalidApplication::class, function() use ($invalidApplication) {
49+
$this->pusher->sendPush($invalidApplication);
50+
});
3151
}
3252
}

tests/_bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
define('YII_ENV', 'test');
34
defined('YII_DEBUG') or define('YII_DEBUG', true);
45

tests/_support/Helper/Unit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace machour\yii2\wpn\tests\Helper;
2+
namespace tests\Helper;
33

44
// here you can define custom actions
55
// all public methods declared in helper class will be available in $I

tests/_support/UnitTester.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace machour\yii2\wpn\tests;
2+
namespace tests;
33

44
/**
55
* Inherited Methods

tests/_support/_generated/UnitTesterActions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php //[STAMP] 194c562054d7b0475f5c9c66c98d6bd8
2-
namespace machour\yii2\wpn\tests\_generated;
2+
namespace tests\_generated;
33

44
// This class was automatically generated by build task
55
// You should not change it manually as it will be overwritten on next build

tests/config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
return [
66
'id' => 'wpn-tests',
77
'basePath' => dirname(__DIR__),
8+
'bootstrap' => ['wpn'],
89
'components' => [
910
'db' => [
1011
'class' => Connection::class,
@@ -14,5 +15,15 @@
1415
'enableSchemaCache' => true,
1516
'charset' => 'latin1',
1617
],
18+
],
19+
'modules' => [
20+
'wpn' => [
21+
'class' => \machour\yii2\wpn\Module::class,
22+
'components' => [
23+
'pusher' => [
24+
'class' => \machour\yii2\wpn\components\Pusher::class,
25+
]
26+
]
27+
]
1728
]
1829
];

tests/fixtures/WpnAppFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace machour\yii2\wpn\tests\fixtures;
3+
namespace tests\fixtures;
44

55
use machour\yii2\wpn\models\WpnApp;
66
use yii\test\ActiveFixture;

tests/fixtures/WpnCampaignFixture.php

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\WpnCampaign;
6+
use yii\test\ActiveFixture;
7+
8+
class WpnCampaignFixture extends ActiveFixture
9+
{
10+
public $modelClass = WpnCampaign::class;
11+
}

tests/fixtures/data/wpn_app.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@
33
return [
44
[
55
'id' => 1,
6-
'name' => 'Test App',
6+
'name' => 'Valid App',
77
'host' => 'localhost',
8-
'subject' => 'mailto:example@localhost.com'
9-
]
8+
'subject' => 'mailto:example@localhost.com',
9+
'public_key' => 'BPNOgWF76BVzSYpRWDWV3SVc9wvZgfvImhhSQNLYGRs_Zfy5rEXb5NITVoUzbEoe9E85oO2830ftuZfwjmHr0FE',
10+
'private_key' => 'KRbzYHPtD54HPyXP6bkqavNUiTGKls8C0rLyAk7fCoU',
11+
'enabled' => true,
12+
'created_at' => date('Y-m-d H:i:s'),
13+
'updated_at' => date('Y-m-d H:i:s'),
14+
],
15+
[
16+
'id' => 2,
17+
'name' => 'Disabled App',
18+
'host' => 'localhost2',
19+
'subject' => 'mailto:example@localhost.com',
20+
'public_key' => 'BPNOgWF76BVzSYpRWDWV3SVc9wvZgfvImhhSQNLYGRs_Zfy5rEXb5NITVoUzbEoe9E85oO2830ftuZfwjmHr0FE',
21+
'private_key' => 'KRbzYHPtD54HPyXP6bkqavNUiTGKls8C0rLyAk7fCoU',
22+
'enabled' => false,
23+
'created_at' => date('Y-m-d H:i:s'),
24+
'updated_at' => date('Y-m-d H:i:s'),
25+
],
1026
];

tests/fixtures/data/wpn_campaign.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
return [
4+
[
5+
'id' => 1,
6+
'app_id' => 1,
7+
'title' => 'Push on valid application',
8+
'tag' => 'push-1',
9+
'body' => 'My push body',
10+
'created_at' => date('Y-m-d H:i:s'),
11+
'scheduled_at' => date('Y-m-d H:i:s'),
12+
'updated_at' => date('Y-m-d H:i:s'),
13+
],
14+
[
15+
'id' => 2,
16+
'app_id' => 2,
17+
'title' => 'Push on invalid application',
18+
'tag' => 'push-2',
19+
'body' => 'My push body',
20+
'created_at' => date('Y-m-d H:i:s'),
21+
'scheduled_at' => date('Y-m-d H:i:s'),
22+
'updated_at' => date('Y-m-d H:i:s'),
23+
],
24+
];

yii_test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Yii console bootstrap file.
5+
*
6+
* @link http://www.yiiframework.com/
7+
* @copyright Copyright (c) 2008 Yii Software LLC
8+
* @license http://www.yiiframework.com/license/
9+
*/
10+
11+
defined('YII_DEBUG') or define('YII_DEBUG', true);
12+
defined('YII_ENV') or define('YII_ENV', 'test');
13+
14+
require __DIR__ . '/vendor/autoload.php';
15+
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
16+
17+
$config = require __DIR__ . '/tests/config.php';
18+
19+
$application = new yii\console\Application($config);
20+
$exitCode = $application->run();
21+
exit($exitCode);

0 commit comments

Comments
 (0)