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

PNPayloadTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 24
rs 9.584
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\PNPayload;
8
9
/**
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class PNPayloadTest extends TestCase
14
{
15
    protected PNPayload $pl;
16
    protected string $strExpected;
17
    
18
    public function setUp() : void
19
    {
20
        $this->pl = new PNPayload('title', 'text', 'icon.png');
21
        
22
        $this->strExpected  = '{';
23
        $this->strExpected .= '"title":"title",';
24
        $this->strExpected .= '"opt":{';
25
        $this->strExpected .= '"body":"text",';
26
        $this->strExpected .= '"icon":"icon.png",';
27
        $this->strExpected .= '"data":{"url":"url.php"},';
28
        $this->strExpected .= '"tag":"tag",';
29
        $this->strExpected .= '"renotify":true,';
30
        $this->strExpected .= '"image":"image.png",';
31
        $this->strExpected .= '"badge":"badge.png",';
32
        $this->strExpected .= '"actions":[';
33
        $this->strExpected .= '{"action":"ok","title":"OK","icon":"ok.png","custom":"customOK"},';
34
        $this->strExpected .= '{"action":"cancel","title":"Cancel","icon":"cancel.png","custom":"customCancel"}';
35
        $this->strExpected .= '],';
36
        $this->strExpected .= '"timestamp":"1588716000000",';
37
        $this->strExpected .= '"requireInteraction":true,';
38
        $this->strExpected .= '"silent":false,';
39
        $this->strExpected .= '"vibrate":[300,100,400],';
40
        $this->strExpected .= '"sound":"sound.mp3"';
41
        $this->strExpected .= '}}';
42
    }
43
    
44
    public function test_construct() : void
45
    {
46
        $data = $this->pl->getPayload();
47
        $this->assertTrue(is_array($data));
48
        $this->assertArrayHasKey('title', $data);
49
        $this->assertArrayHasKey('opt', $data);
50
        $this->assertTrue(is_array($data['opt']));
51
    }
52
    
53
    public function test_set() : void
54
    {
55
        $this->pl->setURL('url.php');
56
        $this->pl->setTag('tag', true);
57
        $this->pl->setImage('image.png');
58
        $this->pl->setBadge('badge.png');
59
        $this->pl->addAction('ok', 'OK', 'ok.png', 'customOK');
60
        $this->pl->addAction('cancel', 'Cancel', 'cancel.png', 'customCancel');
61
        $this->pl->setTimestamp(mktime(0, 0, 0, 5, 6, 2020));
62
        $this->pl->requireInteraction();
63
        // set to true - must be reseted to false by setVibration() !!!
64
        $this->pl->setSilent(true);
65
        $this->pl->setVibration([300, 100, 400]);
66
        $this->pl->setSound('sound.mp3');
67
        $this->assertEquals($this->strExpected, $this->pl->toJSON());
68
69
        // same value but other types
70
        $this->pl->setTimestamp('2020-05-06');
71
        $this->assertEquals($this->strExpected, $this->pl->toJSON());
72
        
73
        $this->pl->setTimestamp(new \DateTime('2020-05-06'));
74
        $this->assertEquals($this->strExpected, $this->pl->toJSON());
75
        
76
        // now the ['opt']['vibrate'] property must be reseted
77
        $this->pl->setSilent(true);
78
        $data = $this->pl->getPayload();
79
        $this->assertArrayNotHasKey('vibrate', $data['opt']);
80
    }
81
}
82