Debian Codegraph @

lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php

raw
1 <?php
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 *
21 */
22
23 namespace Test\Thrift\Unit\Lib\Protocol;
24
25 use PHPUnit\Framework\TestCase;
26 use Test\Thrift\Unit\Lib\ReflectionHelper;
27 use Thrift\Exception\TProtocolException;
28 use Thrift\Protocol\TCompactProtocol;
29 use Thrift\Transport\TTransport;
30 use Thrift\Type\TType;
31
32 class TCompactProtocolTest extends TestCase
33 {
34 use ReflectionHelper;
35
36 private const COMPACT_STOP = 0x00;
37 private const COMPACT_TRUE = 0x01;
38 private const COMPACT_FALSE = 0x02;
39 private const COMPACT_BYTE = 0x03;
40 private const COMPACT_I16 = 0x04;
41 private const COMPACT_I32 = 0x05;
42 private const COMPACT_I64 = 0x06;
43 private const COMPACT_DOUBLE = 0x07;
44 private const COMPACT_BINARY = 0x08;
45 private const COMPACT_LIST = 0x09;
46 private const COMPACT_SET = 0x0A;
47 private const COMPACT_MAP = 0x0B;
48 private const COMPACT_STRUCT = 0x0C;
49
50 private const STATE_CLEAR = 0;
51 private const STATE_FIELD_WRITE = 1;
52 private const STATE_VALUE_WRITE = 2;
53 private const STATE_CONTAINER_WRITE = 3;
54 private const STATE_BOOL_WRITE = 4;
55 private const STATE_FIELD_READ = 5;
56 private const STATE_CONTAINER_READ = 6;
57 private const STATE_VALUE_READ = 7;
58 private const STATE_BOOL_READ = 8;
59
60 private const VERSION_MASK = 0x1f;
61 private const VERSION = 1;
62 private const PROTOCOL_ID = 0x82;
63 private const TYPE_MASK = 0xe0;
64 private const TYPE_BITS = 0x07;
65 private const TYPE_SHIFT_AMOUNT = 5;
66
67 /**
68 * @dataProvider toZigZagDataProvider
69 */
70 public function testToZigZag(
71 $n,
72 $bits,
73 $expected
74 ) {
75 $protocol = new TCompactProtocol($this->createMock(TTransport::class));
76 $this->assertSame($expected, $protocol->toZigZag($n, $bits));
77 }
78
79 public function toZigZagDataProvider()
80 {
81 yield ['n' => 0, 'bits' => 16, 'expected' => 0];
82 yield ['n' => -1, 'bits' => 16, 'expected' => 1];
83 yield ['n' => 1, 'bits' => 16, 'expected' => 2];
84 yield ['n' => -2, 'bits' => 16, 'expected' => 3];
85 yield ['n' => 2, 'bits' => 16, 'expected' => 4];
86 yield ['n' => -1, 'bits' => 32, 'expected' => 1];
87 yield ['n' => 1, 'bits' => 32, 'expected' => 2];
88 yield ['n' => -1, 'bits' => 64, 'expected' => 1];
89 yield ['n' => 1, 'bits' => 64, 'expected' => 2];
90 yield ['n' => -0x7fffffff, 'bits' => 64, 'expected' => 4294967293];
91 yield ['n' => 0x7fffffff, 'bits' => 64, 'expected' => 4294967294];
92 }
93
94 /**
95 * @dataProvider fromZigZagDataProvider
96 */
97 public function testFromZigZag(
98 $n,
99 $expected
100 ) {
101 $protocol = new TCompactProtocol($this->createMock(TTransport::class));
102 $this->assertSame($expected, $protocol->fromZigZag($n));
103 }
104
105 public function fromZigZagDataProvider()
106 {
107 yield ['n' => 0, 'expected' => 0];
108 yield ['n' => 1, 'expected' => -1];
109 yield ['n' => 2, 'expected' => 1];
110 yield ['n' => 3, 'expected' => -2];
111 yield ['n' => 4, 'expected' => 2];
112 yield ['n' => 4294967293, 'expected' => -0x7fffffff];
113 yield ['n' => 4294967294, 'expected' => 0x7fffffff];
114 }
115
116 /**
117 * @dataProvider getVarintDataProvider
118 */
119 public function testGetVarint(
120 $data,
121 $expected
122 ) {
123 $protocol = new TCompactProtocol($this->createMock(TTransport::class));
124 $this->assertSame($expected, $protocol->getVarint($data));
125 }
126
127 public function getVarintDataProvider()
128 {
129 yield ['data' => 0, 'expected' => "\x00"];
130 yield ['data' => 1, 'expected' => "\x01"];
131 yield ['data' => 97, 'expected' => "a"];
132 yield ['data' => 100, 'expected' => "d"];
133 yield ['data' => 1000, 'expected' => "\xe8\x07"];
134 }
135
136 public function testWriteVarint()
137 {
138 $transport = $this->createMock(TTransport::class);
139 $protocol = new TCompactProtocol($transport);
140
141 $transport->expects($this->once())
142 ->method('write')
143 ->with("\xe8\x07", 2);
144
145 $protocol->writeVarint(1000);
146 }
147
148 public function testReadVarint()
149 {
150 $transport = $this->createMock(TTransport::class);
151 $protocol = new TCompactProtocol($transport);
152
153 $transport->expects($this->exactly(2))
154 ->method('readAll')
155 ->with(1)
156 ->willReturnOnConsecutiveCalls(
157 ...[
158 "\xe8",
159 "\x07",
160 ]
161 );
162
163 $this->assertSame(2, $protocol->readVarint($result));
164 $this->assertSame(1000, $result);
165 }
166
167 public function testWriteMessageBegin()
168 {
169 $name = 'testName';
170 $type = TType::STRING;
171 $seqid = 1;
172
173 $transport = $this->createMock(TTransport::class);
174 $protocol = new TCompactProtocol($transport);
175
176 $transport
177 ->expects($this->exactly(5))
178 ->method('write')
179 ->withConsecutive(
180 ...[
181 [pack('C', self::PROTOCOL_ID), 1], #protocal id
182 [pack('C', self::VERSION | ($type << TCompactProtocol::TYPE_SHIFT_AMOUNT)), 1], #version
183 ["\x01", 1], #seqid
184 ["\x08", 1], #field name length
185 ["testName", 8], #field name
186 ]
187 )->willReturnOnConsecutiveCalls(
188 1,
189 1,
190 1,
191 1,
192 8
193 );
194
195 $result = $protocol->writeMessageBegin($name, $type, $seqid);
196 $this->assertSame(12, $result);
197
198 $this->assertSame(self::STATE_VALUE_WRITE, $this->getPropertyValue($protocol, 'state'));
199 }
200
201 public function testWriteMessageEnd()
202 {
203 $transport = $this->createMock(TTransport::class);
204 $protocol = new TCompactProtocol($transport);
205
206 $this->assertSame(0, $protocol->writeMessageEnd());
207 $this->assertSame(self::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
208 }
209
210 public function testWriteStruct()
211 {
212 $name = 'testName';
213
214 $transport = $this->createMock(TTransport::class);
215 $protocol = new TCompactProtocol($transport);
216 $this->assertSame(0, $protocol->writeStructBegin($name));
217 $this->assertSame([[self::STATE_CLEAR, 0]], $this->getPropertyValue($protocol, 'structs'));
218 $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
219 $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
220
221 $this->assertSame(0, $protocol->writeStructBegin($name));
222 $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
223 $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
224 $this->assertSame([[self::STATE_CLEAR, 0], [self::STATE_FIELD_WRITE, 0]], $this->getPropertyValue($protocol, 'structs'));
225
226 $this->assertSame(0, $protocol->writeStructEnd());
227 $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
228 $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
229 $this->assertSame([[self::STATE_CLEAR, 0]], $this->getPropertyValue($protocol, 'structs'));
230
231 $this->assertSame(0, $protocol->writeStructEnd());
232 $this->assertSame(self::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
233 $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
234 $this->assertSame([], $this->getPropertyValue($protocol, 'structs'));
235 }
236
237 public function testWriteFieldStop()
238 {
239 $transport = $this->createMock(TTransport::class);
240 $protocol = new TCompactProtocol($transport);
241
242 $transport->expects($this->once())
243 ->method('write')
244 ->with("\x00", 1);
245
246 $this->assertSame(1, $protocol->writeFieldStop());
247 }
248
249 /**
250 * @dataProvider writeFieldHeaderDataProvider
251 */
252 public function testWriteFieldHeader(
253 $type,
254 $fid,
255 $writeCallParams,
256 $writeCallResult,
257 $expectedResult
258 ) {
259 $transport = $this->createMock(TTransport::class);
260 $protocol = new TCompactProtocol($transport);
261
262 $transport
263 ->expects($this->exactly(count($writeCallParams)))
264 ->method('write')
265 ->withConsecutive(...$writeCallParams)
266 ->willReturnOnConsecutiveCalls(...$writeCallResult);
267
268 $this->assertSame($expectedResult, $protocol->writeFieldHeader($type, $fid));
269 }
270
271 public function writeFieldHeaderDataProvider()
272 {
273 yield 'bool' => [
274 'type' => TType::BOOL,
275 'fid' => 1,
276 'writeCallParams' => [
277 ["\x12", 1], #writeUByte(pack('C', ($delta << 4) | $type)),
278 ],
279 'writeCallResult' => [
280 1,
281 ],
282 'expectedResult' => 1,
283 ];
284 yield 'list' => [
285 'type' => TType::LST,
286 'fid' => 16,
287 'writeCallParams' => [
288 ["\x0f", 1], #writeUByte(pack('C', ($delta << 4) | $type)),
289 [" ", 1], #writeI16($fid),
290 ],
291 'writeCallResult' => [
292 1,
293 ],
294 'expectedResult' => 2,
295 ];
296 }
297
298 /**
299 * @dataProvider writeFieldBeginDataProvider
300 */
301 public function testWriteFieldBegin(
302 $fieldName,
303 $fieldType,
304 $fieldId,
305 $writeCallParams,
306 $writeCallResult,
307 $expectedState,
308 $expectedBoolFid,
309 $expectedLastFid,
310 $expectedResult
311 ) {
312 $transport = $this->createMock(TTransport::class);
313 $protocol = new TCompactProtocol($transport);
314
315 $transport
316 ->expects($this->exactly(count($writeCallParams)))
317 ->method('write')
318 ->withConsecutive(...$writeCallParams)
319 ->willReturnOnConsecutiveCalls(...$writeCallResult);
320
321 $this->assertSame($expectedResult, $protocol->writeFieldBegin($fieldName, $fieldType, $fieldId));
322
323 $this->assertSame($expectedState, $this->getPropertyValue($protocol, 'state'));
324 $this->assertSame($expectedBoolFid, $this->getPropertyValue($protocol, 'boolFid'));
325 $this->assertSame($expectedLastFid, $this->getPropertyValue($protocol, 'lastFid'));
326 }
327
328 public function writeFieldBeginDataProvider()
329 {
330 yield 'bool' => [
331 'fieldName' => 'testName',
332 'fieldType' => TType::BOOL,
333 'fieldId' => 1,
334 'writeCallParams' => [],
335 'writeCallResult' => [],
336 'expectedState' => self::STATE_BOOL_WRITE,
337 'expectedBoolFid' => 1,
338 'expectedLastFid' => 0,
339 'expectedResult' => 0,
340 ];
341 yield 'list' => [
342 'fieldName' => 'testName',
343 'fieldType' => TType::LST,
344 'fieldId' => 1,
345 'writeCallParams' => [
346 ["\x19", 1], #writeUByte(pack('C', ($delta << 4) | $type)),
347 ],
348 'writeCallResult' => [
349 1,
350 ],
351 'expectedState' => self::STATE_VALUE_WRITE,
352 'expectedBoolFid' => null,
353 'expectedLastFid' => 1,
354 'expectedResult' => 1,
355 ];
356 }
357
358 public function testWriteFieldEnd()
359 {
360 $transport = $this->createMock(TTransport::class);
361 $protocol = new TCompactProtocol($transport);
362
363 $this->assertSame(0, $protocol->writeFieldEnd());
364
365 $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
366 }
367
368 /**
369 * @dataProvider writeCollectionDataProvider
370 */
371 public function testWriteCollection(
372 $etype,
373 $size,
374 $writeCallParams,
375 $writeCallResult,
376 $expectedState,
377 $expectedContainers,
378 $expectedResult
379 ) {
380 $transport = $this->createMock(TTransport::class);
381 $protocol = new TCompactProtocol($transport);
382
383 $transport
384 ->expects($this->exactly(count($writeCallParams)))
385 ->method('write')
386 ->withConsecutive(...$writeCallParams)
387 ->willReturnOnConsecutiveCalls(...$writeCallResult);
388
389 $this->assertSame($expectedResult, $protocol->writeCollectionBegin($etype, $size));
390
391 $this->assertSame($expectedState, $this->getPropertyValue($protocol, 'state'));
392 $this->assertSame($expectedContainers, $this->getPropertyValue($protocol, 'containers'));
393
394 $this->assertSame(0, $protocol->writeCollectionEnd());
395 $this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
396 }
397
398 public function writeCollectionDataProvider()
399 {
400 yield 'size < 14' => [
401 'etype' => TType::STRING,
402 'size' => 1,
403 'writeCallParams' => [
404 ["\x18", 1], #writeUByte(pack('C', ($size << 4 | self::$ctypes[$etype])),
405 ],
406 'writeCallResult' => [
407 1,
408 ],
409 'expectedState' => self::STATE_CONTAINER_WRITE,
410 'expectedContainers' => [
411 0 => 0,
412 ],
413 'expectedResult' => 1,
414 ];
415 yield 'size > 14' => [
416 'etype' => TType::STRING,
417 'size' => 16,
418 'writeCallParams' => [
419 ["\xf8", 1], #writeUByte(pack('C', 0xf0 | self::$ctypes[$etype])),
420 ["\x10", 1], #writeVarint(16),
421 ],
422 'writeCallResult' => [
423 1,
424 1,
425 ],
426 'expectedState' => self::STATE_CONTAINER_WRITE,
427 'expectedContainers' => [
428 0 => 0,
429 ],
430 'expectedResult' => 2,
431 ];
432 }
433
434 /**
435 * @dataProvider writeMapDataProvider
436 */
437 public function testWriteMap(
438 $keyType,
439 $valType,
440 $size,
441 $writeCallParams,
442 $writeCallResult,
443 $expectedContainers,
444 $expectedResult
445 ) {
446 $transport = $this->createMock(TTransport::class);
447 $protocol = new TCompactProtocol($transport);
448
449 $transport
450 ->expects($this->exactly(count($writeCallParams)))
451 ->method('write')
452 ->withConsecutive(...$writeCallParams)
453 ->willReturnOnConsecutiveCalls(...$writeCallResult);
454
455 $this->assertSame($expectedResult, $protocol->writeMapBegin($keyType, $valType, $size));
456
457 $this->assertSame($expectedContainers, $this->getPropertyValue($protocol, 'containers'));
458 $this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
459
460 $this->assertSame(0, $protocol->writeMapEnd());
461 $this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
462 $this->assertSame([], $this->getPropertyValue($protocol, 'containers'));
463 }
464
465 public function writeMapDataProvider()
466 {
467 yield 'size zero' => [
468 'keyType' => TType::STRING,
469 'valType' => TType::STRING,
470 'size' => 0,
471 'writeCallParams' => [
472 ["\x00", 1], #writeByte(0),
473 ],
474 'writeCallResult' => [
475 1,
476 ],
477 'expectedContainers' => [
478 0 => 0,
479 ],
480 'expectedResult' => 1,
481 ];
482 yield 'size non zero' => [
483 'keyType' => TType::STRING,
484 'valType' => TType::STRING,
485 'size' => 16,
486 'writeCallParams' => [
487 ["\x10", 1], #writeVarint(16),
488 ["\x88", 1], #writeUByte(pack('C', self::$ctypes[$key_type] << 4 | self::$ctypes[$val_type])),
489 ],
490 'writeCallResult' => [
491 1,
492 1,
493 ],
494 'expectedContainers' => [
495 0 => 0,
496 ],
497 'expectedResult' => 2,
498 ];
499 }
500
501 public function testWriteListBegin()
502 {
503 $protocol = $this->createPartialMock(TCompactProtocol::class, ['writeCollectionBegin']);
504
505 $protocol->expects($this->once())
506 ->method('writeCollectionBegin')
507 ->with(TType::STRING, 1)
508 ->willReturn(1);
509
510 $this->assertSame(1, $protocol->writeListBegin(TType::STRING, 1));
511 }
512
513 public function testWriteListEnd()
514 {
515 $protocol = $this->createPartialMock(TCompactProtocol::class, ['writeCollectionEnd']);
516
517 $protocol->expects($this->once())
518 ->method('writeCollectionEnd')
519 ->willReturn(1);
520
521 $this->assertSame(1, $protocol->writeListEnd());
522 }
523
524 public function testWriteSettBegin()
525 {
526 $protocol = $this->createPartialMock(TCompactProtocol::class, ['writeCollectionBegin']);
527
528 $protocol->expects($this->once())
529 ->method('writeCollectionBegin')
530 ->with(TType::STRING, 1)
531 ->willReturn(1);
532
533 $this->assertSame(1, $protocol->writeSetBegin(TType::STRING, 1));
534 }
535
536 public function testWriteSetEnd()
537 {
538 $protocol = $this->createPartialMock(TCompactProtocol::class, ['writeCollectionEnd']);
539
540 $protocol->expects($this->once())
541 ->method('writeCollectionEnd')
542 ->willReturn(1);
543
544 $this->assertSame(1, $protocol->writeSetEnd());
545 }
546
547 /**
548 * @dataProvider writeBinaryDataProvider
549 */
550 public function testWriteBool(
551 $value,
552 $startState,
553 $writeCallParams,
554 $writeCallResult,
555 $expectedResult,
556 $expectedException,
557 $expectedExceptionMessage
558 ) {
559 if ($expectedException) {
560 $this->expectException($expectedException);
561 $this->expectExceptionMessage($expectedExceptionMessage);
562 }
563
564 $transport = $this->createMock(TTransport::class);
565 $protocol = new TCompactProtocol($transport);
566 if (!is_null($startState)) {
567 $this->setPropertyValue($protocol, 'state', $startState);
568 }
569
570 $transport
571 ->expects($this->exactly(count($writeCallParams)))
572 ->method('write')
573 ->withConsecutive(...$writeCallParams)
574 ->willReturnOnConsecutiveCalls(...$writeCallResult);
575
576 $this->assertSame($expectedResult, $protocol->writeBool($value));
577 }
578
579 public function writeBinaryDataProvider()
580 {
581 yield 'invalid state' => [
582 'value' => true,
583 'startState' => null,
584 'writeCallParams' => [],
585 'writeCallResult' => [],
586 'expectedResult' => 0,
587 'expectedException' => TProtocolException::class,
588 'expectedExceptionMessage' => 'Invalid state in compact protocol',
589 ];
590
591 yield 'true' => [
592 'value' => true,
593 'startState' => TCompactProtocol::STATE_BOOL_WRITE,
594 'writeCallParams' => [
595 ["\x01", 1], #writeByte
596 ["\x00", 1], #writeI16
597 ],
598 'writeCallResult' => [
599 1,
600 1,
601 ],
602 'expectedResult' => 2,
603 'expectedException' => null,
604 'expectedExceptionMessage' => null,
605 ];
606
607 yield 'false' => [
608 'value' => false,
609 'startState' => TCompactProtocol::STATE_BOOL_WRITE,
610 'writeCallParams' => [
611 ["\x02", 1], #writeByte
612 ["\x00", 1], #writeI16
613 ],
614 'writeCallResult' => [
615 1,
616 1,
617 ],
618 'expectedResult' => 2,
619 'expectedException' => null,
620 'expectedExceptionMessage' => null,
621 ];
622
623 yield 'container true' => [
624 'value' => true,
625 'startState' => TCompactProtocol::STATE_CONTAINER_WRITE,
626 'writeCallParams' => [
627 ["\x01", 1], #writeByte
628 ],
629 'writeCallResult' => [
630 1,
631 ],
632 'expectedResult' => 1,
633 'expectedException' => null,
634 'expectedExceptionMessage' => null,
635 ];
636 }
637
638 /**
639 * @dataProvider writeByteDataProvider
640 */
641 public function testWriteByte(
642 $value,
643 $expectedWriteCallParam
644 ) {
645 $transport = $this->createMock(TTransport::class);
646 $protocol = new TCompactProtocol($transport);
647
648 $transport->expects($this->once())
649 ->method('write')
650 ->with($expectedWriteCallParam, 1);
651
652 $this->assertSame(1, $protocol->writeByte($value));
653 }
654
655 public function writeByteDataProvider()
656 {
657 yield 'signed' => [
658 'value' => -1,
659 'expectedWriteCallParam' => "\xff",
660 ];
661 yield 'unsigned' => [
662 'value' => 1,
663 'expectedWriteCallParam' => "\x01",
664 ];
665 yield 'lowercase' => [
666 'value' => 'a',
667 'expectedWriteCallParam' => "\x00",
668 ];
669 yield 'upercase' => [
670 'value' => 'A',
671 'expectedWriteCallParam' => "\x00",
672 ];
673 }
674
675 /**
676 * @dataProvider writeUByteDataProvider
677 */
678 public function testWriteUByte(
679 $value,
680 $expectedWriteCallParam
681 ) {
682 $transport = $this->createMock(TTransport::class);
683 $protocol = new TCompactProtocol($transport);
684
685 $transport->expects($this->once())
686 ->method('write')
687 ->with($expectedWriteCallParam, 1);
688
689 $this->assertSame(1, $protocol->writeUByte($value));
690 }
691
692 public function writeUByteDataProvider()
693 {
694 yield 'signed' => [
695 'value' => -1,
696 'expectedWriteCallParam' => "\xff",
697 ];
698 yield 'unsigned' => [
699 'value' => 1,
700 'expectedWriteCallParam' => "\x01",
701 ];
702 yield 'lowercase' => [
703 'value' => 'a',
704 'expectedWriteCallParam' => "\x00",
705 ];
706 yield 'upercase' => [
707 'value' => 'A',
708 'expectedWriteCallParam' => "\x00",
709 ];
710 }
711
712 public function testWriteI16()
713 {
714 $transport = $this->createMock(TTransport::class);
715 $protocol = new TCompactProtocol($transport);
716
717 $transport->expects($this->once())
718 ->method('write')
719 ->with("\x00", 1);
720
721 $this->assertSame(1, $protocol->writeI16(0));
722 }
723
724 public function testWriteI32()
725 {
726 $transport = $this->createMock(TTransport::class);
727 $protocol = new TCompactProtocol($transport);
728
729 $transport->expects($this->once())
730 ->method('write')
731 ->with("\x00", 1);
732
733 $this->assertSame(1, $protocol->writeI32(0));
734 }
735
736 public function testWriteDouble()
737 {
738 $transport = $this->createMock(TTransport::class);
739 $protocol = new TCompactProtocol($transport);
740
741 $transport->expects($this->once())
742 ->method('write')
743 ->with(pack('d', 0), 8);
744
745 $this->assertSame(8, $protocol->writeDouble(0));
746 }
747
748 public function testWriteString()
749 {
750 $transport = $this->createMock(TTransport::class);
751 $protocol = new TCompactProtocol($transport);
752
753 $transport->expects($this->exactly(2))
754 ->method('write')
755 ->withConsecutive(
756 ["\x04", 1],
757 ["test", 4]
758 );
759
760 $this->assertSame(5, $protocol->writeString('test'));
761 }
762
763 public function testWriteUuid()
764 {
765 $uuid = '01234567-89ab-cdef-0123-456789abcdef';
766 $transport = $this->createMock(TTransport::class);
767 $protocol = new TCompactProtocol($transport);
768
769 $transport
770 ->expects($this->once())
771 ->method('write')
772 ->with(hex2bin('0123456789abcdef0123456789abcdef'), 16)
773 ->willReturn(16);
774
775 $this->assertSame(16, $protocol->writeUuid($uuid));
776 }
777
778 public function testReadUuid()
779 {
780 $transport = $this->createMock(TTransport::class);
781 $protocol = new TCompactProtocol($transport);
782
783 $transport
784 ->expects($this->once())
785 ->method('readAll')
786 ->with(16)
787 ->willReturn(hex2bin('0123456789abcdef0123456789abcdef'));
788
789 $this->assertSame(16, $protocol->readUuid($value));
790 $this->assertSame('01234567-89ab-cdef-0123-456789abcdef', $value);
791 }
792
793 /**
794 * @dataProvider writeI64DataProvider
795 */
796 public function testWriteI64(
797 $value,
798 $expectedWriteCallParam,
799 $expectedResult
800 ) {
801 $transport = $this->createMock(TTransport::class);
802 $protocol = new TCompactProtocol($transport);
803
804 $transport->expects($this->once())
805 ->method('write')
806 ->with(...$expectedWriteCallParam);
807
808 $this->assertSame($expectedResult, $protocol->writeI64($value));
809 }
810
811 public function writeI64DataProvider()
812 {
813 yield 'simple' => [
814 'value' => 0,
815 'expectedWriteCallParam' => ["\x00", 1],
816 'expectedResult' => 1,
817 ];
818 yield 'negative' => [
819 'value' => -1,
820 'expectedWriteCallParam' => ["\x01", 1],
821 'expectedResult' => 1,
822 ];
823 yield 'big' => [
824 'value' => 5000000000,
825 'expectedWriteCallParam' => [hex2bin("80c8afa025"), 5],
826 'expectedResult' => 5,
827 ];
828 yield 'small' => [
829 'value' => -5000000000,
830 'expectedWriteCallParam' => [hex2bin("ffc7afa025"), 5],
831 'expectedResult' => 5,
832 ];
833 yield 'max simple' => [
834 'value' => 0xffffffff,
835 'expectedWriteCallParam' => [hex2bin("feffffff1f"), 5],
836 'expectedResult' => 5,
837 ];
838 }
839 }