Skip to content

Commit 784223b

Browse files
committed
Add missing tests
1 parent 7b3755b commit 784223b

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

tests/Base32Test.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,62 @@ public function testDecodeThenEncodeIsIdempotent(): void
7777
$this->assertSame($original, $reEncoded);
7878
}
7979

80-
public function testLowercaseInputDoesNotDecodeToExpectedValue(): void
80+
public function testLowercaseInputThrowsException(): void
8181
{
8282
$lower = 'mzxw6ytboi';
83-
$decodedLower = Base32::fromBase32($lower);
8483

85-
$this->assertNotSame('foobar', $decodedLower);
84+
$this->expectException(\InvalidArgumentException::class);
85+
$this->expectExceptionMessage("Invalid character 'm' at position 0 in Base32 input.");
86+
87+
Base32::fromBase32($lower);
88+
}
89+
90+
public function testCustomAlphabet(): void
91+
{
92+
$customAlphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
93+
$data = 'Hello World';
94+
95+
$encoded = Base32::toBase32($data, $customAlphabet);
96+
$decoded = Base32::fromBase32($encoded, $customAlphabet);
97+
98+
$this->assertSame($data, $decoded);
99+
}
100+
101+
public function testInvalidAlphabetLengthThrowsException(): void
102+
{
103+
$shortAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ12345'; // 31 chars instead of 32
104+
105+
$this->expectException(\InvalidArgumentException::class);
106+
$this->expectExceptionMessage('Base32 alphabet must contain exactly 32 characters.');
107+
108+
Base32::toBase32('test', $shortAlphabet);
109+
}
110+
111+
public function testDuplicateCharactersInAlphabetThrowsException(): void
112+
{
113+
$duplicateAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ23456A'; // 'A' appears twice
114+
115+
$this->expectException(\InvalidArgumentException::class);
116+
$this->expectExceptionMessage('Base32 alphabet must contain unique characters.');
117+
118+
Base32::toBase32('test', $duplicateAlphabet);
119+
}
120+
121+
public function testPaddingIsIgnoredDuringDecoding(): void
122+
{
123+
$paddedBase32 = 'MZXW6YTBOI======'; // "foobar" with padding
124+
$decoded = Base32::fromBase32($paddedBase32);
125+
126+
$this->assertSame('foobar', $decoded);
127+
}
128+
129+
public function testInvalidCharacterInMiddleThrowsException(): void
130+
{
131+
$invalidBase32 = 'MZXW@YTBOI'; // '@' is invalid
132+
133+
$this->expectException(\InvalidArgumentException::class);
134+
$this->expectExceptionMessage("Invalid character '@' at position 4 in Base32 input.");
135+
136+
Base32::fromBase32($invalidBase32);
86137
}
87138
}

tests/BinaryStringTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,18 @@ public function testEquals()
6464
$this->assertTrue($this->binaryString->equals(BinaryString::fromString("\x01\x02\x03\x04")));
6565
$this->assertFalse($this->binaryString->equals(BinaryString::fromString("\xFF\xFF\xFF\xFF")));
6666
}
67+
68+
public function testToBase32()
69+
{
70+
$binaryString = BinaryString::fromString("foobar");
71+
$this->assertEquals("MZXW6YTBOI", $binaryString->toBase32());
72+
}
73+
74+
public function testFromBase32()
75+
{
76+
$reconstructedString = BinaryString::fromBase32("MZXW6YTBOI");
77+
$expected = BinaryString::fromString("foobar");
78+
79+
$this->assertTrue($expected->equals($reconstructedString));
80+
}
6781
}

0 commit comments

Comments
 (0)