@@ -12,27 +12,40 @@ class ArrayVariableTest extends TestCase
12
12
public function it_encodes_variable_length_arrays ()
13
13
{
14
14
$ arr = new ExampleArrayVariable ([1 , 2 ]);
15
- $ bytes = XDR ::fresh ()->write ($ arr , XDR ::ARRAY_VARIABLE )->buffer ();
16
- $ this ->assertEquals (12 , strlen ($ bytes ));
17
- $ this ->assertEquals ('000000020000000100000002 ' , bin2hex ($ bytes ));
15
+ $ buffer = XDR ::fresh ()->write ($ arr , ExampleArrayVariable::class)->buffer ();
16
+ $ this ->assertEquals ('000000020000000100000002 ' , bin2hex ($ buffer ));
18
17
}
19
18
20
19
/** @test */
21
20
public function it_encodes_variable_length_arrays_using_the_shorter_syntax ()
22
21
{
23
22
$ arr = new ExampleArrayVariable ([1 , 2 ]);
24
- $ bytes = XDR ::fresh ()->write ($ arr , XDR ::ARRAY_VARIABLE )->buffer ();
25
- $ this ->assertEquals (12 , strlen ($ bytes ));
26
- $ this ->assertEquals ('000000020000000100000002 ' , bin2hex ($ bytes ));
23
+ $ buffer = XDR ::fresh ()->write ($ arr )->buffer ();
24
+ $ this ->assertEquals ('000000020000000100000002 ' , bin2hex ($ buffer ));
25
+ }
26
+
27
+ /** @test */
28
+ public function it_encodes_variable_length_arrays_containing_values_of_different_lengths ()
29
+ {
30
+ $ arr = new ExampleVariableStringArray (['one ' , 'two ' , 'three ' ]);
31
+ $ buffer = XDR ::fresh ()->write ($ arr , ExampleVariableStringArray::class)->buffer ();
32
+ $ this ->assertEquals ('00000003000000036f6e65000000000374776f00000000057468726565000000 ' , bin2hex ($ buffer ));
33
+ }
34
+
35
+ /** @test */
36
+ public function it_encodes_variable_length_arrays_containing_data_with_a_fixed_length ()
37
+ {
38
+ $ arr = new ExampleVariableOpaqueFixedArray (['abc ' , 'def ' , 'ghi ' ]);
39
+ $ buffer = XDR ::fresh ()->write ($ arr , ExampleVariableOpaqueFixedArray::class)->buffer ();
40
+ $ this ->assertEquals ('00000003616263006465660067686900 ' , bin2hex ($ buffer ));
27
41
}
28
42
29
43
/** @test */
30
44
public function it_accepts_a_variable_array_instance_class_name_as_a_type_parameter ()
31
45
{
32
46
$ arr = new ExampleArrayVariable ([1 , 2 ]);
33
- $ bytes = XDR ::fresh ()->write ($ arr , ExampleArrayVariable::class)->buffer ();
34
- $ this ->assertEquals (12 , strlen ($ bytes ));
35
- $ this ->assertEquals ('000000020000000100000002 ' , bin2hex ($ bytes ));
47
+ $ buffer = XDR ::fresh ()->write ($ arr , ExampleArrayVariable::class)->buffer ();
48
+ $ this ->assertEquals ('000000020000000100000002 ' , bin2hex ($ buffer ));
36
49
}
37
50
38
51
/** @test */
@@ -50,6 +63,24 @@ public function it_decodes_variable_length_arrays_using_the_shorter_syntax()
50
63
$ this ->assertInstanceOf (ExampleArrayVariable::class, $ arr );
51
64
$ this ->assertEquals ([1 , 2 ], $ arr ->arr );
52
65
}
66
+
67
+ /** @test */
68
+ public function it_decodes_variable_length_arrays_containing_values_of_different_lengths ()
69
+ {
70
+ $ arr = XDR ::fromHex ('00000003000000036f6e65000000000374776f00000000057468726565000000 ' )
71
+ ->read (XDR ::ARRAY_VARIABLE , ExampleVariableStringArray::class);
72
+ $ this ->assertInstanceOf (ExampleVariableStringArray::class, $ arr );
73
+ $ this ->assertEquals (['one ' , 'two ' , 'three ' ], $ arr ->arr );
74
+ }
75
+
76
+ /** @test */
77
+ public function it_decodes_variable_length_arrays_containing_values_with_a_fixed_length ()
78
+ {
79
+ $ arr = XDR ::fromHex ('00000003616263006465660067686900 ' )
80
+ ->read (XDR ::ARRAY_VARIABLE , ExampleVariableOpaqueFixedArray::class);
81
+ $ this ->assertInstanceOf (ExampleVariableOpaqueFixedArray::class, $ arr );
82
+ $ this ->assertEquals (['abc ' , 'def ' , 'ghi ' ], $ arr ->arr );
83
+ }
53
84
}
54
85
55
86
class ExampleArrayVariable implements XdrArray
@@ -84,3 +115,69 @@ public static function newFromXdr(array $arr): static
84
115
return new static ($ arr );
85
116
}
86
117
}
118
+
119
+ class ExampleVariableStringArray implements XdrArray
120
+ {
121
+ public function __construct (public array $ arr = [])
122
+ {
123
+ $ this ->arr = $ arr ;
124
+ }
125
+
126
+ public function getXdrArray (): array
127
+ {
128
+ return $ this ->arr ;
129
+ }
130
+
131
+ public static function getXdrLength (): ?int
132
+ {
133
+ return null ;
134
+ }
135
+
136
+ public static function getXdrType (): string
137
+ {
138
+ return XDR ::STRING ;
139
+ }
140
+
141
+ public static function getXdrTypeLength (): ?int
142
+ {
143
+ return null ;
144
+ }
145
+
146
+ public static function newFromXdr (array $ arr ): static
147
+ {
148
+ return new static ($ arr );
149
+ }
150
+ }
151
+
152
+ class ExampleVariableOpaqueFixedArray implements XdrArray
153
+ {
154
+ public function __construct (public array $ arr )
155
+ {
156
+ $ this ->arr = $ arr ;
157
+ }
158
+
159
+ public function getXdrArray (): array
160
+ {
161
+ return $ this ->arr ;
162
+ }
163
+
164
+ public static function getXdrLength (): ?int
165
+ {
166
+ return null ;
167
+ }
168
+
169
+ public static function getXdrType (): string
170
+ {
171
+ return XDR ::OPAQUE_FIXED ;
172
+ }
173
+
174
+ public static function getXdrTypeLength (): ?int
175
+ {
176
+ return 3 ;
177
+ }
178
+
179
+ public static function newFromXdr (array $ arr ): static
180
+ {
181
+ return new static ($ arr );
182
+ }
183
+ }
0 commit comments