PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/ext-libs/Protobuf-PHP/tests/Protobuf.Codec.Binary.spec.php

https://gitlab.com/billyprice1/app-download.org
PHP | 280 lines | 208 code | 65 blank | 7 comment | 2 complexity | ed953aec0a387cc26846632820b3666b MD5 | raw file
  1. <?php
  2. require_once __DIR__ . '/../library/DrSlump/Protobuf.php';
  3. error_reporting(E_ALL);
  4. use \DrSlump\Protobuf;
  5. Protobuf::autoload();
  6. include_once __DIR__ . '/protos/simple.php';
  7. include_once __DIR__ . '/protos/complex.php';
  8. include_once __DIR__ . '/protos/repeated.php';
  9. include_once __DIR__ . '/protos/addressbook.php';
  10. include_once __DIR__ . '/protos/extension.php';
  11. // Include some hamcrest matchers manually since they are not included by default
  12. // TODO: Fix spec4php to include them
  13. include_once 'Hamcrest/Core/IsNot.php';
  14. include_once 'Hamcrest/Core/AllOf.php';
  15. describe "Binary Codec"
  16. before
  17. $codec = new Protobuf\Codec\Binary();
  18. Protobuf::setDefaultCodec($codec);
  19. $W->bin_simple = file_get_contents(__DIR__ . '/protos/simple.bin');
  20. $W->bin_book = file_get_contents(__DIR__ . '/protos/addressbook.bin');
  21. $W->bin_repeated_string = file_get_contents(__DIR__ . '/protos/repeated-string.bin');
  22. $W->bin_repeated_int32 = file_get_contents(__DIR__ . '/protos/repeated-int32.bin');
  23. $W->bin_repeated_nested = file_get_contents(__DIR__ . '/protos/repeated-nested.bin');
  24. $W->bin_ext = file_get_contents(__DIR__ . '/protos/extension.bin');
  25. end;
  26. describe "serialize"
  27. it "a simple message comparing types with protoc"
  28. $max = pow(2, 54)-1;
  29. $min = -$max;
  30. $fields = array(
  31. 'double' => array(1, 0.1, 1.0, -1, -0.1, -100000, 123456789.12345, -123456789.12345),
  32. 'float' => array(1, 0.1, 1.0, -1, -0.1, -100000, 12345.123, -12345.123),
  33. 'int64' => array(0, 1, -1, 123456789123456789, -123456789123456789, $min),
  34. 'uint64' => array(0, 1, 1000, 123456789123456789, PHP_INT_MAX, $max),
  35. 'int32' => array(0, 1, -1, 123456789, -123456789),
  36. 'fixed64' => array(0, 1, 1000, 123456789123456789),
  37. 'fixed32' => array(0, 1, 1000, 123456789),
  38. 'bool' => array(0, 1),
  39. 'string' => array("", "foo"),
  40. 'bytes' => array("", "foo"),
  41. 'uint32' => array(0, 1, 1000, 123456789),
  42. 'sfixed32' => array(0, 1, -1, 123456789, -123456789),
  43. 'sfixed64' => array(0, 1, -1, 123456789123456789, -123456789123456789),
  44. 'sint32' => array(0, 1, -1, 123456789, -123456789),
  45. 'sint64' => array(0, 1, -1, 123456789123456789, -123456789123456789, $min, $max),
  46. );
  47. foreach ($fields as $field=>$values) {
  48. foreach ($values as $value) {
  49. $simple = new Tests\Simple();
  50. $simple->$field = $value;
  51. $bin = Protobuf::encode($simple);
  52. if (is_string($value)) $value = '"' . $value . '"';
  53. exec("echo '$field: $value' | protoc --encode=tests.Simple -Itests tests/protos/simple.proto", $out);
  54. $out = implode("\n", $out);
  55. $printValue = var_export($value, true);
  56. bin2hex($bin) should eq (bin2hex($out)) as "Encoding $field with value $printValue";
  57. }
  58. }
  59. foreach ($fields as $field=>$values) {
  60. foreach ($values as $value) {
  61. $cmdValue = is_string($value)
  62. ? '"' . $value . '"'
  63. : $value;
  64. exec("echo '$field: $cmdValue' | protoc --encode=tests.Simple -Itests tests/protos/simple.proto", $out);
  65. $out = implode("\n", $out);
  66. $simple = Protobuf::decode('\tests\Simple', $out);
  67. // Hack the comparison for float precision
  68. if (is_float($simple->$field)) {
  69. $precision = strlen($value) - strpos($value, '.');
  70. $simple->$field = round($simple->$field, $precision);
  71. }
  72. $printValue = var_export($value, true);
  73. $simple->$field should eq $value as "Decoding $field with value $printValue";
  74. }
  75. }
  76. end.
  77. it. "an enum comparing with protoc".
  78. $complex = new Tests\Complex();
  79. exec("echo 'enum: FOO' | protoc --encode=tests.Complex -Itests tests/protos/complex.proto", $protocbin);
  80. $protocbin = implode("\n", $protocbin);
  81. // Check encoding an enum
  82. $complex->enum = Tests\Complex\Enum::FOO;
  83. $encbin = Protobuf::encode($complex);
  84. bin2hex($encbin) should eq (bin2hex($protocbin)) as "Encoding Enum field";
  85. // Check decoding an enum
  86. $complex = Protobuf::decode('\tests\Complex', $protocbin);
  87. $complex->enum should eq (Tests\Complex\Enum::FOO) as "Decoding Enum field";
  88. end.
  89. it. "a nested message comparing with protoc"
  90. exec("echo 'nested { foo: \"FOO\" }' | protoc --encode=tests.Complex -Itests tests/protos/complex.proto", $protocbin);
  91. $protocbin = implode("\n", $protocbin);
  92. // Encoding
  93. $complex = new Tests\Complex();
  94. $complex->nested = new Tests\Complex\Nested();
  95. $complex->nested->foo = 'FOO';
  96. $encbin = Protobuf::encode($complex);
  97. bin2hex($encbin) should eq (bin2hex($protocbin)) as "Encoding nested message";
  98. // Decoding
  99. $complex = Protobuf::decode('\tests\Complex', $protocbin);
  100. $complex->nested->foo should eq 'FOO' as "Decoding nested message";
  101. end.
  102. it. "a message with repeated fields"
  103. $repeated = new Tests\Repeated();
  104. $repeated->addString('one');
  105. $repeated->addString('two');
  106. $repeated->addString('three');
  107. $bin = Protobuf::encode($repeated);
  108. $bin should be $W->bin_repeated_string;
  109. $repeated = new Tests\Repeated();
  110. $repeated->addInt(1);
  111. $repeated->addInt(2);
  112. $repeated->addInt(3);
  113. $bin = Protobuf::encode($repeated);
  114. $bin should be $W->bin_repeated_int32;
  115. $repeated = new Tests\Repeated();
  116. $nested = new Tests\Repeated\Nested();
  117. $nested->setId(1);
  118. $repeated->addNested($nested);
  119. $nested = new Tests\Repeated\Nested();
  120. $nested->setId(2);
  121. $repeated->addNested($nested);
  122. $nested = new Tests\Repeated\Nested();
  123. $nested->setId(3);
  124. $repeated->addNested($nested);
  125. $bin = Protobuf::encode($repeated);
  126. $bin should eq $W->bin_repeated_nested;
  127. end.
  128. it. "a complex message"
  129. $book = new Tests\AddressBook();
  130. $person = new Tests\Person();
  131. $person->name = 'John Doe';
  132. $person->id = 2051;
  133. $person->email = 'john.doe@gmail.com';
  134. $phone = new Tests\Person\PhoneNumber;
  135. $phone->number = '1231231212';
  136. $phone->type = Tests\Person\PhoneType::HOME;
  137. $person->addPhone($phone);
  138. $phone = new Tests\Person\PhoneNumber;
  139. $phone->number = '55512321312';
  140. $phone->type = Tests\Person\PhoneType::MOBILE;
  141. $person->addPhone($phone);
  142. $book->addPerson($person);
  143. $person = new Tests\Person();
  144. $person->name = 'Iván Montes';
  145. $person->id = 23;
  146. $person->email = 'drslump@pollinimini.net';
  147. $phone = new Tests\Person\PhoneNumber;
  148. $phone->number = '3493123123';
  149. $phone->type = Tests\Person\PhoneType::WORK;
  150. $person->addPhone($phone);
  151. $book->addPerson($person);
  152. $bin = Protobuf::encode($book);
  153. $bin should eq $W->bin_book but not be false;
  154. end.
  155. it 'a message with extended fields'
  156. $ext = new Tests\ExtA();
  157. $ext->first = 'FIRST';
  158. $ext['tests\ExtB\second'] = 'SECOND';
  159. $bin = Protobuf::encode($ext);
  160. $bin should eq $W->bin_ext but not be false;
  161. end
  162. end;
  163. describe "unserialize"
  164. it "a simple message"
  165. $simple = Protobuf::decode('Tests\Simple', $W->bin_simple);
  166. $simple should be instanceof 'Tests\Simple';
  167. $simple->string should be 'foo';
  168. $simple->int32 should be -123456789;
  169. end.
  170. it "a message with repeated fields"
  171. $repeated = Protobuf::decode('Tests\Repeated', $W->bin_repeated_string);
  172. $repeated should be instanceof 'Tests\Repeated';
  173. $repeated->getString() should eq array('one', 'two', 'three');
  174. $repeated = Protobuf::decode('Tests\Repeated', $W->bin_repeated_int32);
  175. $repeated should be instanceof 'Tests\Repeated';
  176. $repeated->getInt() should eq array(1,2,3);
  177. $repeated = Protobuf::decode('Tests\Repeated', $W->bin_repeated_nested);
  178. $repeated should be instanceof 'Tests\Repeated';
  179. foreach ($repeated->getNested() as $i=>$nested) {
  180. $nested->getId() should eq ($i+1);
  181. }
  182. end.
  183. it "a complex message"
  184. $complex = Protobuf::decode('Tests\AddressBook', $W->bin_book);
  185. count($complex->person) should eq 2;
  186. $complex->getPerson(0)->name should eq 'John Doe';
  187. $complex->getPerson(1)->name should eq 'Iván Montes';
  188. $complex->getPerson(0)->getPhone(1)->number should eq '55512321312';
  189. end.
  190. it 'a message with extended fields'
  191. $ext = Protobuf::decode('Tests\ExtA', $W->bin_ext);
  192. $ext->first should eq 'FIRST';
  193. $ext['tests\ExtB\second'] should eq 'SECOND';
  194. end
  195. end;
  196. describe "multi codec"
  197. before
  198. $W->jsonCodec = new Protobuf\Codec\Json();
  199. end
  200. it "a simple message"
  201. $simple = Protobuf::decode('Tests\Simple', $W->bin_simple);
  202. $json = $W->jsonCodec->encode($simple);
  203. $simple = $W->jsonCodec->decode(new \Tests\Simple, $json);
  204. $bin = Protobuf::encode($simple);
  205. $bin should be $W->bin_simple;
  206. end.
  207. it "a message with repeated fields"
  208. $repeated = Protobuf::decode('Tests\Repeated', $W->bin_repeated_nested);
  209. $json = $W->jsonCodec->encode($repeated);
  210. $repeated = $W->jsonCodec->decode(new \Tests\Repeated, $json);
  211. $bin = Protobuf::encode($repeated);
  212. $bin should be $W->bin_repeated_nested;
  213. end.
  214. end;
  215. end;