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.

testSendWithInvalidResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.584
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\MatomoBundle\Tests\Connection;
11
12
use Core23\MatomoBundle\Connection\HttplugConnection;
13
use Core23\MatomoBundle\Exception\MatomoException;
14
use Core23\MatomoBundle\Tests\Fixtures\ClientException;
15
use DateTime;
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
final class HttplugConnectionTest extends TestCase
26
{
27
    private $client;
28
29
    private $messageFactory;
30
31
    protected function setUp(): void
32
    {
33
        $this->client         = $this->prophesize(HttpClient::class);
34
        $this->messageFactory = $this->prophesize(MessageFactory::class);
35
    }
36
37
    public function testSend(): void
38
    {
39
        $client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url');
40
41
        $request =  $this->prophesize(RequestInterface::class);
42
43
        $this->messageFactory->createRequest('GET', 'http://api.url?foo=bar&module=API')
44
            ->willReturn($request)
45
        ;
46
47
        $response =$this->prepareResponse('my content');
48
49
        $this->client->sendRequest($request)
50
            ->willReturn($response)
51
        ;
52
53
        static::assertSame('my content', $client->send(['foo' => 'bar']));
54
    }
55
56
    public function testSendWithDateParameter(): void
57
    {
58
        $client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url');
59
60
        $request =  $this->prophesize(RequestInterface::class);
61
62
        $this->messageFactory->createRequest('GET', 'http://api.url?date=2010-02-10&module=API')
63
            ->willReturn($request)
64
        ;
65
66
        $response =$this->prepareResponse('my content');
67
68
        $this->client->sendRequest($request)
69
            ->willReturn($response)
70
        ;
71
72
        static::assertSame('my content', $client->send(['date' => new DateTime('2010-02-10')]));
73
    }
74
75
    public function testSendWithBooleanParameter(): void
76
    {
77
        $client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url');
78
79
        $request =  $this->prophesize(RequestInterface::class);
80
81
        $this->messageFactory->createRequest('GET', 'http://api.url?active=1&module=API')
82
            ->willReturn($request)
83
        ;
84
85
        $response =$this->prepareResponse('my content');
86
87
        $this->client->sendRequest($request)
88
            ->willReturn($response)
89
        ;
90
91
        static::assertSame('my content', $client->send(['active' => true, 'inactive' => false]));
92
    }
93
94
    public function testSendWithArrayParameter(): void
95
    {
96
        $client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url');
97
98
        $request =  $this->prophesize(RequestInterface::class);
99
100
        $this->messageFactory->createRequest('GET', 'http://api.url?foo=bar,baz&module=API')
101
            ->willReturn($request)
102
        ;
103
104
        $response =$this->prepareResponse('my content');
105
106
        $this->client->sendRequest($request)
107
            ->willReturn($response)
108
        ;
109
110
        static::assertSame('my content', $client->send(['foo' => ['bar', 'baz']]));
111
    }
112
113
    public function testSendWithException(): void
114
    {
115
        $this->expectException(Exception::class);
116
        $this->expectExceptionMessage('Error calling Matomo API.');
117
118
        $client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url');
119
120
        $request =  $this->prophesize(RequestInterface::class);
121
122
        $this->messageFactory->createRequest('GET', 'http://api.url?foo=bar&module=API')
123
            ->willReturn($request)
124
        ;
125
126
        $this->client->sendRequest($request)
127
            ->willThrow(Exception::class)
128
        ;
129
130
        $client->send(['foo' => 'bar']);
131
    }
132
133
    public function testSendWithClientException(): void
134
    {
135
        $this->expectException(MatomoException::class);
136
        $this->expectExceptionMessage('Error calling Matomo API.');
137
138
        $client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url');
139
140
        $request =  $this->prophesize(RequestInterface::class);
141
142
        $this->messageFactory->createRequest('GET', 'http://api.url?foo=bar&module=API')
143
            ->willReturn($request)
144
        ;
145
146
        $this->client->sendRequest($request)
147
            ->willThrow(ClientException::class)
148
        ;
149
150
        $client->send(['foo' => 'bar']);
151
    }
152
153
    public function testSendWithInvalidResponse(): void
154
    {
155
        $this->expectException(MatomoException::class);
156
        $this->expectExceptionMessage('"http://api.url?foo=bar&module=API" returned an invalid status code: "500"');
157
158
        $client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url');
159
160
        $request =  $this->prophesize(RequestInterface::class);
161
162
        $this->messageFactory->createRequest('GET', 'http://api.url?foo=bar&module=API')
163
            ->willReturn($request)
164
        ;
165
166
        $response =$this->prepareResponse('', 500);
167
168
        $this->client->sendRequest($request)
169
            ->willReturn($response)
170
        ;
171
172
        $client->send(['foo' => 'bar']);
173
    }
174
175
    private function prepareResponse(string $content, int $code = 200): ObjectProphecy
176
    {
177
        $stream = $this->prophesize(StreamInterface::class);
178
        $stream->getContents()
179
            ->willReturn($content)
180
        ;
181
182
        $response = $this->prophesize(ResponseInterface::class);
183
        $response->getBody()
184
            ->willReturn($stream)
185
        ;
186
        $response->getStatusCode()
187
            ->willReturn($code)
188
        ;
189
190
        return $response;
191
    }
192
}
193