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.
Completed
Pull Request — master (#26)
by
unknown
01:26
created

PsrClientConnectionTest   A

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