Passed
Push — master ( c73d10...c1344b )
by Stefan
06:00
created

PNServerTest::setupValidServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
rs 9.7998
c 2
b 0
f 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Test\PNServer;
5
6
use PHPUnit\Framework\TestCase;
7
use SKien\PNServer\PNServer;
8
use SKien\PNServer\PNVapid;
9
use SKien\PNServer\PNPayload;
10
use SKien\PNServer\PNSubscription;
11
use SKien\PNServer\PNDataProviderMySQL;
12
use SKien\PNServer\PNDataProviderSQLite;
13
14
/**
15
 * Test of the main class.
16
 * Test works with SQLite dataprovider.
17
 * (check if it is necessary to test with MySQL dataprovider as well?)
18
 * 
19
 * @author Stefanius <[email protected]>
20
 * @copyright MIT License - see the LICENSE file for details
21
 */
22
class PNServerTest extends TestCase
23
{
24
    use TestHelperTrait;
25
    
26
    /**
27
     * Fixture for the test to work with SQLite dp.
28
     * - create temp directory for datafile if not exist 
29
     * - and ensure it is writeable
30
     * both will be done in self::getTempDataDir
31
     * - start without existing DB (delete DB file if exists so far)
32
     */
33
    public static function setUpBeforeClass() : void
34
    {
35
        self::getTempDataDir();
36
        self::deleteTempDataFile();
37
    }
38
    
39
    /**
40
     * remove created DB-file after last test
41
     */
42
    public static function tearDownAfterClass() : void
43
    {
44
        self::deleteTempDataFile();
45
    }
46
    
47
    public function test_setPayloadObject() : void
48
    {
49
        $srv = new PNServer();
50
        $pl = new PNPayload('title', 'text', 'icon.png');
51
        $srv->setPayload($pl);
52
        $this->assertNotEmpty($srv->getPayload());
53
    }
54
    
55
    public function test_setPayloadPlainText() : void
56
    {
57
        $srv = new PNServer();
58
        $srv->setPayload('plain text');
59
        $this->assertEquals('plain text', $srv->getPayload());
60
    }
61
    
62
    public function test_addSubscription() : void
63
    {
64
        $srv = new PNServer();
65
        $sub = new PNSubscription(VALID_ENDPOINT, VALID_P256H, VALID_AUTH);
66
        $srv->addSubscription($sub);
67
        $this->assertEquals(1, $srv->getSubscriptionCount());
68
    }
69
    
70
    public function test_loadSubscriptions() : void
71
    {
72
        $srv = $this->setupValidServer();
73
        $dp = $srv->getDP();
74
        // 5 subscriptions created: 'valid', 'notfound', 'expired', 'gone', 'invalid'
75
        $this->assertEquals(6, $dp->count());   
76
        // load subscriptions with autoremove true (default)
77
        $this->assertTrue($srv->loadSubscriptions());
78
        // 'expired' must not be loaded...
79
        $this->assertEquals(5, $srv->getSubscriptionCount());
80
        // ... and have to be removed from DB
81
        $this->assertEquals(5, $dp->count());
82
    }
83
    
84
    public function test_push() : PNServer
85
    {
86
        $srv = $this->setupValidServer();
87
        $dp = $srv->getDP();
88
        $srv->loadSubscriptions();
89
        $this->assertTrue($srv->push());
90
        // After notifications have been pushed, all entries except the valid one should have been removed from the database!
91
        $this->assertEquals(1, $dp->count());
92
        
93
        return $srv;
94
    }
95
    
96
    public function test_loadSubscriptionsWithError1() : void
97
    {
98
        // Test errorhandling if no dataprovider set
99
        $srv = new PNServer();
100
        $this->assertFalse($srv->loadSubscriptions());
101
        $this->assertNotEmpty($srv->getError());
102
    }
103
    
104
    public function test_loadSubscriptionsWithError2() : void
105
    {
106
        // Test errorhandling for not conected dataprovider
107
        $srv = new PNServer(new PNDataProviderMySQL('', '', '', ''));
108
        $this->assertFalse($srv->loadSubscriptions());
109
        $this->assertNotEmpty($srv->getError());
110
    }
111
    
112
    public function test_loadSubscriptionsNoAutoremove() : void
113
    {
114
        $srv = $this->setupValidServer();
115
        $dp = $srv->getDP();
116
        // 5 subscriptions created: 'valid', 'notfound', 'expired', 'gone', 'invalid'
117
        $this->assertEquals(6, $dp->count());
118
        $srv->setAutoRemove(false);
119
        $this->assertTrue($srv->loadSubscriptions());
120
        // 'expired' must not be loaded...
121
        $this->assertEquals(5, $srv->getSubscriptionCount());
122
        // ... but must NOT be removed from DB
123
        $this->assertEquals(6, $dp->count());
124
    }
125
    
126
    public function test_pushWithError() : void
127
    {
128
        $srv = new PNServer();
129
        // no vapid set
130
        $this->assertFalse($srv->push());
131
        $this->assertNotEmpty($srv->getError());
132
        // invalid vapid
133
        $srv->setVapid(new PNVapid(VALID_SUBJECT, '', VALID_PRIVATE_KEY));
134
        $this->assertFalse($srv->push());
135
        $this->assertNotEmpty($srv->getError());
136
        // no subscription(s) set
137
        $srv->setVapid(new PNVapid(VALID_SUBJECT, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY));
138
        $this->assertFalse($srv->push());
139
        $this->assertNotEmpty($srv->getError());
140
    }
141
    
142
    /**
143
     * @depends test_push
144
     */
145
    public function test_getLog(PNServer $srv)  : PNServer
146
    {
147
        $log = $srv->getLog();
148
        $this->assertIsArray($log);
149
        $this->assertEquals(5, count($log));
150
        $aResponse = [-1, 201, 404, 410, 0]; // Encryption error, OK, Not Found, Gone. inv. Endpoint
151
        $i = 0;
152
        foreach ($log as $strEndpoint => $aMsg) {
153
            $this->assertNotEmpty($strEndpoint);
154
            $this->assertEquals($aResponse[$i++], $aMsg['curl_response_code']);
155
            // fwrite(STDOUT, "\n" . $aMsg['msg'] . "\n");
156
        }
157
        return $srv;
158
    }
159
    
160
    /**
161
     * @depends test_getLog
162
     */
163
    public function test_getSummary(PNServer $srv)  : void
164
    {
165
        $summary = $srv->getSummary();
166
        $this->assertIsArray($summary);
167
        $this->assertArrayHasKey('total', $summary);
168
        $this->assertArrayHasKey('pushed', $summary);
169
        $this->assertArrayHasKey('failed', $summary);
170
        $this->assertArrayHasKey('expired', $summary);
171
        $this->assertArrayHasKey('removed', $summary);
172
        $this->assertEquals(6, $summary['total']);
173
        $this->assertEquals(1, $summary['pushed']);
174
        $this->assertEquals(4, $summary['failed']);
175
        $this->assertEquals(1, $summary['expired']);
176
        $this->assertEquals(5, $summary['removed']);
177
    }
178
    
179
    /**
180
     * NOT implemented in the setUp()-method because not all testmethods needs valid server
181
     * @return PNServer
182
     */
183
    protected function setupValidServer() : PNServer
184
    {
185
        $dp = new PNDataProviderSQLite(self::getTempDataDir(), self::$strSQLiteDBFilename);
186
        $dp->truncate();
187
        $dp->saveSubscription($this->loadSubscription('valid_subscription.json'));
188
        $dp->saveSubscription($this->loadSubscription('notfound_subscription.json'));
189
        $dp->saveSubscription($this->loadSubscription('expired_subscription.json'));
190
        $dp->saveSubscription($this->loadSubscription('gone_subscription.json'));
191
        $dp->saveSubscription($this->loadSubscription('invalid_subscription.json'));
192
        $dp->saveSubscription($this->loadSubscription('inv_endpoint_subscription.json'));
193
        
194
        $srv = new PNServer($dp);
195
        $vapid = new PNVapid(VALID_SUBJECT, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY);
196
        $srv->setVapid($vapid);
197
        $srv->setPayload('Greetings from phpUnit .-)');
198
        $this->assertIsObject($srv);
199
        
200
        return $srv;
201
    }
202
}
203
204