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
Push — master ( a61514...3a9ec1 )
by Christian
01:29
created

HTTPlugConnectionTest::testItIsInstantiable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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