1717
1818class TextStringTest extends TestCase
1919{
20- #[DataProvider('provideCases ' )]
21- public function testItCanEncodeCorrectly (string $ case , string $ header ): void
20+ #[DataProvider('provideValidCases ' )]
21+ public function testEncodeProducesCorrectCBORRepresentation (string $ input , string $ expectedHeader ): void
2222 {
23- $ actual = bin2hex (TextString::encode ($ case ));
24- $ expected = bin2hex ($ header . $ case );
23+ $ actual = bin2hex (TextString::encode ($ input ));
24+ $ expected = bin2hex ($ expectedHeader . $ input );
2525
2626 $ this ->assertSame ($ expected , $ actual );
2727 }
2828
29- #[DataProvider('provideCases ' )]
30- public function testItCanDecodeCorrectly (string $ case , string $ header ): void
29+ #[DataProvider('provideValidCases ' )]
30+ public function testDecodeExtractsOriginalStringFromCBORRepresentation (string $ input , string $ header ): void
3131 {
32- $ target = $ header . $ case ;
32+ $ encoded = $ header . $ input ;
3333
34- $ actual = TextString::decode ($ target );
35- $ expected = $ case ;
34+ $ actual = TextString::decode ($ encoded );
35+ $ expected = $ input ;
3636
3737 $ this ->assertSame ($ expected , $ actual );
3838 }
3939
40- public static function provideCases (): array
40+ public static function provideValidCases (): array
4141 {
4242 return [
4343 ["f " , "\x61" ],
@@ -54,4 +54,30 @@ public static function provideCases(): array
5454 ]
5555 ];
5656 }
57+
58+ public function testDecodeThrowsExceptionForInvalidMajorType (): void
59+ {
60+ $ this ->expectException (\ValueError::class);
61+ $ this ->expectExceptionMessage ("Invalid CBOR TextString major type. " );
62+
63+ TextString::decode ("\x0C" );
64+ }
65+
66+ public function testDecodeThrowsExceptionForLengthMismatch (): void
67+ {
68+ $ this ->expectException (\ValueError::class);
69+ $ this ->expectExceptionMessage ("Invalid CBOR TextString length mismatch. " );
70+
71+ // Encoded length is 5, but actual length is 4
72+ TextString::decode ("\x65\x66\x6F\x6F\x62" );
73+ }
74+
75+ public function testDecodeThrowsExceptionForInvalidAdditionalInformation (): void
76+ {
77+ $ this ->expectException (\ValueError::class);
78+ $ this ->expectExceptionMessage ("Invalid CBOR TextString length information. " );
79+
80+ // Additional info 28 is invalid for text strings
81+ TextString::decode ("\x7C\x01" );
82+ }
5783}
0 commit comments