Skip to content

Commit

Permalink
Merge pull request #147 from Sysix/coverage
Browse files Browse the repository at this point in the history
Coverage improvments
  • Loading branch information
Sysix authored Feb 11, 2024
2 parents b515170 + cd8efba commit ee7a990
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
/.php-cs-fixer.dist.php export-ignore
/psalm.xml export-ignore
/docker-compose.yml export-ignore
/Dockerfile export-ignore
/Dockerfile export-ignore
/coverage.svg export-ignore
16 changes: 15 additions & 1 deletion .github/workflows/coverage-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,18 @@ jobs:
uses: lucassabreu/comment-coverage-clover@main
with:
file: coverage.xml
base-file: test/base/coverage.xml
base-file: test/base/coverage.xml

- if: ${{ github.event_name != 'pull_request' }}
uses: action-badges/[email protected]
with:
branch-name: badges

- if: ${{ github.event_name != 'pull_request' }}
name: Make Coverage Badge
uses: action-badges/[email protected]
with:
file-name: coverage.svg
badge-branch: badges
github-token: '${{ secrets.GITHUB_TOKEN }}'
coverage-file-name: ./coverage.xml
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Lexoffice PHP API

![tests](https://github.com/sysix/lexoffice-php-api/workflows/tests/badge.svg)
![coverage](https://raw.githubusercontent.com/sysix/lexoffice-php-api/badges/.badges/development/coverage.svg)
[![Latest Stable Version](https://poser.pugx.org/sysix/lex-office-api/v)](//packagist.org/packages/sysix/lex-office-api)
[![License](https://poser.pugx.org/sysix/lex-office-api/license)](//packagist.org/packages/sysix/lex-office-api)

Expand Down
2 changes: 2 additions & 0 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function __construct(

/**
* @deprecated 1.0 use Sysix\LexOffice\Utils::getJsonFromResponse()
*
* @codeCoverageIgnore
*/
public function getAsJson(ResponseInterface $response): mixed
{
Expand Down
7 changes: 6 additions & 1 deletion src/PaginationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ public function getAll(): ResponseInterface
// update content to get all contacts
for ($i = 1; $i < $result->totalPages; $i++) {
$responsePage = $this->getPage($i);

if ($responsePage->getStatusCode() !== 200) {
return $responsePage;
}

/** @var ?stdClass{totalPages:int, content:stdClass[]} $resultPage */
$resultPage = Utils::getJsonFromResponse($responsePage);

if ($resultPage === null) {
continue;
return $responsePage;
}

foreach ($resultPage->content as $entity) {
Expand Down
19 changes: 14 additions & 5 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class Utils
{
public static function getJsonFromResponse(ResponseInterface $response, bool $assoc = false): mixed
{
$body = $response->getBody()->__toString();

if (str_contains($response->getHeaderLine('Content-Type'), 'application/json')) {
return self::jsonDecode($body, $assoc);
if (!str_contains($response->getHeaderLine('Content-Type'), 'application/json')) {
return null;
}

return null;
$body = $response->getBody()->__toString();

return self::jsonDecode($body, $assoc);
}

/**
Expand All @@ -40,6 +40,8 @@ public static function streamFor(string $resource = '', array $options = []): St
}

/**
* @internal
*
* @param int<1, 2147483647> $depth
*/
public static function jsonEncode(mixed $value, int $options = 0, int $depth = 512): string
Expand All @@ -53,6 +55,8 @@ public static function jsonEncode(mixed $value, int $options = 0, int $depth = 5
}

/**
* @internal
*
* @param int<1, 2147483647> $depth
*/
public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0): mixed
Expand All @@ -65,12 +69,17 @@ public static function jsonDecode(string $json, bool $assoc = false, int $depth
return $data;
}

/**
* @internal
*/
public static function createStream(mixed $content): StreamInterface
{
return self::streamFor(self::jsonEncode($content));
}

/**
* @internal
*
* @param array<string, string|bool|resource> $content
*/
public static function createMultipartStream(array $content, string $boundary = null): MultipartStream
Expand Down
10 changes: 10 additions & 0 deletions tests/Clients/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sysix\LexOffice\Tests\Clients;

use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\File;
use Sysix\LexOffice\Config\FileClient\VoucherConfig;
Expand Down Expand Up @@ -37,6 +38,15 @@ public function testGetWithAccceptHeader(): void
$this->assertEquals('application/xml', $api->getRequest()->getHeaderLine('Accept'));
}

public function testUploadNotSupportedType(): void
{
$this->expectException(InvalidArgumentException::class);

[, $stub] = $this->createClientMockObject(File::class);

$stub->upload('somefile.jpg', 'invalid-type');
}

public function testUploadNotSupportedExtension(): void
{
$this->expectException(LexOfficeApiException::class);
Expand Down
9 changes: 0 additions & 9 deletions tests/DeprecationException.php

This file was deleted.

14 changes: 14 additions & 0 deletions tests/PaginationClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public function testGetAllMultiple(): void
);
}

public function testGetMultipleWithError(): void
{
$this->expectDeprecationV1Warning('getAll');

[, $stub] = $this->createPaginationClientMockObject(
[
new Response(200, ['Content-Type' => 'application/json'], '{"content": [{"name": "a"}], "totalPages": 3}'),
new Response(500)
]
);

$this->assertEquals(500, $stub->getAll()->getStatusCode());
}

public function testGetPage(): void
{
[$api, $stub] = $this->createPaginationClientMockObject(
Expand Down
22 changes: 17 additions & 5 deletions tests/TestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@

class TestClient extends TestCase
{
/** @var string[] $userGeneratedWarnings */
protected array $userGeneratedWarnings = [];

/** @var string[] $expectedUserGeneratedWarnings */
protected array $expectedUserGeneratedWarnings = [];

protected function tearDown(): void
{
// see self::expectDeprecationV1Warning()
restore_error_handler();

if (!empty($this->expectedUserGeneratedWarnings)) {
// we really only care about the first message
// we can not check for full class names, because we are mocking objects
$this->assertStringContainsString($this->expectedUserGeneratedWarnings[0], $this->userGeneratedWarnings[0]);
}
}
/**
* @return Api&MockObject
Expand Down Expand Up @@ -98,13 +110,13 @@ public function getCacheDir(): string

public function expectDeprecationV1Warning(string $method): void
{
set_error_handler(static function (int $errno, string $errstr): void {
throw new DeprecationException($errstr, $errno);
$self = $this;
set_error_handler(static function (int $errno, string $errstr) use ($self): bool {
$self->userGeneratedWarnings[] = $errstr;
return true;
}, E_USER_DEPRECATED);

$this->expectException(DeprecationException::class);

// we can not check for full class names, because we are mocking objects
$this->expectExceptionMessage('::' . $method . ' should not be called anymore, in future versions this method WILL not exist');
$this->expectedUserGeneratedWarnings[] = '::' . $method . ' should not be called anymore, in future versions this method WILL not exist';
}
}
39 changes: 39 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sysix\LexOffice\Tests;

use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Sysix\LexOffice\Utils;

Expand Down Expand Up @@ -55,4 +56,42 @@ public function testGetJsonFromResponseWithValidCharsetHeader(): void
'test' => true
], $json);
}

public function testJsonDecodeValid(): void
{
$json = Utils::jsonDecode('{"content":"test","object":{"content":"test2"}}');

$this->assertEquals($json, (object)[
'content' => 'test',
'object' => (object) [
'content' => 'test2'
]
]);
}

public function testJsonDecodeInValid(): void
{
$this->expectException(InvalidArgumentException::class);

Utils::jsonDecode('{content:"test"}'); // missing quotes for key
}

public function testJsonEncodeValid(): void
{
$jsonString = Utils::jsonEncode((object)[
'content' => 'test',
'object' => (object) [
'content' => 'test2'
]
]);

$this->assertEquals('{"content":"test","object":{"content":"test2"}}', $jsonString);
}

public function testJsonEncodeInValid(): void
{
$this->expectException(InvalidArgumentException::class);

Utils::jsonEncode(fopen('php://temp', 'r'));
}
}

0 comments on commit ee7a990

Please sign in to comment.