Completed
Push — master ( 020c5c...9dbce5 )
by Julián
08:49
created

GcmAdapterTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
c 4
b 0
f 0
lcom 1
cbo 4
dl 0
loc 72
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 1
A testInvalidApiKey() 0 4 1
A testSend() 0 12 1
B testExceptionErrorCode() 0 24 1
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify\Tests\Adapter\Gcm;
11
12
use Jgut\Tify\Adapter\Gcm\GcmAdapter;
13
use Jgut\Tify\Adapter\Gcm\GcmBuilder;
14
use Jgut\Tify\Adapter\Gcm\GcmMessage;
15
use Jgut\Tify\Message;
16
use Jgut\Tify\Notification;
17
use Jgut\Tify\Receiver\GcmReceiver;
18
use ZendService\Google\Exception\RuntimeException;
19
use ZendService\Google\Gcm\Client;
20
use ZendService\Google\Gcm\Response;
21
22
/**
23
 * Gcm adapter tests.
24
 */
25
class GcmAdapterTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var \Jgut\Tify\Adapter\Gcm\GcmAdapter
29
     */
30
    protected $adapter;
31
32
    public function setUp()
33
    {
34
        $message = $this->getMock(GcmMessage::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
35
        $message->expects(self::any())->method('getRegistrationIds')->will(self::returnValue(['aaa', 'bbb']));
36
37
        $response = $this->getMock(Response::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
38
        $response->expects(self::any())->method('getResults')
39
            ->will(self::returnValue(['aaa' => [], 'bbb' => ['error' => 'NotRegistered']]));
40
41
        $client = $this->getMock(Client::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
42
        $client->expects(self::any())->method('send')->will(self::returnValue($response));
43
44
        $builder = $this->getMock(GcmBuilder::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
45
        $builder->expects(self::any())->method('buildPushClient')->will(self::returnValue($client));
46
        $builder->expects(self::any())->method('buildPushMessage')->will(self::returnValue($message));
47
48
        $this->adapter = new GcmAdapter(['api_key' => 'aaa'], $builder);
49
    }
50
51
    /**
52
     * @expectedException \Jgut\Tify\Exception\AdapterException
53
     */
54
    public function testInvalidApiKey()
55
    {
56
        new GcmAdapter();
57
    }
58
59
    public function testSend()
60
    {
61
        $message = $this->getMock(Message::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
63
        $receiver = $this->getMock(GcmReceiver::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
64
        $receiver->expects(self::any())->method('getToken')->will(self::returnValue('aaa'));
65
66
        $notification = new Notification($message, [$receiver]);
67
        $results = $this->adapter->push($notification);
68
69
        self::assertCount(2, $results);
70
    }
71
72
    public function testExceptionErrorCode()
73
    {
74
        $reflection = new \ReflectionClass(get_class($this->adapter));
75
        $method = $reflection->getMethod('getErrorCodeFromException');
76
        $method->setAccessible(true);
77
78
        $exception = new RuntimeException('500 Internal Server Error');
79
        self::assertEquals(GcmAdapter::RESULT_INTERNAL_SERVER_ERROR, $method->invoke($this->adapter, $exception));
80
81
        $exception = new RuntimeException('503 Server Unavailable; Retry-After 200');
82
        self::assertEquals(GcmAdapter::RESULT_SERVER_UNAVAILABLE, $method->invoke($this->adapter, $exception));
83
84
        $exception = new RuntimeException('401 Forbidden; Authentication Error');
85
        self::assertEquals(GcmAdapter::RESULT_AUTHENTICATION_ERROR, $method->invoke($this->adapter, $exception));
86
87
        $exception = new RuntimeException('400 Bad Request; invalid message');
88
        self::assertEquals(GcmAdapter::RESULT_INVALID_MESSAGE, $method->invoke($this->adapter, $exception));
89
90
        $exception = new RuntimeException('Response body did not contain a valid JSON response');
91
        self::assertEquals(GcmAdapter::RESULT_BAD_FORMATTED_RESPONSE, $method->invoke($this->adapter, $exception));
92
93
        $exception = new RuntimeException('Unknown');
94
        self::assertEquals(GcmAdapter::RESULT_UNKNOWN, $method->invoke($this->adapter, $exception));
95
    }
96
}
97