NotificationTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
c 2
b 0
f 0
dl 0
loc 89
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testToArray() 0 10 1
A testCreate() 0 3 1
A testAddHeading() 0 9 1
A testAddData() 0 16 1
A testAddContent() 0 9 1
A testUnsupportedHeadingLocale() 0 9 1
A testUnsupportedContentLocale() 0 9 1
A testForSegment() 0 7 1
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