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

PNVapidTest::test_isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 28
rs 9.6
c 1
b 0
f 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Test\PNServer;
5
6
use PHPUnit\Framework\TestCase;
7
use SKien\PNServer\PNVapid;
8
9
/**
10
 * The constants VALID_SUBJECT, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY are defined
11
 * in the phpunit.xml configuration file.#
12
 *   
13
 * @author Stefanius <[email protected]>
14
 * @copyright MIT License - see the LICENSE file for details
15
 */
16
class PNVapidTest extends TestCase
17
{
18
    const COMPRESSED_PUBLIC_KEY = "ADtOCcUUTYvuUzx9ktgYs3mB6tQCjFLNfOkuiaIi_2LNosLbHQY6P91eMzQ8opTDLK_PjJHsjMSiJ-MUOeSjV8E";
19
    
20
    public function test_isValid() : void
21
    {
22
        $vapid = new PNVapid(VALID_SUBJECT, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY);
23
        $this->assertTrue($vapid->isValid());
24
25
        $vapid = new PNVapid('', VALID_PUBLIC_KEY . 'XX', VALID_PRIVATE_KEY);
26
        $this->assertFalse($vapid->isValid());
27
        $this->assertEquals(PNVapid::ERR_EMPTY_ARGUMENT, $vapid->getError());
28
        
29
        $vapid = new PNVapid(VALID_SUBJECT, '', VALID_PRIVATE_KEY);
30
        $this->assertFalse($vapid->isValid());
31
        $this->assertEquals(PNVapid::ERR_EMPTY_ARGUMENT, $vapid->getError());
32
        
33
        $vapid = new PNVapid(VALID_SUBJECT, VALID_PUBLIC_KEY, '');
34
        $this->assertFalse($vapid->isValid());
35
        $this->assertEquals(PNVapid::ERR_EMPTY_ARGUMENT, $vapid->getError());
36
        
37
        $vapid = new PNVapid(VALID_SUBJECT, VALID_PUBLIC_KEY . 'XX', VALID_PRIVATE_KEY);
38
        $this->assertFalse($vapid->isValid());
39
        $this->assertEquals(PNVapid::ERR_INVALID_PUBLIC_KEY_LENGTH, $vapid->getError());
40
        
41
        $vapid = new PNVapid(VALID_SUBJECT, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY . 'XX');
42
        $this->assertFalse($vapid->isValid());
43
        $this->assertEquals(PNVapid::ERR_INVALID_PRIVATE_KEY_LENGTH, $vapid->getError());
44
        
45
        $vapid = new PNVapid(VALID_SUBJECT, self::COMPRESSED_PUBLIC_KEY, VALID_PRIVATE_KEY);
46
        $this->assertFalse($vapid->isValid());
47
        $this->assertEquals(PNVapid::ERR_NO_COMPRESSED_KEY_SUPPORTED, $vapid->getError());
48
    }
49
    
50
    public function test_getHeaders() : void
51
    {
52
        $vapid = new PNVapid(VALID_SUBJECT, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY);
53
        $result = $vapid->getHeaders(VALID_ENDPOINT);
54
        $this->assertTrue(is_array($result));
55
        // content of the keys can not be tested - containing processed time() value
56
        $this->assertArrayHasKey('Authorization', $result);
57
        $this->assertArrayHasKey('Crypto-Key', $result);
58
        
59
        // cause an error 
60
        $vapid = new PNVapid(VALID_SUBJECT, '', VALID_PRIVATE_KEY);
61
        $this->expectError();
62
        $result = $vapid->getHeaders(VALID_ENDPOINT);
63
    }
64
}
65
66