Skip to content

Commit 8330f8c

Browse files
committed
fix: update tests
1 parent bccaf53 commit 8330f8c

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
config.php
22
composer.lock
33
.phpunit.result.cache
4+
test-results
45

56
.idea/
67
/build/

phpunit.xml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage processUncoveredFiles="true">
4-
<include>
5-
<directory suffix=".php">./src</directory>
6-
</include>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="tests/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage>
74
<report>
85
<html outputDirectory="build/coverage/"/>
96
</report>
@@ -12,4 +9,9 @@
129
<directory>tests</directory>
1310
</testsuite>
1411
<logging/>
12+
<source>
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</source>
1517
</phpunit>

src/Matomo.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
*/
1515
class Matomo
1616
{
17-
public const ERROR_EMPTY = 11;
18-
1917
public const PERIOD_DAY = 'day';
2018
public const PERIOD_WEEK = 'week';
2119
public const PERIOD_MONTH = 'month';
@@ -539,7 +537,7 @@ public function reset(): Matomo
539537
* @param string $method HTTP method
540538
* @param array $params Query parameters
541539
* @param array $optional Optional arguments for this api call
542-
* @param string|null $format Override the response format
540+
* @param string|null $overrideFormat Override the response format
543541
*
544542
* @return bool|object
545543
* @@throws InvalidRequestException|JsonException|InvalidResponseException
@@ -549,13 +547,15 @@ private function _request(
549547
string $method,
550548
array $params = [],
551549
array $optional = [],
552-
string $format = null
550+
string $overrideFormat = null
553551
): mixed {
554552
$url = $this->_parseUrl($method, $params + $optional);
555553
if ($url === '') {
556554
throw new InvalidRequestException('Could not parse URL!');
557555
}
558556

557+
$format = $overrideFormat ?? $this->_format;
558+
559559
try {
560560
$response = $this->_client->get($url, [
561561
'verify' => $this->_verifySsl,
@@ -570,12 +570,14 @@ private function _request(
570570

571571
// Validate if the response was successful
572572
if ($response->getStatusCode() !== 200) {
573-
throw new InvalidRequestException($response->getBody(), $response->getStatusCode());
573+
throw new InvalidRequestException($response->getBody()->getContents(),
574+
$response->getStatusCode());
574575
}
575576

576577
// Sometimes the response was unsuccessful, but the status code was 200
577578
if ($format === self::FORMAT_JSON) {
578579
$valid = $this->_isValidResponse($response);
580+
579581
if ($valid !== true) {
580582
throw new InvalidResponseException($valid.' ('.$this->_parseUrl($method, $params)
581583
.')', 403);
@@ -654,22 +656,24 @@ private function _parseUrl(string $method, array $params = []): string
654656
*
655657
* @param mixed $response
656658
*
657-
* @return bool|int
659+
* @return bool|string
658660
* @throws JsonException
659661
*/
660-
private function _isValidResponse(Response $response): bool|int
662+
private function _isValidResponse(Response $response): bool|string
661663
{
662-
if (is_null($response->getRawBody())) {
663-
return self::ERROR_EMPTY;
664+
$body = $response->getBody()->getContents();
665+
666+
if ($body === '') {
667+
return 'Empty response!';
664668
}
665669

666-
$result = json_decode($response->getRawBody(), true, 512, JSON_THROW_ON_ERROR);
670+
$result = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
667671

668-
if ( ! isset($result['result']) || ($result['result'] !== 'error')) {
669-
return true;
672+
if (isset($result['result']) && (strtolower($result['result']) === 'error')) {
673+
return $result['message'];
670674
}
671675

672-
return $result['message'];
676+
return true;
673677
}
674678

675679
/**
@@ -686,7 +690,8 @@ private function _parseResponse(Response $response, string $overrideFormat = nul
686690
$format = $overrideFormat ?? $this->_format;
687691

688692
return match ($format) {
689-
self::FORMAT_JSON => json_decode($response->getBody(), $this->_isJsonDecodeAssoc, 512,
693+
self::FORMAT_JSON => json_decode($response->getBody()->getContents(),
694+
$this->_isJsonDecodeAssoc, 512,
690695
JSON_THROW_ON_ERROR),
691696
default => $response,
692697
};

tests/MatomoTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,16 @@ public function testNoPeriodOrDate(): void
178178
/**
179179
* Test that an exception is thrown with an invalid access token.
180180
*
181-
* @throws JsonException|InvalidResponseException
181+
* @throws JsonException|InvalidRequestException|InvalidResponseException
182182
*/
183183
public function testInvalidAccessToken(): void
184184
{
185185
$this->_matomo->setToken('403');
186+
$this->assertTrue(true);
186187

187-
try {
188-
$this->_matomo->getVisitsSummary();
189-
} catch (InvalidRequestException $e) {
190-
$this->assertEquals(403, $e->getCode());
191-
}
188+
$this->_matomo->getVisitsSummary();
189+
190+
$this->fail('No exception thrown.');
192191
}
193192

194193
/**

0 commit comments

Comments
 (0)