Repository navigation
Expand file tree
/
Copy pathBase62Test.php
More file actions
134 lines (109 loc) · 5.39 KB
/
Copy pathBase62Test.php
File metadata and controls
134 lines (109 loc) · 5.39 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
<?php
declare(strict_types=1);
namespace Test\TinyBlocks\Encoder;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TinyBlocks\Encoder\Base62;
use TinyBlocks\Encoder\Exceptions\InvalidDecoding;
final class Base62Test extends TestCase
{
#[DataProvider('providerForDecoding')]
public function testWhenDecodingThenReturnsOriginalValue(string $value, string $expected): void
{
/** @Given a Base62 encoded string */
$encoded = $value;
/** @When decoding the encoded string using Base62 */
$actual = Base62::from(value: $encoded)->decode();
/** @Then the decoded value matches the original payload */
self::assertEquals($expected, $actual);
}
#[DataProvider('providerForEncoding')]
public function testWhenEncodingThenReturnsBase62Representation(string $value, string $expected): void
{
/** @Given a raw string value */
$payload = $value;
/** @When encoding the payload using Base62 */
$actual = Base62::from(value: $payload)->encode();
/** @Then the encoded representation matches the expected Base62 string */
self::assertEquals($expected, $actual);
}
#[DataProvider('providerForRoundTripWithAllZeroBytes')]
public function testWhenRoundTrippingAllZeroBytesThenReturnsOriginalPayload(string $value): void
{
/** @Given a binary payload containing only zero bytes */
$payload = $value;
/** @When round-tripping the payload through encode and decode */
$decoded = Base62::from(value: Base62::from(value: $payload)->encode())->decode();
/** @Then the decoded payload matches the original binary payload */
self::assertEquals($payload, $decoded);
}
public function testWhenDecodingValueWithBackslashThenInvalidDecodingIsThrown(): void
{
/** @Given a payload containing a backslash that is not in the Base62 alphabet */
$payload = '\\A';
/** @Then an InvalidDecoding exception describing the payload should be thrown */
$this->expectException(InvalidDecoding::class);
$this->expectExceptionMessage(sprintf('The value <%s> could not be decoded.', $payload));
/** @When attempting to decode the invalid payload */
Base62::from(value: $payload)->decode();
}
#[DataProvider('providerForRoundTripWithLeadingZeroBytes')]
public function testWhenRoundTrippingPayloadWithLeadingZeroBytesThenReturnsOriginalPayload(string $value): void
{
/** @Given a binary payload with leading zero bytes */
$payload = $value;
/** @When round-tripping the payload through encode and decode */
$decoded = Base62::from(value: Base62::from(value: $payload)->encode())->decode();
/** @Then the decoded payload matches the original binary payload */
self::assertEquals($payload, $decoded);
}
public function testWhenDecodingValueWithCharactersOutsideAlphabetThenInvalidDecodingIsThrown(): void
{
/** @Given a binary payload containing characters outside the Base62 alphabet */
$payload = hex2bin('9850EEEC191BF4FF26F99315CE43B0C8');
/** @Then an InvalidDecoding exception describing the payload should be thrown */
$this->expectException(InvalidDecoding::class);
$this->expectExceptionMessage(sprintf('The value <%s> could not be decoded.', $payload));
/** @When attempting to decode the invalid payload */
Base62::from(value: $payload)->decode();
}
public static function providerForDecoding(): 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 providerForEncoding(): 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 providerForRoundTripWithAllZeroBytes(): array
{
return [
'Single zero byte' => ['value' => "\x00"],
'Two zero bytes' => ['value' => "\x00\x00"],
'Eight zero bytes' => ['value' => str_repeat("\x00", 8)]
];
}
public static function providerForRoundTripWithLeadingZeroBytes(): array
{
return [
'Leading zero bytes 01' => ['value' => '001jlt60MnKnB9ECKRt4gl'],
'Leading zero bytes 02' => ['value' => hex2bin('07d8e31da269bf28')],
'Leading zero bytes 03' => ['value' => hex2bin('0000010203040506')]
];
}
}