Passed
Pull Request — master (#59)
by Raúl
04:00
created

AbstractMethodTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 146
c 1
b 0
f 0
dl 0
loc 264
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 13 1
A testSetResponseException() 0 23 1
A testSetResponse() 0 22 1
A testGetResponse() 0 19 1
A testGetResponseAsJson() 0 25 1
C testParseHttpException() 0 51 9
A testGetRequest() 0 28 1
A testHasSlashConstant() 0 4 1
A testAddGetParameters() 0 14 1
1
<?php
2
3
namespace Test\Pagantis\OrdersApiClient\Method;
4
5
use Httpful\Request;
6
use Pagantis\OrdersApiClient\Exception\HttpException;
7
use Pagantis\OrdersApiClient\Method\AbstractMethod;
8
use Pagantis\OrdersApiClient\Model\ApiConfiguration;
9
use Test\Pagantis\OrdersApiClient\AbstractTest;
10
11
/**
12
 * Class AbstractMethodTest
13
 *
14
 * @package Test\Pagantis\OrdersApiClient\Method;
15
 */
16
class AbstractMethodTest extends AbstractTest
17
{
18
    /**
19
     * Has Slash
20
     */
21
    public function testHasSlashConstant()
22
    {
23
        $constant = AbstractMethod::SLASH;
24
        $this->assertEquals('/', $constant);
25
    }
26
27
    /**
28
     * Test Constructor
29
     *
30
     * @throws \ReflectionException
31
     */
32
    public function testConstructor()
33
    {
34
        $apiConfiguration = new ApiConfiguration();
35
        $abstractMethod = $this->getMock(
36
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
37
            array('call'),
38
            array($apiConfiguration)
39
        );
40
41
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
42
        $property = $reflectedClass->getProperty('apiConfiguration');
43
        $property->setAccessible(true);
44
        $this->assertSame($apiConfiguration, $property->getValue($abstractMethod));
45
    }
46
47
    /**
48
     * Test get Request
49
     *
50
     * @throws \ReflectionException
51
     */
52
    public function testGetRequest()
53
    {
54
        $headers = array('key' => 'value');
55
        $publicKey = 'publicKey';
56
        $privateKey = 'privateKey';
57
58
        $apiConfiguration = new ApiConfiguration();
59
        $apiConfiguration
60
            ->setHeaders($headers)
61
            ->setPrivateKey($privateKey)
62
            ->setPublicKey($publicKey)
63
        ;
64
        $abstractMethod = $this->getMock(
65
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
66
            array('call'),
67
            array($apiConfiguration)
68
        );
69
70
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
71
        $method = $reflectedClass->getMethod('getRequest');
72
        $method->setAccessible(true);
73
        /** @var Request $request */
74
        $request = $method->invoke($abstractMethod);
75
        $this->assertInstanceOf('Httpful\Request', $request);
76
77
        $this->assertSame($headers, $request->headers);
78
        $this->assertSame($publicKey, $request->username);
79
        $this->assertSame($privateKey, $request->password);
80
    }
81
82
    /**
83
     * testGetResponse
84
     *
85
     * @throws \ReflectionException
86
     */
87
    public function testGetResponse()
88
    {
89
        $apiConfiguration = new ApiConfiguration();
90
        $abstractMethod = $this->getMock(
91
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
92
            array('call'),
93
            array($apiConfiguration)
94
        );
95
96
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
97
        $method = $reflectedClass->getMethod('getResponse');
98
        $method->setAccessible(true);
99
        $this->assertFalse($method->invoke($abstractMethod));
100
101
        $responseMock = $this->getMockBuilder('Httpful\Response')->disableOriginalConstructor()->getMock();
102
        $property = $reflectedClass->getProperty('response');
103
        $property->setAccessible(true);
104
        $property->setValue($abstractMethod, $responseMock);
105
        $this->assertInstanceOf('Httpful\Response', $method->invoke($abstractMethod));
106
    }
107
108
    /**
109
     * testGetResponseAsJson
110
     *
111
     * @throws \ReflectionException
112
     */
113
    public function testGetResponseAsJson()
114
    {
115
        $apiConfiguration = new ApiConfiguration();
116
        $abstractMethod = $this->getMock(
117
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
118
            array('call'),
119
            array($apiConfiguration)
120
        );
121
122
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
123
        $method = $reflectedClass->getMethod('getResponseAsJson');
124
        $method->setAccessible(true);
125
        $this->assertFalse($method->invoke($abstractMethod));
126
127
        $json = 'body';
128
        $responseMock = $this->getMockBuilder('Httpful\Response')->disableOriginalConstructor()->getMock();
129
        $responseMockReflect = new \ReflectionClass('Httpful\Response');
130
        $property = $responseMockReflect->getProperty('raw_body');
131
        $property->setAccessible(true);
132
        $property->setValue($responseMock, $json);
133
134
        $property = $reflectedClass->getProperty('response');
135
        $property->setAccessible(true);
136
        $property->setValue($abstractMethod, $responseMock);
137
        $this->assertSame($json, $method->invoke($abstractMethod));
138
    }
139
140
    /**
141
     * Test Add get parameters work correctly
142
     *
143
     * @throws \ReflectionException
144
     */
145
    public function testAddGetParameters()
146
    {
147
        $apiConfiguration = new ApiConfiguration();
148
        $abstractMethod = $this->getMock(
149
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
150
            array('call'),
151
            array($apiConfiguration)
152
        );
153
154
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
155
        $method = $reflectedClass->getMethod('addGetParameters');
156
        $method->setAccessible(true);
157
        $this->assertEquals('', $method->invoke($abstractMethod, array()));
158
        $this->assertEquals('?id=123', $method->invoke($abstractMethod, array('id' => 123)));
159
    }
160
161
    /**
162
     * Test Parse HTTP Exceptions
163
     *
164
     * @throws \ReflectionException
165
     */
166
    public function testParseHttpException()
167
    {
168
        $apiConfiguration = new ApiConfiguration();
169
        $abstractMethod = $this->getMock(
170
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
171
            array('call'),
172
            array($apiConfiguration)
173
        );
174
175
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
176
        $method = $reflectedClass->getMethod('parseHttpException');
177
        $method->setAccessible(true);
178
        try {
179
            $method->invoke($abstractMethod, HttpException::HTTP_BAD_REQUEST);
180
        } catch (\Exception $exception) {
181
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
182
        }
183
        try {
184
            $method->invoke($abstractMethod, HttpException::HTTP_UNAUTHORIZED);
185
        } catch (\Exception $exception) {
186
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
187
        }
188
        try {
189
            $method->invoke($abstractMethod, HttpException::HTTP_FORBIDDEN);
190
        } catch (\Exception $exception) {
191
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
192
        }
193
        try {
194
            $method->invoke($abstractMethod, HttpException::HTTP_NOT_FOUND);
195
        } catch (\Exception $exception) {
196
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
197
        }
198
        try {
199
            $method->invoke($abstractMethod, HttpException::HTTP_METHOD_NOT_ALLOWED);
200
        } catch (\Exception $exception) {
201
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
202
        }
203
        try {
204
            $method->invoke($abstractMethod, HttpException::HTTP_UNPROCESSABLE_ENTITY);
205
        } catch (\Exception $exception) {
206
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
207
        }
208
        try {
209
            $method->invoke($abstractMethod, HttpException::HTTP_INTERNAL_SERVER_ERROR);
210
        } catch (\Exception $exception) {
211
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
212
        }
213
        try {
214
            $method->invoke($abstractMethod, HttpException::HTTP_SERVICE_UNAVAILABLE);
215
        } catch (\Exception $exception) {
216
            $this->assertInstanceOf('Pagantis\OrdersApiClient\Exception\HttpException', $exception);
217
        }
218
    }
219
220
    /**
221
     * testSetResponse
222
     *
223
     * @expectedException Pagantis\OrdersApiClient\Exception\HttpException
224
     *
225
     * @throws \ReflectionException
226
     */
227
    public function testSetResponseException()
228
    {
229
        $responseMock = $this
230
            ->getMockBuilder('Httpful\Response')
231
            ->disableOriginalConstructor()
232
            ->getMock()
233
        ;
234
235
        $abstractMethod = $this
236
            ->getMockBuilder('Pagantis\OrdersApiClient\Method\AbstractMethod')
237
            ->disableOriginalConstructor()
238
            ->getMock()
239
        ;
240
241
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
242
        $method = $reflectedClass->getMethod('setResponse');
243
        $method->setAccessible(true);
244
245
        $responseMock->code = HttpException::HTTP_INTERNAL_SERVER_ERROR;
0 ignored issues
show
Bug introduced by
Accessing code on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
246
        $responseMock->method('hasErrors')->willReturn(true);
247
        $this->assertInstanceOf(
248
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
249
            $method->invoke($abstractMethod, $responseMock)
250
        );
251
    }
252
253
    /**
254
     * testSetResponse
255
     *
256
     * @throws \ReflectionException
257
     */
258
    public function testSetResponse()
259
    {
260
        $responseMock = $this
261
            ->getMockBuilder('Httpful\Response')
262
            ->disableOriginalConstructor()
263
            ->getMock()
264
        ;
265
266
        $abstractMethod = $this
267
            ->getMockBuilder('Pagantis\OrdersApiClient\Method\AbstractMethod')
268
            ->disableOriginalConstructor()
269
            ->getMock()
270
        ;
271
272
        $reflectedClass = new \ReflectionClass('Pagantis\OrdersApiClient\Method\AbstractMethod');
273
        $method = $reflectedClass->getMethod('setResponse');
274
        $method->setAccessible(true);
275
276
        $responseMock->method('hasErrors')->willReturn(false);
277
        $this->assertInstanceOf(
278
            'Pagantis\OrdersApiClient\Method\AbstractMethod',
279
            $method->invoke($abstractMethod, $responseMock)
280
        );
281
    }
282
}
283