Skip to content

Commit 7f0dd93

Browse files
committed
refactor: refactored tests
1 parent d2737d2 commit 7f0dd93

File tree

8 files changed

+43
-68
lines changed

8 files changed

+43
-68
lines changed

tests/Coder/Base64Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function data(): array
2020
#[DataProvider('data')]
2121
public function testEncodes(string $decoded, string $encoded, bool $urlSafe): void
2222
{
23-
self::assertSame(
23+
self::assertBinarySame(
2424
$encoded,
2525
(new Base64())->encode(
2626
$decoded,
@@ -32,7 +32,7 @@ public function testEncodes(string $decoded, string $encoded, bool $urlSafe): vo
3232
#[DataProvider('data')]
3333
public function testDecodes(string $decoded, string $encoded): void
3434
{
35-
self::assertSame(
35+
self::assertBinarySame(
3636
$decoded,
3737
(new Base64())->decode(
3838
$encoded,

tests/Coder/ChecksumTest.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PetrKnap\Binary\Coder;
66

7-
use PetrKnap\Shorts\Exception\MissingRequirement;
87
use PHPUnit\Framework\Attributes\DataProvider;
98

109
final class ChecksumTest extends CoderTestCase
@@ -21,9 +20,9 @@ public static function data(): array
2120
#[DataProvider('data')]
2221
public function testEncodes(string $decoded, string $encoded, string $algorithm): void
2322
{
24-
self::assertSame(
23+
self::assertBinarySame(
2524
$encoded,
26-
self::getChecksum()->encode(
25+
(new Checksum())->encode(
2726
$decoded,
2827
algorithm: $algorithm,
2928
),
@@ -35,7 +34,7 @@ public function testEncodeThrows(string $algorithm): void
3534
{
3635
self::expectException(Exception\CoderCouldNotEncodeData::class);
3736

38-
self::getChecksum()->encode(
37+
(new Checksum())->encode(
3938
self::getDecodedData(),
4039
algorithm: $algorithm,
4140
);
@@ -51,9 +50,9 @@ public static function dataEncodeThrows(): array
5150
#[DataProvider('data')]
5251
public function testDecodes(string $decoded, string $encoded, string $algorithm): void
5352
{
54-
self::assertSame(
53+
self::assertBinarySame(
5554
$decoded,
56-
self::getChecksum()->decode(
55+
(new Checksum())->decode(
5756
$encoded,
5857
algorithm: $algorithm,
5958
),
@@ -65,7 +64,7 @@ public function testDecodeThrows(string $data, string $algorithm): void
6564
{
6665
self::expectException(Exception\CoderCouldNotDecodeData::class);
6766

68-
self::getChecksum()->decode(
67+
(new Checksum())->decode(
6968
$data,
7069
algorithm: $algorithm,
7170
);
@@ -80,13 +79,4 @@ public static function dataDecodeThrows(): array
8079
'wrong checksum' => ['?' . self::getEncodedData(), Checksum::DEFAULT_ALGORITHM],
8180
];
8281
}
83-
84-
private static function getChecksum(): Checksum
85-
{
86-
try {
87-
return new Checksum();
88-
} catch (MissingRequirement $reason) {
89-
self::markTestSkipped($reason->getMessage());
90-
}
91-
}
9282
}

tests/Coder/CoderTestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99

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

14-
public static function getDecodedData(): string
14+
abstract public static function data(): array;
15+
16+
protected static function getDecodedData(): string
1517
{
1618
return base64_decode(self::DATA_BASE64);
1719
}
1820

19-
public static function getEncodedData(): string
21+
protected static function getEncodedData(): string
2022
{
2123
foreach (static::data() as $data) {
2224
return $data[1];
2325
}
2426
throw new LogicException('Empty data set.');
2527
}
2628

27-
abstract public static function data(): array;
28-
2929
final protected static function assertBinarySame(string $expected, string $actual): void
3030
{
3131
self::assertSame(bin2hex($expected), bin2hex($actual));

tests/Coder/HexTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function data(): array
2121
#[DataProvider('data')]
2222
public function testEncodes(string $decoded, string $encoded): void
2323
{
24-
self::assertSame(
24+
self::assertBinarySame(
2525
$encoded,
2626
(new Hex())->encode(
2727
$decoded,
@@ -32,7 +32,7 @@ public function testEncodes(string $decoded, string $encoded): void
3232
#[DataProvider('data')]
3333
public function testDecodes(string $decoded, string $encoded): void
3434
{
35-
self::assertSame(
35+
self::assertBinarySame(
3636
$decoded,
3737
(new Hex())->decode(
3838
$encoded,

tests/Coder/NoCoderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function data(): array
1616
#[DataProvider('data')]
1717
public function testEncodes(string $data): void
1818
{
19-
self::assertSame(
19+
self::assertBinarySame(
2020
$data,
2121
(new NoCoder())->encode($data),
2222
);
@@ -25,7 +25,7 @@ public function testEncodes(string $data): void
2525
#[DataProvider('data')]
2626
public function testDecodes(string $data): void
2727
{
28-
self::assertSame(
28+
self::assertBinarySame(
2929
$data,
3030
(new NoCoder())->decode($data),
3131
);

tests/Coder/ZlibTest.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static function data(): array
2222
#[DataProvider('data')]
2323
public function testEncodes(string $decoded, string $encoded, int $encoding): void
2424
{
25-
self::assertSame(
25+
self::assertBinarySame(
2626
$encoded,
27-
self::getZlib()->encode(
27+
(new Zlib())->encode(
2828
$decoded,
2929
encoding: $encoding,
3030
),
@@ -36,7 +36,7 @@ public function testEncodeThrows(int|null $encoding, int|null $level): void
3636
{
3737
self::expectException(Exception\CoderCouldNotEncodeData::class);
3838

39-
self::getZlib()->encode(
39+
(new zlib())->encode(
4040
self::getDecodedData(),
4141
encoding: $encoding,
4242
level: $level,
@@ -54,9 +54,9 @@ public static function dataEncodeThrows(): array
5454
#[DataProvider('data')]
5555
public function testDecodes(string $decoded, string $encoded): void
5656
{
57-
self::assertSame(
57+
self::assertBinarySame(
5858
$decoded,
59-
self::getZlib()->decode(
59+
(new Zlib())->decode(
6060
$encoded,
6161
),
6262
);
@@ -67,7 +67,7 @@ public function testDecodeThrows(string $data, int|null $maxLength): void
6767
{
6868
self::expectException(Exception\CoderCouldNotDecodeData::class);
6969

70-
self::getZlib()->decode(
70+
(new Zlib())->decode(
7171
$data,
7272
maxLength: $maxLength,
7373
);
@@ -80,13 +80,4 @@ public static function dataDecodeThrows(): array
8080
'wrong maximal length' => [base64_decode('AwA='), -1],
8181
];
8282
}
83-
84-
private static function getZlib(): Zlib
85-
{
86-
try {
87-
return new Zlib();
88-
} catch (MissingRequirement $reason) {
89-
self::markTestSkipped($reason->getMessage());
90-
}
91-
}
9283
}

tests/Serializer/SerializerTestCase.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,6 @@
99

1010
abstract class SerializerTestCase extends TestCase
1111
{
12-
public static function getSerializable(): stdClass
13-
{
14-
$serializable = new stdClass();
15-
$serializable->array = [];
16-
$serializable->binary = 0b0;
17-
$serializable->float = .0;
18-
$serializable->int = 0;
19-
$serializable->null = null;
20-
$serializable->string = '';
21-
22-
return $serializable;
23-
}
24-
25-
abstract public static function getSerialized(): string;
26-
27-
abstract public static function getSerializer(): SerializerInterface;
28-
2912
public function testSerializesSerializable(): void
3013
{
3114
self::assertEquals(
@@ -56,4 +39,21 @@ public function testSerializeThrowsOnNonserialized(): void
5639

5740
static::getSerializer()->unserialize('?' . static::getSerialized());
5841
}
42+
43+
private static function getSerializable(): stdClass
44+
{
45+
$serializable = new stdClass();
46+
$serializable->array = [];
47+
$serializable->binary = 0b0;
48+
$serializable->float = .0;
49+
$serializable->int = 0;
50+
$serializable->null = null;
51+
$serializable->string = '';
52+
53+
return $serializable;
54+
}
55+
56+
abstract protected static function getSerialized(): string;
57+
58+
abstract protected static function getSerializer(): SerializerInterface;
5959
}

tests/SerializerTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44

55
namespace PetrKnap\Binary;
66

7-
use PetrKnap\Shorts\Exception\MissingRequirement;
8-
97
final class SerializerTest extends Serializer\SerializerTestCase
108
{
11-
public static function getSerialized(): string
9+
protected static function getSerialized(): string
1210
{
1311
return base64_decode('NYtJCoAwEAT/0i8IuCA9R+/6hhFRAiFCJh4k+HdDwGNVV6+cCMv7HNQMHFmMA6Ep6QNROpbXqsbmo6aqPJ205AiXZsjeuCN8zP/aE/EOAbJI+1pOPp6o4AjI+wE=');
1412
}
1513

16-
public static function getSerializer(): Serializer\SerializerInterface
14+
protected static function getSerializer(): Serializer\SerializerInterface
1715
{
18-
try {
19-
return new Serializer();
20-
} catch (MissingRequirement $reason) {
21-
self::markTestSkipped($reason->getMessage());
22-
}
16+
return new Serializer();
2317
}
2418
}

0 commit comments

Comments
 (0)