Completed
Push — master ( b2c31d...142309 )
by Vitaliy
01:49
created

HttpProtocolTest::shouldFailSend()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 31
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the AppleApnPush package
5
 *
6
 * (c) Vitaliy Zhuk <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Tests\Apple\ApnPush\Protocol;
13
14
use Apple\ApnPush\Encoder\PayloadEncoderInterface;
15
use Apple\ApnPush\Exception\SendNotification\SendNotificationException;
16
use Apple\ApnPush\Model\Alert;
17
use Apple\ApnPush\Model\Aps;
18
use Apple\ApnPush\Model\DeviceToken;
19
use Apple\ApnPush\Model\Notification;
20
use Apple\ApnPush\Model\Payload;
21
use Apple\ApnPush\Model\Receiver;
22
use Apple\ApnPush\Protocol\Http\Authenticator\AuthenticatorInterface;
23
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactoryInterface;
24
use Apple\ApnPush\Protocol\Http\Request;
25
use Apple\ApnPush\Protocol\Http\Response;
26
use Apple\ApnPush\Protocol\Http\Sender\HttpSenderInterface;
27
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactoryInterface;
28
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface;
29
use Apple\ApnPush\Protocol\HttpProtocol;
30
use PHPUnit\Framework\TestCase;
31
32
class HttpProtocolTest extends TestCase
33
{
34
    /**
35
     * @var AuthenticatorInterface|\PHPUnit_Framework_MockObject_MockObject
36
     */
37
    private $authenticator;
38
39
    /**
40
     * @var HttpSenderInterface|\PHPUnit_Framework_MockObject_MockObject
41
     */
42
    private $httpSender;
43
44
    /**
45
     * @var PayloadEncoderInterface|\PHPUnit_Framework_MockObject_MockObject
46
     */
47
    private $payloadEncoder;
48
49
    /**
50
     * @var UriFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
51
     */
52
    private $uriFactory;
53
54
    /**
55
     * @var HttpProtocolVisitorInterface|\PHPUnit_Framework_MockObject_MockObject
56
     */
57
    private $visitor;
58
59
    /**
60
     * @var ExceptionFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
61
     */
62
    private $exceptionFactory;
63
64
    /**
65
     * @var HttpProtocol
66
     */
67
    private $protocol;
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function setUp()
73
    {
74
        $this->authenticator = self::createMock(AuthenticatorInterface::class);
75
        $this->httpSender = self::createMock(HttpSenderInterface::class);
76
        $this->payloadEncoder = self::createMock(PayloadEncoderInterface::class);
77
        $this->uriFactory = self::createMock(UriFactoryInterface::class);
78
        $this->visitor = self::createMock(HttpProtocolVisitorInterface::class);
79
        $this->exceptionFactory = self::createMock(ExceptionFactoryInterface::class);
80
81
        $this->protocol = new HttpProtocol(
82
            $this->authenticator,
83
            $this->httpSender,
84
            $this->payloadEncoder,
85
            $this->uriFactory,
86
            $this->visitor,
87
            $this->exceptionFactory
88
        );
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function shouldSuccessSend()
95
    {
96
        $deviceToken = new DeviceToken(str_repeat('af', 32));
97
        $receiver = new Receiver($deviceToken, 'com.test');
98
        $payload = new Payload(new Aps(new Alert()));
99
        $notification = new Notification($payload);
100
101
        $this->payloadEncoder->expects(self::once())
102
            ->method('encode')
103
            ->with($payload)
104
            ->willReturn('{"aps":{}}');
105
106
        $this->uriFactory->expects(self::once())
107
            ->method('create')
108
            ->with($deviceToken, false)
109
            ->willReturn('https://some.com/'.$deviceToken);
110
111
        $this->authenticator->expects(self::once())
112
            ->method('authenticate')
113
            ->with(self::isInstanceOf(Request::class))
114
            ->willReturnCallback(function (Request $innerRequest) {
115
                return $innerRequest;
116
            });
117
118
        $this->visitor->expects(self::once())
119
            ->method('visit')
120
            ->with($notification, self::isInstanceOf(Request::class));
121
122
        $this->httpSender->expects(self::once())
123
            ->method('send')
124
            ->with(self::isInstanceOf(Request::class))
125
            ->willReturn(new Response(200, '{}'));
126
127
        $this->protocol->send($receiver, $notification, false);
128
    }
129
130
    /**
131
     * @test
132
     *
133
     * @expectedException \Apple\ApnPush\Exception\SendNotification\SendNotificationException
134
     */
135
    public function shouldFailSend()
136
    {
137
        $deviceToken = new DeviceToken(str_repeat('af', 32));
138
        $receiver = new Receiver($deviceToken, 'com.test');
139
        $payload = new Payload(new Aps(new Alert()));
140
        $notification = new Notification($payload);
141
142
        $this->payloadEncoder->expects(self::once())
143
            ->method('encode')
144
            ->with($payload)
145
            ->willReturn('{"aps":{}}');
146
147
        $this->uriFactory->expects(self::once())
148
            ->method('create')
149
            ->with($deviceToken, false)
150
            ->willReturn('https://some.com/'.$deviceToken);
151
152
        $this->authenticator->expects(self::once())
153
            ->method('authenticate')
154
            ->with(self::isInstanceOf(Request::class))
155
            ->willReturnCallback(function (Request $innerRequest) {
156
                return $innerRequest;
157
            });
158
159
        $this->visitor->expects(self::once())
160
            ->method('visit')
161
            ->with($notification, self::isInstanceOf(Request::class));
162
163
        $this->httpSender->expects(self::once())
164
            ->method('send')
165
            ->with(self::isInstanceOf(Request::class))
166
            ->willReturn(new Response(404, '{}'));
167
168
        $this->httpSender->expects(self::once())
169
            ->method('close');
170
171
        $this->exceptionFactory->expects(self::once())
172
            ->method('create')
173
            ->with(new Response(404, '{}'))
174
            ->willReturn(self::createMock(SendNotificationException::class));
175
176
        $this->protocol->send($receiver, $notification, false);
177
    }
178
}
179