GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HTTPlugConnectionTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 167
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testSend() 0 21 1
A testSendWithBooleanParameter() 0 21 1
A testSendWithArrayParameter() 0 21 1
A testSendWithException() 0 22 1
A testSendWithClientException() 0 22 1
A testSendWithInvalidResponse() 0 24 1
A prepareResponse() 0 17 1
1
<?php
2
3
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\SetlistFm\Tests\Connection;
11
12
use Core23\SetlistFm\Connection\HTTPlugConnection;
13
use Core23\SetlistFm\Exception\ApiException;
14
use Core23\SetlistFm\Tests\Fixtures\ClientException;
15
use Exception;
16
use Http\Client\HttpClient;
17
use Http\Message\MessageFactory;
18
use PHPUnit\Framework\TestCase;
19
use Prophecy\Prophecy\ObjectProphecy;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\StreamInterface;
23
24
final class HTTPlugConnectionTest extends TestCase
25
{
26
    private $client;
27
28
    private $messageFactory;
29
30
    protected function setUp(): void
31
    {
32
        $this->client         = $this->prophesize(HttpClient::class);
33
        $this->messageFactory = $this->prophesize(MessageFactory::class);
34
    }
35
36
    public function testSend(): void
37
    {
38
        $client = new HTTPlugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'my-key', 'http://api.url/');
39
40
        $request =  $this->prophesize(RequestInterface::class);
41
42
        $this->messageFactory->createRequest('GET', 'http://api.url/method?foo=bar', [
43
            'Accept'    => 'application/json',
44
            'x-api-key' => 'my-key',
45
        ])
46
            ->willReturn($request)
47
        ;
48
49
        $response =$this->prepareResponse('{"data": "test"}');
50
51
        $this->client->sendRequest($request)
52
            ->willReturn($response)
53
        ;
54
55
        static::assertSame(['data' => 'test'], $client->call('method', ['foo' => 'bar']));
56
    }
57
58
    public function testSendWithBooleanParameter(): void
59
    {
60
        $client = new HTTPlugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'my-key', 'http://api.url/');
61
62
        $request =  $this->prophesize(RequestInterface::class);
63
64
        $this->messageFactory->createRequest('GET', 'http://api.url/method?active=1&inactive=0', [
65
            'Accept'    => 'application/json',
66
            'x-api-key' => 'my-key',
67
        ])
68
            ->willReturn($request)
69
        ;
70
71
        $response =$this->prepareResponse('{"data": "test"}');
72
73
        $this->client->sendRequest($request)
74
            ->willReturn($response)
75
        ;
76
77
        static::assertSame(['data' => 'test'], $client->call('method', ['active' => true, 'inactive' => false]));
78
    }
79
80
    public function testSendWithArrayParameter(): void
81
    {
82
        $client = new HTTPlugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'my-key', 'http://api.url/');
83
84
        $request =  $this->prophesize(RequestInterface::class);
85
86
        $this->messageFactory->createRequest('GET', 'http://api.url/method?foo%5B0%5D=bar&foo%5B1%5D=baz', [
87
            'Accept'    => 'application/json',
88
            'x-api-key' => 'my-key',
89
        ])
90
            ->willReturn($request)
91
        ;
92
93
        $response =$this->prepareResponse('{"data": "test"}');
94
95
        $this->client->sendRequest($request)
96
            ->willReturn($response)
97
        ;
98
99
        static::assertSame(['data' => 'test'], $client->call('method', ['foo' => ['bar', 'baz']]));
100
    }
101
102
    public function testSendWithException(): void
103
    {
104
        $this->expectException(Exception::class);
105
        $this->expectExceptionMessage('Technical error occurred.');
106
107
        $client = new HTTPlugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'my-key', 'http://api.url/');
108
109
        $request =  $this->prophesize(RequestInterface::class);
110
111
        $this->messageFactory->createRequest('GET', 'http://api.url/method?foo=bar', [
112
            'Accept'    => 'application/json',
113
            'x-api-key' => 'my-key',
114
        ])
115
            ->willReturn($request)
116
        ;
117
118
        $this->client->sendRequest($request)
119
            ->willThrow(Exception::class)
120
        ;
121
122
        $client->call('method', ['foo' => 'bar']);
123
    }
124
125
    public function testSendWithClientException(): void
126
    {
127
        $this->expectException(ApiException::class);
128
        $this->expectExceptionMessage('Technical error occurred.');
129
130
        $client = new HTTPlugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'my-key', 'http://api.url/');
131
132
        $request =  $this->prophesize(RequestInterface::class);
133
134
        $this->messageFactory->createRequest('GET', 'http://api.url/method?foo=bar', [
135
            'Accept'    => 'application/json',
136
            'x-api-key' => 'my-key',
137
        ])
138
            ->willReturn($request)
139
        ;
140
141
        $this->client->sendRequest($request)
142
            ->willThrow(ClientException::class)
143
        ;
144
145
        $client->call('method', ['foo' => 'bar']);
146
    }
147
148
    public function testSendWithInvalidResponse(): void
149
    {
150
        $this->expectException(ApiException::class);
151
        $this->expectExceptionMessage('Server did not reply with a valid response.');
152
153
        $client = new HTTPlugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'my-key', 'http://api.url/');
154
155
        $request =  $this->prophesize(RequestInterface::class);
156
157
        $this->messageFactory->createRequest('GET', 'http://api.url/method?foo=bar', [
158
            'Accept'    => 'application/json',
159
            'x-api-key' => 'my-key',
160
        ])
161
            ->willReturn($request)
162
        ;
163
164
        $response =$this->prepareResponse('', 500);
165
166
        $this->client->sendRequest($request)
167
            ->willReturn($response)
168
        ;
169
170
        $client->call('method', ['foo' => 'bar']);
171
    }
172
173
    private function prepareResponse(string $content, int $code = 200): ObjectProphecy
174
    {
175
        $stream = $this->prophesize(StreamInterface::class);
176
        $stream->getContents()
177
            ->willReturn($content)
178
        ;
179
180
        $response = $this->prophesize(ResponseInterface::class);
181
        $response->getBody()
182
            ->willReturn($stream)
183
        ;
184
        $response->getStatusCode()
185
            ->willReturn($code)
186
        ;
187
188
        return $response;
189
    }
190
}
191