Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed abstract exceptions & fixed inheritance #8

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions src/Exception/CouldNotCompressData.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/Exception/CouldNotDecompressData.php

This file was deleted.

7 changes: 6 additions & 1 deletion src/Exception/LzmaCouldNotCompressData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace PetrKnap\XzUtils\Exception;

final class LzmaCouldNotCompressData extends CouldNotCompressData implements LzmaException
use PetrKnap\Shorts\Exception\CouldNotProcessData;

/**
* @extends CouldNotProcessData<string>
*/
final class LzmaCouldNotCompressData extends CouldNotProcessData implements LzmaException
{
}
7 changes: 6 additions & 1 deletion src/Exception/LzmaCouldNotDecompressData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace PetrKnap\XzUtils\Exception;

final class LzmaCouldNotDecompressData extends CouldNotDecompressData implements XzException
use PetrKnap\Shorts\Exception\CouldNotProcessData;

/**
* @extends CouldNotProcessData<string>
*/
final class LzmaCouldNotDecompressData extends CouldNotProcessData implements LzmaException
{
}
7 changes: 6 additions & 1 deletion src/Exception/XzCouldNotCompressData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace PetrKnap\XzUtils\Exception;

final class XzCouldNotCompressData extends CouldNotCompressData implements XzException
use PetrKnap\Shorts\Exception\CouldNotProcessData;

/**
* @extends CouldNotProcessData<string>
*/
final class XzCouldNotCompressData extends CouldNotProcessData implements XzException
{
}
7 changes: 6 additions & 1 deletion src/Exception/XzCouldNotDecompressData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace PetrKnap\XzUtils\Exception;

final class XzCouldNotDecompressData extends CouldNotDecompressData implements XzException
use PetrKnap\Shorts\Exception\CouldNotProcessData;

/**
* @extends CouldNotProcessData<string>
*/
final class XzCouldNotDecompressData extends CouldNotProcessData implements XzException
{
}
5 changes: 4 additions & 1 deletion src/Lzma.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ protected static function compressException(): string
return Exception\LzmaCouldNotCompressData::class;
}

protected static function decompressException(): string
/**
* @internal public for testing purposes only
*/
public static function decompressException(): string
{
return Exception\LzmaCouldNotDecompressData::class;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Xz.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ protected static function compressException(): string
return Exception\XzCouldNotCompressData::class;
}

protected static function decompressException(): string
/**
* @internal public for testing purposes only
*/
public static function decompressException(): string
{
return Exception\XzCouldNotDecompressData::class;
}
Expand Down
13 changes: 7 additions & 6 deletions src/XzUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
namespace PetrKnap\XzUtils;

use PetrKnap\ExternalFilter\Exception\FilterException;
use PetrKnap\Shorts\Exception\CouldNotProcessData;

/**
* @internal shared logic
*
* @template TCompressException of Exception\CouldNotCompressData
* @template TDecompressException of Exception\CouldNotDecompressData
* @template TCompressException of Exception\Exception
* @template TDecompressException of Exception\Exception
*/
abstract class XzUtils
{
/**
* @param array<non-empty-string>|null $options
*
* @throws TCompressException&Exception\CouldNotCompressData
* @throws TCompressException
*/
final public function compress(
string $data,
Expand All @@ -34,7 +35,7 @@ final public function compress(
/**
* @param array<non-empty-string>|null $options
*
* @throws TDecompressException&Exception\CouldNotDecompressData
* @throws TDecompressException
*/
final public function decompress(
string $data,
Expand All @@ -48,12 +49,12 @@ final public function decompress(
}

/**
* @return class-string<TCompressException&Exception\CouldNotCompressData>
* @return class-string<TCompressException&CouldNotProcessData<string>>
*/
abstract protected static function compressException(): string;

/**
* @return class-string<TDecompressException&Exception\CouldNotDecompressData>
* @return class-string<TDecompressException&CouldNotProcessData<string>>
*/
abstract protected static function decompressException(): string;

Expand Down
16 changes: 13 additions & 3 deletions tests/XzUtilsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace PetrKnap\XzUtils;

use PetrKnap\Shorts\Exception\CouldNotProcessData;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Throwable;

#[CoversClass(FilterFactory::class)]
abstract class XzUtilsTestCase extends TestCase
Expand All @@ -14,16 +16,16 @@
private const DATA_B64 = 'h/J4V2gAK1njeEOqyTiTjg==';

#[DataProvider('data')]
final public function testCompressesData(string $decompressed, string $compressed, int|null $compressionPreset): void

Check notice on line 19 in tests/XzUtilsTestCase.php

View workflow job for this annotation

GitHub Actions / run

* [Line length] Line exceeds maximum limit of 100 characters; contains 121 characters
{
self::assertSame(
bin2hex($compressed),
bin2hex(static::getInstance()->compress($decompressed, $compressionPreset)),

Check notice on line 23 in tests/XzUtilsTestCase.php

View workflow job for this annotation

GitHub Actions / run

* [Line length] Line exceeds 80 characters; contains 88 characters
);
}

#[DataProvider('data')]
final public function testDecompressesData(string $decompressed, string $compressed): void

Check notice on line 28 in tests/XzUtilsTestCase.php

View workflow job for this annotation

GitHub Actions / run

* [Line length] Line exceeds 80 characters; contains 94 characters
{
self::assertSame(
bin2hex($decompressed),
Expand All @@ -31,14 +33,22 @@
);
}

final public function testDecompressThrowsOnWrongData(): void
final public function testDecompressThrowsCorrectlyOnWrongData(): void
{
self::expectException(Exception\CouldNotDecompressData::class);
self::expectException(Exception\Exception::class);

static::getInstance()->decompress('?');
try {
static::getInstance()->decompress('wrong data');
} catch (Throwable $throwable) {
self::assertInstanceOf(call_user_func([static::getInstance()::class, 'decompressException']), $throwable);

Check notice on line 43 in tests/XzUtilsTestCase.php

View workflow job for this annotation

GitHub Actions / run

* [Line length] Line exceeds maximum limit of 100 characters; contains 118 characters
/** @var CouldNotProcessData $throwable */

Check notice on line 44 in tests/XzUtilsTestCase.php

View workflow job for this annotation

GitHub Actions / run

* [Inline doc comment declaration] Missing variable $throwable before or after the documentation comment.
self::assertSame('wrong data', $throwable->getData());

throw $throwable;
}
}

public static function data(): array

Check notice on line 51 in tests/XzUtilsTestCase.php

View workflow job for this annotation

GitHub Actions / run

* [Return type hint] Method \PetrKnap\XzUtils\XzUtilsTestCase::data() does not have @return annotation for its traversable return value.
{
$decompressed = base64_decode(self::DATA_B64);
$compressed = static::getCompressedDataAsBase64();
Expand Down
Loading