Skip to content

Commit 6724b28

Browse files
committed
tests: fix tests
1 parent 8330f8c commit 6724b28

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/Matomo.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Exception\GuzzleException;
7-
use GuzzleHttp\Psr7\Response;
87
use InvalidArgumentException;
98
use JsonException;
109

@@ -568,23 +567,25 @@ private function _request(
568567
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
569568
}
570569

570+
$contents = $response->getBody()->getContents();
571+
571572
// Validate if the response was successful
572573
if ($response->getStatusCode() !== 200) {
573-
throw new InvalidRequestException($response->getBody()->getContents(),
574+
throw new InvalidRequestException($contents,
574575
$response->getStatusCode());
575576
}
576577

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

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

587-
return $this->_parseResponse($response, $format);
588+
return $this->_parseResponse($contents, $format);
588589
}
589590

590591
/**
@@ -654,20 +655,18 @@ private function _parseUrl(string $method, array $params = []): string
654655
/**
655656
* Check if the request was successful.
656657
*
657-
* @param mixed $response
658+
* @param string $contents
658659
*
659660
* @return bool|string
660661
* @throws JsonException
661662
*/
662-
private function _isValidResponse(Response $response): bool|string
663+
private function _isValidResponse(string $contents): bool|string
663664
{
664-
$body = $response->getBody()->getContents();
665-
666-
if ($body === '') {
665+
if ($contents === '') {
667666
return 'Empty response!';
668667
}
669668

670-
$result = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
669+
$result = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
671670

672671
if (isset($result['result']) && (strtolower($result['result']) === 'error')) {
673672
return $result['message'];
@@ -679,21 +678,21 @@ private function _isValidResponse(Response $response): bool|string
679678
/**
680679
* Parse request result
681680
*
682-
* @param Response $response
681+
* @param string $contents
683682
* @param string|null $overrideFormat Override the default format
684683
*
685684
* @return mixed Either the parsed response body object (parsed from json) or the raw response object.
686685
* @throws JsonException
687686
*/
688-
private function _parseResponse(Response $response, string $overrideFormat = null): mixed
687+
private function _parseResponse(string $contents, string $overrideFormat = null): mixed
689688
{
690689
$format = $overrideFormat ?? $this->_format;
691690

692691
return match ($format) {
693-
self::FORMAT_JSON => json_decode($response->getBody()->getContents(),
692+
self::FORMAT_JSON => json_decode($contents,
694693
$this->_isJsonDecodeAssoc, 512,
695694
JSON_THROW_ON_ERROR),
696-
default => $response,
695+
default => $contents,
697696
};
698697
}
699698

@@ -2677,7 +2676,7 @@ public function getVisitsUntilConversion(
26772676
* value without '#'
26782677
* @param array $optional
26792678
*
2680-
* @return bool|object
2679+
* @return bool|string
26812680
* @@throws InvalidRequestException|JsonException|InvalidResponseException
26822681
*/
26832682
public function getImageGraph(

tests/MatomoTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ public function testNoPeriodOrDate(): void
182182
*/
183183
public function testInvalidAccessToken(): void
184184
{
185+
$this->expectException(InvalidResponseException::class);
186+
185187
$this->_matomo->setToken('403');
186188
$this->assertTrue(true);
187189

188190
$this->_matomo->getVisitsSummary();
189-
190-
$this->fail('No exception thrown.');
191191
}
192192

193193
/**
@@ -262,11 +262,11 @@ public function testEmptySiteId(): void
262262
public function testGetImageGraph(): void
263263
{
264264
/**
265-
* @var $response Response
265+
* @var $imageBinaryString string
266266
*/
267-
$response = $this->_matomo->getImageGraph('UserCountry', 'getCountry');
268-
$this->assertIsObject($response);
269-
$this->assertEquals(200, $response->getStatusCode());
270-
$this->assertStringContainsString('PNG', $response->getBody());
267+
$imageBinaryString = $this->_matomo->getImageGraph('UserCountry', 'getCountry');
268+
269+
$this->assertIsString($imageBinaryString);
270+
$this->assertStringContainsString('PNG', $imageBinaryString);
271271
}
272272
}

0 commit comments

Comments
 (0)