-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase62Test.php
More file actions
135 lines (108 loc) · 4.89 KB
/
Base62Test.php
File metadata and controls
135 lines (108 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace TinyBlocks\Encoder;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TinyBlocks\Encoder\Internal\Exceptions\InvalidDecoding;
final class Base62Test extends TestCase
{
#[DataProvider('providerForTestEncode')]
public function testEncode(string $value, string $expected): void
{
/** @Given a string value to encode */
$encoder = Base62::from(value: $value);
/** @When encoding the value using Base62 */
$actual = $encoder->encode();
/** @Then the encoded value should match the expected result */
self::assertEquals($expected, $actual);
}
#[DataProvider('providerForTestDecode')]
public function testDecode(string $value, string $expected): void
{
/** @Given a Base62 encoded string */
$encoder = Base62::from(value: $value);
/** @When decoding the value using Base62 */
$actual = $encoder->decode();
/** @Then the decoded value should match the expected result */
self::assertEquals($expected, $actual);
}
public function testWhenInvalidDecodingBase62(): void
{
$value = hex2bin('9850EEEC191BF4FF26F99315CE43B0C8');
$template = 'The value <%s> could not be decoded.';
$this->expectException(InvalidDecoding::class);
$this->expectExceptionMessage(sprintf($template, $value));
Base62::from(value: $value)->decode();
}
#[DataProvider('providerForTestEncodeAndDecodeWithAllZeroBytes')]
public function testEncodeAndDecodeWithAllZeroBytes(string $value): void
{
/** @Given a binary value containing only zero bytes */
$encoder = Base62::from(value: $value);
/** @When encoding the binary value */
$encoded = $encoder->encode();
/** @When decoding the encoded value */
$decoded = Base62::from(value: $encoded)->decode();
/** @Then the decoded value should match the original binary value */
self::assertEquals($value, $decoded);
}
public function testWhenInvalidDecodingBase62WhenHex2BinFails(): void
{
$value = '\\A';
$template = 'The value <%s> could not be decoded.';
$this->expectException(InvalidDecoding::class);
$this->expectExceptionMessage(sprintf($template, $value));
Base62::from(value: $value)->decode();
}
#[DataProvider('providerForTestEncodeAndDecodeWithLeadingZeroBytes')]
public function testEncodeAndDecodeWithLeadingZeroBytes(string $value): void
{
/** @Given a binary value with leading zero bytes */
$encoder = Base62::from(value: $value);
/** @When encoding the binary value */
$encoded = $encoder->encode();
/** @When decoding the encoded value */
$decoded = Base62::from(value: $encoded)->decode();
/** @Then the decoded value should match the original binary value */
self::assertEquals($value, $decoded);
}
public static function providerForTestEncode(): array
{
return [
'Hello world' => ['value' => 'Hello world!', 'expected' => 'T8dgcjRGuYUueWht'],
'Empty string' => ['value' => '', 'expected' => ''],
'Numeric string' => ['value' => '1234567890', 'expected' => '1A0afZkibIAR2O'],
'Special characters' => ['value' => '@#$%^&*()', 'expected' => 'MjehbVgJedVR']
];
}
public static function providerForTestDecode(): array
{
return [
'Zero value' => ['value' => '0', 'expected' => ''],
'Empty string' => ['value' => '', 'expected' => ''],
'Hello world' => ['value' => 'T8dgcjRGuYUueWht', 'expected' => 'Hello world!'],
'Leading zeros' => ['value' => '000001', 'expected' => hex2bin('000000000001')],
'Two zero bytes' => ['value' => '000', 'expected' => "\x00\x00"],
'Numeric string' => ['value' => '1A0afZkibIAR2O', 'expected' => '1234567890'],
'Single zero byte' => ['value' => '00', 'expected' => "\x00"],
'Single character' => ['value' => '1', 'expected' => "\001"],
'Special characters' => ['value' => 'MjehbVgJedVR', 'expected' => '@#$%^&*()']
];
}
public static function providerForTestEncodeAndDecodeWithAllZeroBytes(): array
{
return [
'Single zero byte' => ['value' => "\x00"],
'Two zero bytes' => ['value' => "\x00\x00"],
'Eight zero bytes' => ['value' => str_repeat("\x00", 8)]
];
}
public static function providerForTestEncodeAndDecodeWithLeadingZeroBytes(): array
{
return [
'Leading zero bytes 01' => ['value' => '001jlt60MnKnB9ECKRt4gl'],
'Leading zero bytes 02' => ['value' => hex2bin('07d8e31da269bf28')],
'Leading zero bytes 03' => ['value' => hex2bin('0000010203040506')]
];
}
}