Skip to content

Commit

Permalink
refactor: refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Nov 2, 2024
1 parent d2737d2 commit 7f0dd93
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 68 deletions.
4 changes: 2 additions & 2 deletions tests/Coder/Base64Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function data(): array
#[DataProvider('data')]
public function testEncodes(string $decoded, string $encoded, bool $urlSafe): void
{
self::assertSame(
self::assertBinarySame(
$encoded,
(new Base64())->encode(
$decoded,
Expand All @@ -32,7 +32,7 @@ public function testEncodes(string $decoded, string $encoded, bool $urlSafe): vo
#[DataProvider('data')]
public function testDecodes(string $decoded, string $encoded): void
{
self::assertSame(
self::assertBinarySame(
$decoded,
(new Base64())->decode(
$encoded,
Expand Down
22 changes: 6 additions & 16 deletions tests/Coder/ChecksumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PetrKnap\Binary\Coder;

use PetrKnap\Shorts\Exception\MissingRequirement;
use PHPUnit\Framework\Attributes\DataProvider;

final class ChecksumTest extends CoderTestCase
Expand All @@ -21,9 +20,9 @@ public static function data(): array
#[DataProvider('data')]
public function testEncodes(string $decoded, string $encoded, string $algorithm): void
{
self::assertSame(
self::assertBinarySame(
$encoded,
self::getChecksum()->encode(
(new Checksum())->encode(
$decoded,
algorithm: $algorithm,
),
Expand All @@ -35,7 +34,7 @@ public function testEncodeThrows(string $algorithm): void
{
self::expectException(Exception\CoderCouldNotEncodeData::class);

self::getChecksum()->encode(
(new Checksum())->encode(
self::getDecodedData(),
algorithm: $algorithm,
);
Expand All @@ -51,9 +50,9 @@ public static function dataEncodeThrows(): array
#[DataProvider('data')]
public function testDecodes(string $decoded, string $encoded, string $algorithm): void
{
self::assertSame(
self::assertBinarySame(
$decoded,
self::getChecksum()->decode(
(new Checksum())->decode(
$encoded,
algorithm: $algorithm,
),
Expand All @@ -65,7 +64,7 @@ public function testDecodeThrows(string $data, string $algorithm): void
{
self::expectException(Exception\CoderCouldNotDecodeData::class);

self::getChecksum()->decode(
(new Checksum())->decode(
$data,
algorithm: $algorithm,
);
Expand All @@ -80,13 +79,4 @@ public static function dataDecodeThrows(): array
'wrong checksum' => ['?' . self::getEncodedData(), Checksum::DEFAULT_ALGORITHM],
];
}

private static function getChecksum(): Checksum
{
try {
return new Checksum();
} catch (MissingRequirement $reason) {
self::markTestSkipped($reason->getMessage());
}
}
}
10 changes: 5 additions & 5 deletions tests/Coder/CoderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

abstract class CoderTestCase extends TestCase
{
public const DATA_BASE64 = '2jmj7l5rSw0yVb/vlWAYkK/YBwnaOaPuXmtLDTJVv++VYBiQr9gHCdo5o+5ea0sNMlW/75VgGJCv2AcJ';
protected const DATA_BASE64 = '2jmj7l5rSw0yVb/vlWAYkK/YBwnaOaPuXmtLDTJVv++VYBiQr9gHCdo5o+5ea0sNMlW/75VgGJCv2AcJ';

public static function getDecodedData(): string
abstract public static function data(): array;

protected static function getDecodedData(): string
{
return base64_decode(self::DATA_BASE64);
}

public static function getEncodedData(): string
protected static function getEncodedData(): string
{
foreach (static::data() as $data) {
return $data[1];
}
throw new LogicException('Empty data set.');
}

abstract public static function data(): array;

final protected static function assertBinarySame(string $expected, string $actual): void
{
self::assertSame(bin2hex($expected), bin2hex($actual));
Expand Down
4 changes: 2 additions & 2 deletions tests/Coder/HexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function data(): array
#[DataProvider('data')]
public function testEncodes(string $decoded, string $encoded): void
{
self::assertSame(
self::assertBinarySame(
$encoded,
(new Hex())->encode(
$decoded,
Expand All @@ -32,7 +32,7 @@ public function testEncodes(string $decoded, string $encoded): void
#[DataProvider('data')]
public function testDecodes(string $decoded, string $encoded): void
{
self::assertSame(
self::assertBinarySame(
$decoded,
(new Hex())->decode(
$encoded,
Expand Down
4 changes: 2 additions & 2 deletions tests/Coder/NoCoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function data(): array
#[DataProvider('data')]
public function testEncodes(string $data): void
{
self::assertSame(
self::assertBinarySame(
$data,
(new NoCoder())->encode($data),
);
Expand All @@ -25,7 +25,7 @@ public function testEncodes(string $data): void
#[DataProvider('data')]
public function testDecodes(string $data): void
{
self::assertSame(
self::assertBinarySame(
$data,
(new NoCoder())->decode($data),
);
Expand Down
21 changes: 6 additions & 15 deletions tests/Coder/ZlibTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public static function data(): array
#[DataProvider('data')]
public function testEncodes(string $decoded, string $encoded, int $encoding): void
{
self::assertSame(
self::assertBinarySame(
$encoded,
self::getZlib()->encode(
(new Zlib())->encode(
$decoded,
encoding: $encoding,
),
Expand All @@ -36,7 +36,7 @@ public function testEncodeThrows(int|null $encoding, int|null $level): void
{
self::expectException(Exception\CoderCouldNotEncodeData::class);

self::getZlib()->encode(
(new zlib())->encode(
self::getDecodedData(),
encoding: $encoding,
level: $level,
Expand All @@ -54,9 +54,9 @@ public static function dataEncodeThrows(): array
#[DataProvider('data')]
public function testDecodes(string $decoded, string $encoded): void
{
self::assertSame(
self::assertBinarySame(
$decoded,
self::getZlib()->decode(
(new Zlib())->decode(
$encoded,
),
);
Expand All @@ -67,7 +67,7 @@ public function testDecodeThrows(string $data, int|null $maxLength): void
{
self::expectException(Exception\CoderCouldNotDecodeData::class);

self::getZlib()->decode(
(new Zlib())->decode(
$data,
maxLength: $maxLength,
);
Expand All @@ -80,13 +80,4 @@ public static function dataDecodeThrows(): array
'wrong maximal length' => [base64_decode('AwA='), -1],
];
}

private static function getZlib(): Zlib
{
try {
return new Zlib();
} catch (MissingRequirement $reason) {
self::markTestSkipped($reason->getMessage());
}
}
}
34 changes: 17 additions & 17 deletions tests/Serializer/SerializerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,6 @@

abstract class SerializerTestCase extends TestCase
{
public static function getSerializable(): stdClass
{
$serializable = new stdClass();
$serializable->array = [];
$serializable->binary = 0b0;
$serializable->float = .0;
$serializable->int = 0;
$serializable->null = null;
$serializable->string = '';

return $serializable;
}

abstract public static function getSerialized(): string;

abstract public static function getSerializer(): SerializerInterface;

public function testSerializesSerializable(): void
{
self::assertEquals(
Expand Down Expand Up @@ -56,4 +39,21 @@ public function testSerializeThrowsOnNonserialized(): void

static::getSerializer()->unserialize('?' . static::getSerialized());
}

private static function getSerializable(): stdClass
{
$serializable = new stdClass();
$serializable->array = [];
$serializable->binary = 0b0;
$serializable->float = .0;
$serializable->int = 0;
$serializable->null = null;
$serializable->string = '';

return $serializable;
}

abstract protected static function getSerialized(): string;

abstract protected static function getSerializer(): SerializerInterface;
}
12 changes: 3 additions & 9 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@

namespace PetrKnap\Binary;

use PetrKnap\Shorts\Exception\MissingRequirement;

final class SerializerTest extends Serializer\SerializerTestCase
{
public static function getSerialized(): string
protected static function getSerialized(): string
{
return base64_decode('NYtJCoAwEAT/0i8IuCA9R+/6hhFRAiFCJh4k+HdDwGNVV6+cCMv7HNQMHFmMA6Ep6QNROpbXqsbmo6aqPJ205AiXZsjeuCN8zP/aE/EOAbJI+1pOPp6o4AjI+wE=');
}

public static function getSerializer(): Serializer\SerializerInterface
protected static function getSerializer(): Serializer\SerializerInterface
{
try {
return new Serializer();
} catch (MissingRequirement $reason) {
self::markTestSkipped($reason->getMessage());
}
return new Serializer();
}
}

0 comments on commit 7f0dd93

Please sign in to comment.