Skip to content

Commit

Permalink
improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Feb 10, 2024
1 parent 0aee937 commit 9e9e092
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
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
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
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
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 9e9e092

Please sign in to comment.