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\Client; |
11
|
|
|
|
12
|
|
|
use Core23\MatomoBundle\Client\Client; |
13
|
|
|
use Core23\MatomoBundle\Client\ClientInterface; |
14
|
|
|
use Core23\MatomoBundle\Connection\ConnectionInterface; |
15
|
|
|
use Core23\MatomoBundle\Exception\MatomoException; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
class ClientTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private $connection; |
21
|
|
|
|
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->connection = $this->prophesize(ConnectionInterface::class); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testItIsInstantiable(): void |
28
|
|
|
{ |
29
|
|
|
$client = new Client($this->connection->reveal()); |
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf(ClientInterface::class, $client); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetConnection(): void |
35
|
|
|
{ |
36
|
|
|
$client = new Client($this->connection->reveal()); |
37
|
|
|
|
38
|
|
|
$this->assertSame($this->connection->reveal(), $client->getConnection()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCall(): void |
42
|
|
|
{ |
43
|
|
|
$this->connection->send([ |
44
|
|
|
'foo' => 'bar', |
45
|
|
|
'method' => 'foo/method', |
46
|
|
|
'token_auth' => 'MY_TOKEN', |
47
|
|
|
'format' => 'php', |
48
|
|
|
]) |
49
|
|
|
->willReturn('a:1:{s:6:"result";s:4:"data";}') |
50
|
|
|
; |
51
|
|
|
|
52
|
|
|
$client = new Client($this->connection->reveal(), 'MY_TOKEN'); |
53
|
|
|
$result = $client->call('foo/method', ['foo' => 'bar']); |
54
|
|
|
|
55
|
|
|
$this->assertSame(['result' => 'data'], $result); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testCallWithCustomFormat(): void |
59
|
|
|
{ |
60
|
|
|
$this->connection->send([ |
61
|
|
|
'foo' => 'bar', |
62
|
|
|
'method' => 'foo/method', |
63
|
|
|
'token_auth' => 'MY_TOKEN', |
64
|
|
|
'format' => 'custom', |
65
|
|
|
]) |
66
|
|
|
->willReturn('The result') |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
$client = new Client($this->connection->reveal(), 'MY_TOKEN'); |
70
|
|
|
$result = $client->call('foo/method', ['foo' => 'bar'], 'custom'); |
71
|
|
|
|
72
|
|
|
$this->assertSame('The result', $result); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testCallWithApiError(): void |
76
|
|
|
{ |
77
|
|
|
$this->expectException(MatomoException::class); |
78
|
|
|
$this->expectExceptionMessage('There is an error'); |
79
|
|
|
|
80
|
|
|
$this->connection->send([ |
81
|
|
|
'foo' => 'bar', |
82
|
|
|
'method' => 'foo/method', |
83
|
|
|
'token_auth' => 'MY_TOKEN', |
84
|
|
|
'format' => 'php', |
85
|
|
|
]) |
86
|
|
|
->willReturn('a:2:{s:6:"result";s:5:"error";s:7:"message";s:17:"There is an error";}') |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$client = new Client($this->connection->reveal(), 'MY_TOKEN'); |
90
|
|
|
|
91
|
|
|
$client->call('foo/method', ['foo' => 'bar']); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|