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