1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Level51\OneSignal; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
|
7
|
|
|
class NotificationTest extends SapphireTest |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function testCreate() |
11
|
|
|
{ |
12
|
|
|
$this->assertEquals(Notification::class, get_class(Notification::create())); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function testToArray() |
16
|
|
|
{ |
17
|
|
|
$notification = Notification::create(); |
18
|
|
|
|
19
|
|
|
$this->assertEquals([ |
20
|
|
|
'included_segments' => [Notification::DEFAULT_SEGMENT], |
21
|
|
|
'contents' => [], |
22
|
|
|
'headings' => [], |
23
|
|
|
'data' => [] |
24
|
|
|
], $notification->toArray()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testUnsupportedContentLocale() |
28
|
|
|
{ |
29
|
|
|
$locale = 'unsupportedlocale'; |
30
|
|
|
$content = 'Test Content 1'; |
31
|
|
|
|
32
|
|
|
$this->expectException(OneSignalException::class); |
33
|
|
|
$this->expectExceptionMessage("'$locale' is not a supported locale."); |
34
|
|
|
|
35
|
|
|
Notification::create()->addContent($locale, $content); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testUnsupportedHeadingLocale() |
39
|
|
|
{ |
40
|
|
|
$locale = 'unsupportedlocaleXYZ'; |
41
|
|
|
$content = 'Test Heading 1'; |
42
|
|
|
|
43
|
|
|
$this->expectException(OneSignalException::class); |
44
|
|
|
$this->expectExceptionMessage("'$locale' is not a supported locale."); |
45
|
|
|
|
46
|
|
|
Notification::create()->addHeading($locale, $content); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testAddContent() |
50
|
|
|
{ |
51
|
|
|
$locale = 'de'; |
52
|
|
|
$content = 'Test Content 1'; |
53
|
|
|
$payload = Notification::create()->addContent($locale, $content)->toArray(); |
54
|
|
|
|
55
|
|
|
$this->assertEquals([ |
56
|
|
|
$locale => $content |
57
|
|
|
], $payload['contents']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testAddHeading() |
61
|
|
|
{ |
62
|
|
|
$locale = 'de'; |
63
|
|
|
$subject = 'Test Heading 1'; |
64
|
|
|
$payload = Notification::create()->addHeading($locale, $subject)->toArray(); |
65
|
|
|
|
66
|
|
|
$this->assertEquals([ |
67
|
|
|
$locale => $subject |
68
|
|
|
], $payload['headings']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testAddData() |
72
|
|
|
{ |
73
|
|
|
$key1 = 'testkey1'; |
74
|
|
|
$val1 = 'Test Value 1'; |
75
|
|
|
$key2 = 'testkey2'; |
76
|
|
|
$val2 = ['testvalue2', 'testvalue3']; |
77
|
|
|
|
78
|
|
|
$payload = Notification::create() |
79
|
|
|
->addData($key1, $val1) |
80
|
|
|
->addData($key2, $val2) |
81
|
|
|
->toArray(); |
82
|
|
|
|
83
|
|
|
$this->assertEquals([ |
84
|
|
|
$key1 => $val1, |
85
|
|
|
$key2 => $val2 |
86
|
|
|
], $payload['data']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testForSegment() |
90
|
|
|
{ |
91
|
|
|
$testSegment = 'Test Segment'; |
92
|
|
|
$notification = Notification::create(); |
93
|
|
|
|
94
|
|
|
$this->assertEquals([Notification::DEFAULT_SEGMENT], $notification->toArray()['included_segments']); |
95
|
|
|
$this->assertEquals([$testSegment], $notification->forSegment($testSegment)->toArray()['included_segments']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|