Completed
Branch master (431168)
by Gennady
09:40
created

MessageTest::testSetCustomDataValidObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Apns;
4
5
class MessageTest extends \PHPUnit_Framework_TestCase
6
{
7
    public function testCreation()
8
    {
9
        $msg = new Message();
10
        $this->assertEquals(['aps' => []], $this->serialized($msg));
11
        $this->assertNull($msg->getDeviceIdentifier());
12
    }
13
14
    public function testCreationWithDeviceIdentifier()
15
    {
16
        $msg = new Message('foo');
17
        $this->assertSame('foo', $msg->getDeviceIdentifier());
18
    }
19
20
    public function testSetId()
21
    {
22
        $msg = new Message();
23
        $msg->setId('foo');
24
        $this->assertEquals('foo', $msg->getId());
25
        $this->assertEquals(['apns-id' => 'foo'], $msg->getMessageHeaders());
26
    }
27
28
    public function testSetExpiration()
29
    {
30
        $msg = new Message();
31
        $msg->setExpiry(999);
32
        $this->assertEquals(999, $msg->getExpiry());
33
        $this->assertEquals(['apns-expiration' => 999], $msg->getMessageHeaders());
34
    }
35
36
    public function testSetTopic()
37
    {
38
        $msg = new Message();
39
        $msg->setTopic('foo');
40
        $this->assertEquals('foo', $msg->getTopic());
41
        $this->assertEquals(['apns-topic' => 'foo'], $msg->getMessageHeaders());
42
    }
43
44
    public function testSetPriority()
45
    {
46
        $msg = new Message();
47
        $msg->setPriority(10);
48
        $this->assertEquals(10, $msg->getPriority());
49
        $this->assertEquals(['apns-priority' => 10], $msg->getMessageHeaders());
50
    }
51
52
    public function testSetAPSSound()
53
    {
54
        $msg = new Message();
55
        $msg->setAPSSound('foo');
56
        $this->assertEquals(['aps' => ['sound' => 'foo']], $this->serialized($msg));
57
    }
58
59
    public function testSetAPSBadge()
60
    {
61
        $msg = new Message();
62
        $msg->setAPSBadge(999);
63
        $this->assertEquals(['aps' => ['badge' => 999]], $this->serialized($msg));
64
    }
65
66
    public function testSetAPSContentAvailable()
67
    {
68
        $msg = new Message();
69
        $msg->setAPSContentAvailable(1);
70
        $this->assertEquals(['aps' => ['content-available' => 1]], $this->serialized($msg));
71
72
        $msg->setAPSContentAvailable();
73
        $this->assertEquals(['aps' => []], $this->serialized($msg));
74
    }
75
76
    public function testSetCategory()
77
    {
78
        $msg = new Message();
79
        $msg->setAPSCategory('foo');
80
81
        $this->assertEquals(['aps' => ['category' => 'foo']], $this->serialized($msg));
82
    }
83
84
    public function testSetAlert()
85
    {
86
        $msg = new Message();
87
        $msg->setAlert('foo');
88
        $this->assertEquals(['aps' => ['alert' => 'foo']], $this->serialized($msg));
89
    }
90
91
    public function testSetCustomAlert()
92
    {
93
        $msg = new Message();
94
        $msg->setAlert(
95
            (new MessageAlert())
96
                ->setBody('foo')
97
        );
98
99
        $this->assertEquals(['aps' => ['alert' => ['body' => 'foo']]], $this->serialized($msg));
100
    }
101
102
    /**
103
     * @expectedException \Exception
104
     */
105
    public function testSetInvalidAlert()
106
    {
107
        $msg = new Message();
108
        $msg->setAlert(new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a object<Apns\MessageAlert>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
    }
110
111
    public function testSetData()
112
    {
113
        $msg = new Message();
114
        $msg->setData(['aps' => 'foo', 'foo' => 'bar']);
115
116
        $this->assertEquals(['aps' => [], 'foo' => 'bar'], $this->serialized($msg));
117
    }
118
119
    public function testSetCustomData()
120
    {
121
        $msg = new Message();
122
        $msg->addCustomData('foo', 'bar');
123
124
        $this->assertEquals(['aps' => [], 'foo' => 'bar'], $this->serialized($msg));
125
    }
126
127
    /**
128
     * @expectedException  \Exception
129
     */
130
    public function testSetCustomDataInvalidKey()
131
    {
132
        $msg = new Message();
133
        $msg->addCustomData('aps', 'foo');
134
    }
135
136
    /**
137
     * @expectedException  \Exception
138
     * @expectedExceptionMessage DateTime
139
     */
140
    public function testSetCustomDataInvalidObject()
141
    {
142
        $msg = new Message();
143
        $msg->addCustomData('foo', new \DateTime);
144
    }
145
146
    public function testSetCustomDataValidObject()
147
    {
148
        $msg = new Message();
149
        $msg->addCustomData('foo', new \stdClass());
150
151
        $this->assertEquals(['aps' => [], 'foo' => []], $this->serialized($msg));
152
    }
153
154
    /**
155
     * @param Message $msg
156
     * @return array
157
     */
158
    private function serialized(Message $msg)
159
    {
160
        return json_decode(json_encode($msg), true);
161
    }
162
}