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

ExceptionFactoryTest   C

Complexity

Total Complexity 6

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 33

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 33
dl 0
loc 111
rs 5
c 0
b 0
f 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\Http\ExceptionFactory;
13
14
use Apple\ApnPush\Exception\SendNotification\BadCertificateEnvironmentException;
15
use Apple\ApnPush\Exception\SendNotification\BadCertificateException;
16
use Apple\ApnPush\Exception\SendNotification\BadCollapseIdException;
17
use Apple\ApnPush\Exception\SendNotification\BadDeviceTokenException;
18
use Apple\ApnPush\Exception\SendNotification\BadExpirationDateException;
19
use Apple\ApnPush\Exception\SendNotification\BadMessageIdException;
20
use Apple\ApnPush\Exception\SendNotification\BadPathException;
21
use Apple\ApnPush\Exception\SendNotification\BadPriorityException;
22
use Apple\ApnPush\Exception\SendNotification\BadTopicException;
23
use Apple\ApnPush\Exception\SendNotification\DeviceTokenNotForTopicException;
24
use Apple\ApnPush\Exception\SendNotification\DuplicateHeadersException;
25
use Apple\ApnPush\Exception\SendNotification\ExpiredProviderTokenException;
26
use Apple\ApnPush\Exception\SendNotification\ForbiddenException;
27
use Apple\ApnPush\Exception\SendNotification\IdleTimeoutException;
28
use Apple\ApnPush\Exception\SendNotification\InternalServerErrorException;
29
use Apple\ApnPush\Exception\SendNotification\InvalidProviderTokenException;
30
use Apple\ApnPush\Exception\SendNotification\MethodNotAllowedException;
31
use Apple\ApnPush\Exception\SendNotification\MissingDeviceTokenException;
32
use Apple\ApnPush\Exception\SendNotification\MissingProviderTokenException;
33
use Apple\ApnPush\Exception\SendNotification\MissingTopicException;
34
use Apple\ApnPush\Exception\SendNotification\PayloadEmptyException;
35
use Apple\ApnPush\Exception\SendNotification\PayloadTooLargeException;
36
use Apple\ApnPush\Exception\SendNotification\ServiceUnavailableException;
37
use Apple\ApnPush\Exception\SendNotification\ShutdownException;
38
use Apple\ApnPush\Exception\SendNotification\TooManyProviderTokenUpdatesException;
39
use Apple\ApnPush\Exception\SendNotification\TooManyRequestsException;
40
use Apple\ApnPush\Exception\SendNotification\TopicDisallowedException;
41
use Apple\ApnPush\Exception\SendNotification\UndefinedErrorException;
42
use Apple\ApnPush\Exception\SendNotification\UnregisteredException;
43
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactory;
44
use Apple\ApnPush\Protocol\Http\Response;
45
use PHPUnit\Framework\TestCase;
46
47
class ExceptionFactoryTest extends TestCase
48
{
49
    /**
50
     * @var ExceptionFactory
51
     */
52
    private $exceptionFactory;
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function setUp()
58
    {
59
        $this->exceptionFactory = new ExceptionFactory();
60
    }
61
62
    /**
63
     * @test
64
     *
65
     * @expectedException \Apple\ApnPush\Exception\SendNotification\MissingContentInResponseException
66
     * @expectedExceptionMessage Missing content in response.
67
     */
68
    public function shouldFailIfContentNotFound()
69
    {
70
        $response = new Response(400, '');
71
        throw $this->exceptionFactory->create($response);
72
    }
73
74
    /**
75
     * @test
76
     *
77
     * @expectedException \Apple\ApnPush\Exception\SendNotification\InvalidResponseException
78
     */
79
    public function shouldFailIfInvalidJson()
80
    {
81
        $response = new Response(400, '{"some}');
82
        throw $this->exceptionFactory->create($response);
83
    }
84
85
    /**
86
     * @test
87
     *
88
     * @expectedException \Apple\ApnPush\Exception\SendNotification\MissingErrorReasonInResponseException
89
     * @expectedExceptionMessage Missing error reason in response.
90
     */
91
    public function shouldFailIfMissingReason()
92
    {
93
        $response = new Response(400, '{"key":"value"}');
94
        throw $this->exceptionFactory->create($response);
95
    }
96
97
    /**
98
     * @test
99
     *
100
     * @param string     $reason
101
     * @param \Exception $expectedException
102
     * @param array      $extra
103
     *
104
     * @dataProvider provideReasons
105
     */
106
    public function shouldSuccessCreate($reason, \Exception $expectedException, array $extra = [])
107
    {
108
        $json = array_merge([
109
            'reason' => $reason,
110
        ], $extra);
111
112
        $response = new Response(200, json_encode($json));
113
        $exception = $this->exceptionFactory->create($response);
114
115
        self::assertEquals($expectedException, $exception);
116
    }
117
118
    /**
119
     * Provide reasons
120
     *
121
     * @return array
122
     */
123
    public function provideReasons()
124
    {
125
        $lastUse = \DateTime::createFromFormat('!Y/m/d', '2017/01/01');
126
127
        return [
128
            ['BadCollapseId', new BadCollapseIdException()],
129
            ['BadDeviceToken', new BadDeviceTokenException()],
130
            ['BadExpirationDate', new BadExpirationDateException()],
131
            ['BadMessageId', new BadMessageIdException()],
132
            ['BadPriority', new BadPriorityException()],
133
            ['BadTopic', new BadTopicException()],
134
            ['DeviceTokenNotForTopic', new DeviceTokenNotForTopicException()],
135
            ['DuplicateHeaders', new DuplicateHeadersException()],
136
            ['IdleTimeout', new IdleTimeoutException()],
137
            ['MissingDeviceToken', new MissingDeviceTokenException()],
138
            ['MissingTopic', new MissingTopicException()],
139
            ['PayloadEmpty', new PayloadEmptyException()],
140
            ['TopicDisallowed', new TopicDisallowedException()],
141
            ['BadCertificate', new BadCertificateException()],
142
            ['BadCertificateEnvironment', new BadCertificateEnvironmentException()],
143
            ['ExpiredProviderToken', new ExpiredProviderTokenException()],
144
            ['Forbidden', new ForbiddenException()],
145
            ['InvalidProviderToken', new InvalidProviderTokenException()],
146
            ['MissingProviderToken', new MissingProviderTokenException()],
147
            ['BadPath', new BadPathException()],
148
            ['MethodNotAllowed', new MethodNotAllowedException()],
149
            ['Unregistered', new UnregisteredException($lastUse), ['timestamp' => $lastUse->format('U')]],
150
            ['PayloadTooLarge', new PayloadTooLargeException()],
151
            ['TooManyProviderTokenUpdates', new TooManyProviderTokenUpdatesException()],
152
            ['TooManyRequests', new TooManyRequestsException()],
153
            ['InternalServerError', new InternalServerErrorException()],
154
            ['ServiceUnavailable', new ServiceUnavailableException()],
155
            ['Shutdown', new ShutdownException()],
156
            ['some', new UndefinedErrorException()],
157
        ];
158
    }
159
}
160