Skip to content

Commit

Permalink
Add support for matomo 4
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Dec 6, 2020
1 parent 91a4d1f commit edb931c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"require": {
"php": "^7.3",
"ext-json": "*",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
Expand Down
4 changes: 2 additions & 2 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"text": "build/infection/infection-log.txt"
},
"ignoreMsiWithNoMutations": true,
"minMsi": 41,
"minCoveredMsi": 42
"minMsi": 39,
"minCoveredMsi": 40
}
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ parameters:
count: 1
path: tests/Block/BlockServiceIntegrationTest.php

-
message: "#^Parameter \\#2 \\$haystack of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertCount\\(\\) expects Countable\\|iterable, array\\|string given\\.$#"
count: 2
path: tests/Client/ClientTest.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Twig\\\\\\\\TwigFunction' and Twig\\\\TwigFunction will always evaluate to true\\.$#"
count: 1
Expand Down
19 changes: 10 additions & 9 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Nucleos\MatomoBundle\Client;

use Exception;
use Nucleos\MatomoBundle\Connection\ConnectionInterface;
use Nucleos\MatomoBundle\Exception\MatomoException;

Expand Down Expand Up @@ -47,20 +48,20 @@ public function call(string $method, array $params = [], string $format = 'php')
{
$params['method'] = $method;
$params['token_auth'] = $this->token;
$params['format'] = $format;
$params['format'] = 'json';
$data = $this->getConnection()->send($params);

if ('php' === $format) {
$object = unserialize($data);

if (isset($object['result']) && 'error' === $object['result']) {
throw new MatomoException($object['message']);
}
if ('php' !== $format) {
@trigger_error(sprintf('Argument #3 of %s is deprecated and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
}

return $object;
try {
$object = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
throw new MatomoException('Invalid server response');
}

return $data;
return $object;
}

public function getConnection(): ConnectionInterface
Expand Down
1 change: 0 additions & 1 deletion src/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function setToken(string $token): void;
*
* @param string $method method name
* @param array<string, mixed> $params method parameters
* @param string $format return format (php, json, xml, csv, tsv, html, rss)
*
* @throws MatomoException
*
Expand Down
60 changes: 34 additions & 26 deletions tests/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,58 @@ public function testGetConnection(): void

public function testCall(): void
{
$this->connection->send([
'foo' => 'bar',
'method' => 'foo/method',
'token_auth' => 'MY_TOKEN',
'format' => 'php',
])
->willReturn('a:1:{s:6:"result";s:4:"data";}')
;
$this->connection
->send([
'foo' => 'bar',
'method' => 'foo/method',
'token_auth' => 'MY_TOKEN',
'format' => 'json',
])
->willReturn('{"2020-11-07":1,"2020-11-08":2,"2020-11-09":6,"2020-11-10":6,"2020-11-11":8,"2020-11-12":6,"2020-11-13":2,"2020-11-14":1,"2020-11-15":3,"2020-11-16":4,"2020-11-17":13,"2020-11-18":4,"2020-11-19":5,"2020-11-20":3,"2020-11-21":5,"2020-11-22":5,"2020-11-23":6,"2020-11-24":11,"2020-11-25":4,"2020-11-26":9,"2020-11-27":4,"2020-11-28":4,"2020-11-29":4,"2020-11-30":3,"2020-12-01":12,"2020-12-02":7,"2020-12-03":6,"2020-12-04":3,"2020-12-05":4,"2020-12-06":1}')
;

$client = new Client($this->connection->reveal(), 'MY_TOKEN');
$result = $client->call('foo/method', ['foo' => 'bar']);

static::assertSame(['result' => 'data'], $result);
static::assertCount(30, $result);
}

/**
* @expectedDeprecation Argument #3 of Nucleos\MatomoBundle\Client\Client::call is deprecated and will be removed in 4.0.
*
* @group legacy
*/
public function testCallWithCustomFormat(): void
{
$this->connection->send([
'foo' => 'bar',
'method' => 'foo/method',
'token_auth' => 'MY_TOKEN',
'format' => 'custom',
])
->willReturn('The result')
$this->connection
->send([
'foo' => 'bar',
'method' => 'foo/method',
'token_auth' => 'MY_TOKEN',
'format' => 'json',
])
->willReturn('{"2020-11-07":1,"2020-11-08":2,"2020-11-09":6,"2020-11-10":6,"2020-11-11":8,"2020-11-12":6,"2020-11-13":2,"2020-11-14":1,"2020-11-15":3,"2020-11-16":4,"2020-11-17":13,"2020-11-18":4,"2020-11-19":5,"2020-11-20":3,"2020-11-21":5,"2020-11-22":5,"2020-11-23":6,"2020-11-24":11,"2020-11-25":4,"2020-11-26":9,"2020-11-27":4,"2020-11-28":4,"2020-11-29":4,"2020-11-30":3,"2020-12-01":12,"2020-12-02":7,"2020-12-03":6,"2020-12-04":3,"2020-12-05":4,"2020-12-06":1}')
;

$client = new Client($this->connection->reveal(), 'MY_TOKEN');
$result = $client->call('foo/method', ['foo' => 'bar'], 'custom');

static::assertSame('The result', $result);
static::assertCount(30, $result);
}

public function testCallWithApiError(): void
{
$this->expectException(MatomoException::class);
$this->expectExceptionMessage('There is an error');

$this->connection->send([
'foo' => 'bar',
'method' => 'foo/method',
'token_auth' => 'MY_TOKEN',
'format' => 'php',
])
->willReturn('a:2:{s:6:"result";s:5:"error";s:7:"message";s:17:"There is an error";}')
$this->expectExceptionMessage('Invalid server response');

$this->connection
->send([
'foo' => 'bar',
'method' => 'foo/method',
'token_auth' => 'MY_TOKEN',
'format' => 'json',
])
->willReturn('')
;

$client = new Client($this->connection->reveal(), 'MY_TOKEN');
Expand Down

0 comments on commit edb931c

Please sign in to comment.