Skip to content

Commit

Permalink
tests: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfensdrfer committed Jun 12, 2023
1 parent 8330f8c commit 6724b28
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
29 changes: 14 additions & 15 deletions src/Matomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;
use JsonException;

Expand Down Expand Up @@ -568,23 +567,25 @@ private function _request(
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}

$contents = $response->getBody()->getContents();

// Validate if the response was successful
if ($response->getStatusCode() !== 200) {
throw new InvalidRequestException($response->getBody()->getContents(),
throw new InvalidRequestException($contents,
$response->getStatusCode());
}

// Sometimes the response was unsuccessful, but the status code was 200
if ($format === self::FORMAT_JSON) {
$valid = $this->_isValidResponse($response);
$valid = $this->_isValidResponse($contents);

if ($valid !== true) {
throw new InvalidResponseException($valid.' ('.$this->_parseUrl($method, $params)
.')', 403);
}
}

return $this->_parseResponse($response, $format);
return $this->_parseResponse($contents, $format);
}

/**
Expand Down Expand Up @@ -654,20 +655,18 @@ private function _parseUrl(string $method, array $params = []): string
/**
* Check if the request was successful.
*
* @param mixed $response
* @param string $contents
*
* @return bool|string
* @throws JsonException
*/
private function _isValidResponse(Response $response): bool|string
private function _isValidResponse(string $contents): bool|string
{
$body = $response->getBody()->getContents();

if ($body === '') {
if ($contents === '') {
return 'Empty response!';
}

$result = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
$result = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);

if (isset($result['result']) && (strtolower($result['result']) === 'error')) {
return $result['message'];
Expand All @@ -679,21 +678,21 @@ private function _isValidResponse(Response $response): bool|string
/**
* Parse request result
*
* @param Response $response
* @param string $contents
* @param string|null $overrideFormat Override the default format
*
* @return mixed Either the parsed response body object (parsed from json) or the raw response object.
* @throws JsonException
*/
private function _parseResponse(Response $response, string $overrideFormat = null): mixed
private function _parseResponse(string $contents, string $overrideFormat = null): mixed
{
$format = $overrideFormat ?? $this->_format;

return match ($format) {
self::FORMAT_JSON => json_decode($response->getBody()->getContents(),
self::FORMAT_JSON => json_decode($contents,
$this->_isJsonDecodeAssoc, 512,
JSON_THROW_ON_ERROR),
default => $response,
default => $contents,
};
}

Expand Down Expand Up @@ -2677,7 +2676,7 @@ public function getVisitsUntilConversion(
* value without '#'
* @param array $optional
*
* @return bool|object
* @return bool|string
* @@throws InvalidRequestException|JsonException|InvalidResponseException
*/
public function getImageGraph(
Expand Down
14 changes: 7 additions & 7 deletions tests/MatomoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ public function testNoPeriodOrDate(): void
*/
public function testInvalidAccessToken(): void
{
$this->expectException(InvalidResponseException::class);

$this->_matomo->setToken('403');
$this->assertTrue(true);

$this->_matomo->getVisitsSummary();

$this->fail('No exception thrown.');
}

/**
Expand Down Expand Up @@ -262,11 +262,11 @@ public function testEmptySiteId(): void
public function testGetImageGraph(): void
{
/**
* @var $response Response
* @var $imageBinaryString string
*/
$response = $this->_matomo->getImageGraph('UserCountry', 'getCountry');
$this->assertIsObject($response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('PNG', $response->getBody());
$imageBinaryString = $this->_matomo->getImageGraph('UserCountry', 'getCountry');

$this->assertIsString($imageBinaryString);
$this->assertStringContainsString('PNG', $imageBinaryString);
}
}

0 comments on commit 6724b28

Please sign in to comment.