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 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
|
|
|
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(), '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(), 'http://api.url'); |
47
|
|
|
|
48
|
|
|
$request = $this->prophesize(RequestInterface::class); |
49
|
|
|
|
50
|
|
|
$this->messageFactory->createRequest('GET', 'http://api.url?foo=bar&module=API') |
51
|
|
|
->willReturn($request) |
52
|
|
|
; |
53
|
|
|
|
54
|
|
|
$response =$this->prepareResponse('my content'); |
55
|
|
|
|
56
|
|
|
$this->client->sendRequest($request) |
57
|
|
|
->willReturn($response) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
$this->assertSame('my content', $client->send(['foo' => 'bar'])); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testSendWithDateParameter(): void |
64
|
|
|
{ |
65
|
|
|
$client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url'); |
66
|
|
|
|
67
|
|
|
$request = $this->prophesize(RequestInterface::class); |
68
|
|
|
|
69
|
|
|
$this->messageFactory->createRequest('GET', 'http://api.url?date=2010-02-10&module=API') |
70
|
|
|
->willReturn($request) |
71
|
|
|
; |
72
|
|
|
|
73
|
|
|
$response =$this->prepareResponse('my content'); |
74
|
|
|
|
75
|
|
|
$this->client->sendRequest($request) |
76
|
|
|
->willReturn($response) |
77
|
|
|
; |
78
|
|
|
|
79
|
|
|
$this->assertSame('my content', $client->send(['date' => new DateTime('2010-02-10')])); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testSendWithBooleanParameter(): void |
83
|
|
|
{ |
84
|
|
|
$client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url'); |
85
|
|
|
|
86
|
|
|
$request = $this->prophesize(RequestInterface::class); |
87
|
|
|
|
88
|
|
|
$this->messageFactory->createRequest('GET', 'http://api.url?active=1&module=API') |
89
|
|
|
->willReturn($request) |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$response =$this->prepareResponse('my content'); |
93
|
|
|
|
94
|
|
|
$this->client->sendRequest($request) |
95
|
|
|
->willReturn($response) |
96
|
|
|
; |
97
|
|
|
|
98
|
|
|
$this->assertSame('my content', $client->send(['active' => true, 'inactive' => false])); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testSendWithException(): void |
102
|
|
|
{ |
103
|
|
|
$this->expectException(Exception::class); |
104
|
|
|
$this->expectExceptionMessage('Error calling Matomo API.'); |
105
|
|
|
|
106
|
|
|
$client = new HttplugConnection($this->client->reveal(), $this->messageFactory->reveal(), 'http://api.url'); |
107
|
|
|
|
108
|
|
|
$request = $this->prophesize(RequestInterface::class); |
109
|
|
|
|
110
|
|
|
$this->messageFactory->createRequest('GET', 'http://api.url?foo=bar&module=API') |
111
|
|
|
->willReturn($request) |
112
|
|
|
; |
113
|
|
|
|
114
|
|
|
$this->client->sendRequest($request) |
115
|
|
|
->willThrow(Exception::class) |
116
|
|
|
; |
117
|
|
|
|
118
|
|
|
$client->send(['foo' => 'bar']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testSendWithInvalidResponse(): void |
122
|
|
|
{ |
123
|
|
|
$this->expectException(MatomoException::class); |
124
|
|
|
$this->expectExceptionMessage('"http://api.url?foo=bar&module=API" returned an invalid status code: "500"'); |
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
|
|
|
$response =$this->prepareResponse('', 500); |
135
|
|
|
|
136
|
|
|
$this->client->sendRequest($request) |
137
|
|
|
->willReturn($response) |
138
|
|
|
; |
139
|
|
|
|
140
|
|
|
$client->send(['foo' => 'bar']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function prepareResponse(string $content, int $code = 200): ObjectProphecy |
144
|
|
|
{ |
145
|
|
|
$stream = $this->prophesize(StreamInterface::class); |
146
|
|
|
$stream->getContents() |
147
|
|
|
->willReturn($content) |
148
|
|
|
; |
149
|
|
|
|
150
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
151
|
|
|
$response->getBody() |
152
|
|
|
->willReturn($stream) |
153
|
|
|
; |
154
|
|
|
$response->getStatusCode() |
155
|
|
|
->willReturn($code) |
156
|
|
|
; |
157
|
|
|
|
158
|
|
|
return $response; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|