@@ -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}
0 commit comments