FirebaseServiceTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testError500SendingNotification() 0 13 2
A testCreateMessageError() 0 6 1
A testError400SendingNotification() 0 13 2
A testCreateMessageSuccess() 0 7 1
A testError401SendingNotification() 0 13 2
A testErrorOtherSendingNotification() 0 13 2
A testSuccess200SendingNotification() 0 10 1
A testSendNotification() 0 8 1
A getFirebaseNotificationService() 0 3 1
1
<?php
2
3
namespace Ghoubre\FirebaseServiceBundle\Tests\Service;
4
5
use Ghoubre\FirebaseServiceBundle\Service\FirebaseNotificationService;
6
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
7
8
class FirebaseServiceTest extends TestCase
9
{
10
    public function testSendNotification()
11
    {
12
        $firebaseService = $this->getFirebaseNotificationService();
13
14
        $rightMessage = array("to" => "test");
15
        $firebaseService->createMessage($rightMessage);
16
17
        $this->assertEquals(true, array_key_exists("curlConf", $firebaseService->sendNotification()));
18
    }
19
20
    public function testCreateMessageError()
21
    {
22
        $firebaseService = $this->getFirebaseNotificationService();
23
24
        $wrongMessage = array("from" => "test");
25
        $this->assertEquals(false, $firebaseService->createMessage($wrongMessage));
26
    }
27
28
    public function testCreateMessageSuccess()
29
    {
30
        $firebaseService = $this->getFirebaseNotificationService();
31
32
        $rightMessage = array("to" => "test");
33
34
        $this->assertEquals(true, $firebaseService->createMessage($rightMessage));
35
    }
36
37
    public function testSuccess200SendingNotification()
38
    {
39
        $firebaseService = $this->getFirebaseNotificationService();
40
41
        $result = [
42
          "status" => 200,
43
          "response" => "OK"
44
        ];
45
46
        $this->assertEquals(json_decode("OK", true), $firebaseService->parseResult($result));
47
    }
48
49
    public function testError400SendingNotification()
50
    {
51
        $firebaseService = $this->getFirebaseNotificationService();
52
53
        $result = [
54
            "status" => 400,
55
            "response" => "OK"
56
        ];
57
58
        try {
59
            $firebaseService->parseResult($result);
60
        } catch (\Exception $exception) {
61
            $this->assertEquals("Invalid fields or request not parsed as JSON", $exception->getMessage());
62
        }
63
    }
64
65
    public function testError401SendingNotification()
66
    {
67
        $firebaseService = $this->getFirebaseNotificationService();
68
69
        $result = [
70
            "status" => 401,
71
            "response" => "OK"
72
        ];
73
74
        try {
75
            $firebaseService->parseResult($result);
76
        } catch (\Exception $exception) {
77
            $this->assertEquals("Authenticate Error", $exception->getMessage());
78
        }
79
    }
80
81
    public function testError500SendingNotification()
82
    {
83
        $firebaseService = $this->getFirebaseNotificationService();
84
85
        $result = [
86
            "status" => 500,
87
            "response" => "OK"
88
        ];
89
90
        try {
91
            $firebaseService->parseResult($result);
92
        } catch (\Exception $exception) {
93
            $this->assertEquals("Internal error in the FCM connection server", $exception->getMessage());
94
        }
95
    }
96
97
    public function testErrorOtherSendingNotification()
98
    {
99
        $firebaseService = $this->getFirebaseNotificationService();
100
101
        $result = [
102
            "status" => 0,
103
            "response" => "OK"
104
        ];
105
106
        try {
107
            $firebaseService->parseResult($result);
108
        } catch (\Exception $exception) {
109
            $this->assertEquals("Look status and check status in firebase.google.com", $exception->getMessage());
110
        }
111
    }
112
113
    private function getFirebaseNotificationService()
114
    {
115
        return new FirebaseNotificationService("server_key");
116
    }
117
}
118