Passed
Pull Request — develop (#19)
by Paulius
02:32
created

testInvalidApiKeyError403()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Dokobit\Gateway\Tests\Http;
3
4
use GuzzleHttp\Exception\ClientException;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\BufferStream;
7
use GuzzleHttp\Psr7\Response;
8
use Dokobit\Gateway\Http\GuzzleClientAdapter;
9
use Psr\Log\LoggerAwareInterface;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use PHPUnit\Framework\MockObject\MockObject;
13
14
use PHPUnit\Framework\TestCase;
15
16
class GuzzleClientAdapterTest extends TestCase
17
{
18
    /**
19
     * @var GuzzleClientAdapter
20
     */
21
    private $adapter;
22
23
    /** @var PHPUnit_Framework_MockObject_MockObject|Client */
0 ignored issues
show
Bug introduced by
The type Dokobit\Gateway\Tests\Ht...k_MockObject_MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
    private $client;
25
26
    protected function setUp(): void
27
    {
28
        $this->client = $this->getMockBuilder(Client::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Gu...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Dokobit\Gateway\Tests\Ht...bject|GuzzleHttp\Client of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
            ->disableOriginalConstructor()
30
            ->getMock();
31
32
        $this->adapter = new GuzzleClientAdapter($this->client);
33
    }
34
35
    public function testPost()
36
    {
37
        $response = $this->getMockBuilder(ResponseInterface::class)
38
            ->disableOriginalConstructor()
39
            ->getMock()
40
        ;
41
        $response
42
            ->expects($this->once())
43
            ->method('getBody')
44
            ->willReturn(new BufferStream())
45
        ;
46
        $this->client
47
            ->expects($this->once())
48
            ->method('request')
0 ignored issues
show
Bug introduced by
The method method() does not exist on GuzzleHttp\Promise\PromiseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            ->/** @scrutinizer ignore-call */ method('request')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
            ->willReturn($response)
50
        ;
51
52
        $this->adapter->requestJson('POST', 'https://gateway-sandbox.dokobit.com');
53
    }
54
55
    public function testDataValidationError400()
56
    {
57
        $this->expectException(\Dokobit\Gateway\Exception\InvalidData::class);
58
        $this->expectExceptionCode('400');
59
        $request = $this->getMockBuilder(RequestInterface::class)->getMock();
60
        $response = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
61
62
        $response
63
            ->method('getStatusCode')
64
            ->willReturn(400)
65
        ;
66
        $response
67
            ->expects($this->once())
68
            ->method('getBody')
69
            ->willReturn(new BufferStream())
70
        ;
71
72
        $this->client
73
            ->method('request')
74
            ->will(
0 ignored issues
show
Bug introduced by
The method will() does not exist on GuzzleHttp\Promise\PromiseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            ->/** @scrutinizer ignore-call */ will(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
                $this->throwException(
76
                    new ClientException(
77
                        'Error',
78
                        $request,
79
                        $response
80
                    )
81
                )
82
            )
83
        ;
84
85
        $this->adapter->requestJson('POST', 'https://gateway-sandbox.dokobit.com');
86
    }
87
88
    public function testInvalidApiKeyError403()
89
    {
90
        $this->expectException(\Dokobit\Gateway\Exception\InvalidApiKey::class);
91
        $this->expectExceptionCode('403');
92
        $request = $this->getMockBuilder(RequestInterface::class)->getMock();
93
        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
94
95
        $response
96
            ->method('getStatusCode')
97
            ->willReturn(403)
98
        ;
99
        $response
100
            ->expects($this->once())
101
            ->method('getBody')
102
            ->willReturn('response body')
103
        ;
104
105
        $this->client
106
            ->method('request')
107
            ->will(
108
                $this->throwException(
109
                    new ClientException(
110
                        'Error',
111
                        $request,
112
                        $response
113
                    )
114
                )
115
            )
116
        ;
117
118
        $this->adapter->requestJson('POST', 'https://gateway-sandbox.dokobit.com');
119
    }
120
121
    public function testServerError500()
122
    {
123
        $this->expectException(\Dokobit\Gateway\Exception\ServerError::class);
124
        $this->expectExceptionCode('500');
125
        $request = $this->getMockBuilder(RequestInterface::class)->getMock();
126
        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
127
128
        $response
129
            ->method('getStatusCode')
130
            ->willReturn(500)
131
        ;
132
        $response
133
            ->expects($this->once())
134
            ->method('getBody')
135
            ->willReturn('response body')
136
        ;
137
138
        $this->client
139
            ->method('request')
140
            ->will(
141
                $this->throwException(
142
                    new ClientException(
143
                        'Error',
144
                        $request,
145
                        $response
146
                    )
147
                )
148
            )
149
        ;
150
151
        $this->adapter->requestJson('POST', 'https://gateway-sandbox.dokobit.com');
152
    }
153
154
    public function testTimeout504()
155
    {
156
        $this->expectException(\Dokobit\Gateway\Exception\Timeout::class);
157
        $this->expectExceptionCode('504');
158
        $request = $this->getMockBuilder(RequestInterface::class)->getMock();
159
        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
160
161
        $response
162
            ->method('getStatusCode')
163
            ->willReturn(504)
164
        ;
165
        $response
166
            ->expects($this->once())
167
            ->method('getBody')
168
            ->willReturn('response body')
169
        ;
170
171
        $this->client
172
            ->method('request')
173
            ->will(
174
                $this->throwException(
175
                    new ClientException(
176
                        'Error',
177
                        $request,
178
                        $response
179
                    )
180
                )
181
            )
182
        ;
183
184
        $this->adapter->requestJson('POST', 'https://gateway-sandbox.dokobit.com');
185
    }
186
187
    public function testUnexpectedResponseStatusCode()
188
    {
189
        $this->expectException(\Dokobit\Gateway\Exception\UnexpectedResponse::class);
190
        $this->expectExceptionCode('101');
191
        $request = $this->getMockBuilder(RequestInterface::class)->getMock();
192
        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
193
194
        $response
195
            ->method('getStatusCode')
196
            ->willReturn(101)
197
        ;
198
        $response
199
            ->expects($this->once())
200
            ->method('getBody')
201
            ->willReturn('response body')
202
        ;
203
204
        $this->client
205
            ->method('request')
206
            ->will(
207
                $this->throwException(
208
                    new ClientException(
209
                        'Error',
210
                        $request,
211
                        $response
212
                    )
213
                )
214
            )
215
        ;
216
217
        $this->adapter->requestJson('POST', 'https://gateway-sandbox.dokobit.com');
218
    }
219
220
    public function testUnexpectedError()
221
    {
222
        $this->expectException(\Dokobit\Gateway\Exception\UnexpectedError::class);
223
        $this->client
224
            ->method('request')
225
            ->will(
226
                $this->throwException(
227
                    new \Exception()
228
                )
229
            )
230
        ;
231
232
        $this->adapter->requestJson('POST', 'https://gateway-sandbox.dokobit.com');
233
    }
234
}
235