UtilsTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. namespace AlibabaCloud\Tea\Utils\Tests;
  3. use AlibabaCloud\Tea\Model;
  4. use AlibabaCloud\Tea\Utils\Utils;
  5. use GuzzleHttp\Psr7\Stream;
  6. use PHPUnit\Framework\TestCase;
  7. use Psr\Http\Message\StreamInterface;
  8. /**
  9. * @internal
  10. * @coversNothing
  11. */
  12. final class UtilsTest extends TestCase
  13. {
  14. public function getStream()
  15. {
  16. return new Stream(fopen('http://httpbin.org/get', 'r'));
  17. }
  18. public function testToBytes()
  19. {
  20. $this->assertEquals([
  21. 115, 116, 114, 105, 110, 103,
  22. ], Utils::toBytes('string'));
  23. }
  24. public function testToString()
  25. {
  26. $this->assertEquals('string', Utils::toString([
  27. 115, 116, 114, 105, 110, 103,
  28. ]));
  29. }
  30. public function testParseJSON()
  31. {
  32. $this->assertEquals([
  33. 'a' => 'b',
  34. ], Utils::parseJSON('{"a":"b"}'));
  35. }
  36. public function testReadAsBytes()
  37. {
  38. $bytes = Utils::readAsBytes($this->getStream());
  39. $this->assertEquals(123, $bytes[0]);
  40. }
  41. public function testReadAsString()
  42. {
  43. $string = Utils::readAsString($this->getStream());
  44. $this->assertEquals($string[0], '{');
  45. }
  46. public function testReadAsJSON()
  47. {
  48. $result = Utils::readAsJSON($this->getStream());
  49. $this->assertEquals('http://httpbin.org/get', $result['url']);
  50. }
  51. public function testGetNonce()
  52. {
  53. $nonce1 = Utils::getNonce();
  54. $nonce2 = Utils::getNonce();
  55. $this->assertNotEquals($nonce1, $nonce2);
  56. }
  57. public function testGetDateUTCString()
  58. {
  59. $gmdate = Utils::getDateUTCString();
  60. $now = time();
  61. $this->assertTrue(abs($now - strtotime($gmdate)) <= 1);
  62. }
  63. public function testDefaultString()
  64. {
  65. $this->assertEquals('', Utils::defaultString(null));
  66. $this->assertEquals('default', Utils::defaultString(null, 'default'));
  67. $this->assertEquals('real', Utils::defaultString('real', 'default'));
  68. }
  69. public function testDefaultNumber()
  70. {
  71. $this->assertEquals(0, Utils::defaultNumber(null));
  72. $this->assertEquals(0, Utils::defaultNumber(0, 3));
  73. $this->assertEquals(404, Utils::defaultNumber(null, 404));
  74. $this->assertEquals(200, Utils::defaultNumber(200, 404));
  75. }
  76. public function testToFormString()
  77. {
  78. $query = [
  79. 'foo' => 'bar',
  80. 'empty' => '',
  81. 'a' => null,
  82. 'withWhiteSpace' => 'a b',
  83. ];
  84. $this->assertEquals('foo=bar&empty=&withWhiteSpace=a%20b', Utils::toFormString($query));
  85. $object = json_decode(json_encode($query));
  86. $this->assertEquals('foo=bar&empty=&withWhiteSpace=a%20b', Utils::toFormString($object));
  87. }
  88. public function testToJSONString()
  89. {
  90. $object = new \stdClass();
  91. $this->assertJson(Utils::toJSONString($object));
  92. $this->assertEquals('[]', Utils::toJSONString([]));
  93. $this->assertEquals('["foo"]', Utils::toJSONString(['foo']));
  94. $this->assertEquals('{"str":"test","number":1,"bool":false,"null":null}', Utils::toJSONString([
  95. 'str' => 'test',
  96. 'number' => 1,
  97. 'bool' => FALSE,
  98. 'null' => null,
  99. ]));
  100. $this->assertEquals('1', Utils::toJSONString(1));
  101. $this->assertEquals('true', Utils::toJSONString(TRUE));
  102. $this->assertEquals('null', Utils::toJSONString(null));
  103. }
  104. public function testEmpty()
  105. {
  106. $this->assertTrue(Utils::_empty(''));
  107. $this->assertFalse(Utils::_empty('not empty'));
  108. }
  109. public function testEqualString()
  110. {
  111. $this->assertTrue(Utils::equalString('a', 'a'));
  112. $this->assertFalse(Utils::equalString('a', 'b'));
  113. }
  114. public function testEqualNumber()
  115. {
  116. $this->assertTrue(Utils::equalNumber(1, 1));
  117. $this->assertFalse(Utils::equalNumber(1, 2));
  118. }
  119. public function testIsUnset()
  120. {
  121. $this->assertTrue(Utils::isUnset($a));
  122. $b = 1;
  123. $this->assertFalse(Utils::isUnset($b));
  124. }
  125. public function testStringifyMapValue()
  126. {
  127. $this->assertEquals([], Utils::stringifyMapValue(null));
  128. $this->assertEquals([
  129. 'foo' => 'bar',
  130. 'null' => '',
  131. 'true' => 'true',
  132. 'false' => 'false',
  133. 'number' => '1000',
  134. ], Utils::stringifyMapValue([
  135. 'foo' => 'bar',
  136. 'null' => null,
  137. 'true' => true,
  138. 'false' => false,
  139. 'number' => 1000,
  140. ]));
  141. }
  142. public function testAnyifyMapValue()
  143. {
  144. $this->assertEquals([
  145. 'foo' => 'bar',
  146. 'null' => null,
  147. 'true' => true,
  148. 'false' => false,
  149. 'number' => 1000,
  150. ], Utils::anyifyMapValue([
  151. 'foo' => 'bar',
  152. 'null' => null,
  153. 'true' => true,
  154. 'false' => false,
  155. 'number' => 1000,
  156. ]));
  157. }
  158. public function testAssertAsBoolean()
  159. {
  160. $this->expectException(\InvalidArgumentException::class);
  161. $this->expectExceptionMessage('It is not a boolean value.');
  162. Utils::assertAsBoolean('true');
  163. try {
  164. $map = true;
  165. $this->assertEquals($map, Utils::assertAsBoolean($map));
  166. } catch (\Exception $e) {
  167. // should not be here
  168. $this->assertTrue(false);
  169. }
  170. }
  171. public function testAssertAsString()
  172. {
  173. $this->expectException(\InvalidArgumentException::class);
  174. $this->expectExceptionMessage('It is not a string value.');
  175. Utils::assertAsString(123);
  176. try {
  177. $map = '123';
  178. $this->assertEquals($map, Utils::assertAsString($map));
  179. } catch (\Exception $e) {
  180. // should not be here
  181. $this->assertTrue(false);
  182. }
  183. }
  184. public function testAssertAsBytes()
  185. {
  186. $this->expectException(\InvalidArgumentException::class);
  187. $this->expectExceptionMessage('It is not a bytes value.');
  188. // failed because $var is not array
  189. Utils::assertAsBytes('test');
  190. // failed because $var is map not array
  191. Utils::assertAsBytes(['foo' => 1]);
  192. // failed because item value is not int
  193. Utils::assertAsBytes(['1']);
  194. // failed because item value is out off range
  195. Utils::assertAsBytes([256]);
  196. try {
  197. // success
  198. $map = [1, 2, 3];
  199. $this->assertEquals($map, Utils::assertAsBytes($map));
  200. $this->assertEquals([
  201. 115, 116, 114, 105, 110, 103,
  202. ], Utils::assertAsBytes(Utils::toBytes('string')));
  203. } catch (\Exception $e) {
  204. // should not be here
  205. $this->assertTrue(false);
  206. }
  207. }
  208. public function testAssertAsNumber()
  209. {
  210. $this->expectException(\InvalidArgumentException::class);
  211. $this->expectExceptionMessage('It is not a number value.');
  212. Utils::assertAsNumber('is not number');
  213. try {
  214. $map = 123;
  215. $this->assertEquals($map, Utils::assertAsNumber($map));
  216. } catch (\Exception $e) {
  217. // should not be here
  218. $this->assertTrue(false);
  219. }
  220. }
  221. public function testAssertAsMap()
  222. {
  223. $this->expectException(\InvalidArgumentException::class);
  224. $this->expectExceptionMessage('It is not a map value.');
  225. Utils::assertAsMap('is not array');
  226. try {
  227. $map = ['foo' => 'bar'];
  228. $this->assertEquals($map, Utils::assertAsMap($map));
  229. } catch (\Exception $e) {
  230. // should not be here
  231. $this->assertTrue(false);
  232. }
  233. }
  234. public function testAssertAsArray()
  235. {
  236. $this->expectException(\InvalidArgumentException::class);
  237. $this->expectExceptionMessage('It is not a array value.');
  238. Utils::assertAsArray('is not array');
  239. try {
  240. $map = ['foo'];
  241. $this->assertEquals($map, Utils::assertAsArray($map));
  242. } catch (\Exception $e) {
  243. // should not be here
  244. $this->assertTrue(false);
  245. }
  246. }
  247. public function testGetUserAgent()
  248. {
  249. $this->assertTrue(false !== strpos(Utils::getUserAgent('CustomUserAgent'), 'CustomUserAgent'));
  250. }
  251. public function testIs2xx()
  252. {
  253. $this->assertTrue(Utils::is2xx(200));
  254. $this->assertFalse(Utils::is2xx(300));
  255. }
  256. public function testIs3xx()
  257. {
  258. $this->assertTrue(Utils::is3xx(300));
  259. $this->assertFalse(Utils::is3xx(400));
  260. }
  261. public function testIs4xx()
  262. {
  263. $this->assertTrue(Utils::is4xx(400));
  264. $this->assertFalse(Utils::is4xx(500));
  265. }
  266. public function testIs5xx()
  267. {
  268. $this->assertTrue(Utils::is5xx(500));
  269. $this->assertFalse(Utils::is5xx(600));
  270. }
  271. public function testToMap()
  272. {
  273. $from = new RequestTest();
  274. $from->query = new RequestTestQuery([
  275. 'booleanParamInQuery' => true,
  276. 'mapParamInQuery' => [1, 2, 3],
  277. ]);
  278. $this->assertTrue($from->query->booleanParamInQuery);
  279. $this->assertEquals([1, 2, 3], $from->query->mapParamInQuery);
  280. $target = new RequestShrinkTest([]);
  281. $this->convert($from, $target);
  282. $this->assertEquals([
  283. 'BooleanParamInQuery' => true,
  284. 'MapParamInQuery' => [1, 2, 3],
  285. ], $target->query->toMap());
  286. $target->query->mapParamInQueryShrink = json_encode($from->query->mapParamInQuery);
  287. $this->assertEquals([
  288. 'BooleanParamInQuery' => true,
  289. 'MapParamInQuery' => '[1,2,3]',
  290. ], Utils::toMap($target->query));
  291. }
  292. public function testSleep()
  293. {
  294. $before = microtime(true) * 1000;
  295. Utils::sleep(1000);
  296. $after = microtime(true) * 1000;
  297. $sub = $after - $before;
  298. $this->assertTrue(990 <= $sub && $sub <= 1100);
  299. }
  300. public function testToArray()
  301. {
  302. $model = new RequestTest();
  303. $model->query = 'foo';
  304. $this->assertEquals([
  305. ['query' => 'foo'],
  306. ], Utils::toArray([$model]));
  307. $subModel = new RequestTest();
  308. $subModel->query = 'bar';
  309. $model->query = $subModel;
  310. $this->assertEquals([
  311. ['query' => ['query' => 'bar']],
  312. ], Utils::toArray([$model]));
  313. }
  314. public function testAssertAsReadable()
  315. {
  316. $s0 = Utils::assertAsReadable('string content');
  317. $this->assertTrue($s0 instanceof Stream);
  318. $s1 = Utils::assertAsReadable($s0);
  319. $this->assertEquals($s1, $s0);
  320. $this->expectException(\InvalidArgumentException::class);
  321. $this->expectExceptionMessage('It is not a stream value.');
  322. Utils::assertAsReadable(0);
  323. }
  324. private function convert($body, &$content)
  325. {
  326. $class = new \ReflectionClass($body);
  327. foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
  328. $name = $property->getName();
  329. if (!$property->isStatic()) {
  330. $value = $property->getValue($body);
  331. if ($value instanceof StreamInterface) {
  332. continue;
  333. }
  334. $content->{$name} = $value;
  335. }
  336. }
  337. }
  338. }
  339. /**
  340. * @internal
  341. * @coversNothing
  342. */
  343. class RequestTest extends Model
  344. {
  345. /**
  346. * @var RequestTestQuery
  347. */
  348. public $query;
  349. }
  350. /**
  351. * @internal
  352. * @coversNothing
  353. */
  354. class RequestShrinkTest extends Model
  355. {
  356. /**
  357. * @var RequestTestShrinkQuery
  358. */
  359. public $query;
  360. }
  361. class RequestTestQuery extends Model
  362. {
  363. /**
  364. * @description test
  365. *
  366. * @var bool
  367. */
  368. public $booleanParamInQuery;
  369. /**
  370. * @description test
  371. *
  372. * @var array
  373. */
  374. public $mapParamInQuery;
  375. protected $_name = [
  376. 'booleanParamInQuery' => 'BooleanParamInQuery',
  377. 'mapParamInQuery' => 'MapParamInQuery',
  378. ];
  379. public function toMap()
  380. {
  381. $res = [];
  382. if (null !== $this->booleanParamInQuery) {
  383. $res['BooleanParamInQuery'] = $this->booleanParamInQuery;
  384. }
  385. if (null !== $this->mapParamInQuery) {
  386. $res['MapParamInQuery'] = $this->mapParamInQuery;
  387. }
  388. return $res;
  389. }
  390. }
  391. class RequestTestShrinkQuery extends Model
  392. {
  393. /**
  394. * @description test
  395. *
  396. * @var float
  397. */
  398. public $booleanParamInQuery;
  399. /**
  400. * @description test
  401. *
  402. * @var string
  403. */
  404. public $mapParamInQueryShrink;
  405. protected $_name = [
  406. 'booleanParamInQuery' => 'BooleanParamInQuery',
  407. 'mapParamInQueryShrink' => 'MapParamInQuery',
  408. ];
  409. public function toMap()
  410. {
  411. $res = [];
  412. if (null !== $this->booleanParamInQuery) {
  413. $res['BooleanParamInQuery'] = $this->booleanParamInQuery;
  414. }
  415. if (null !== $this->mapParamInQueryShrink) {
  416. $res['MapParamInQuery'] = $this->mapParamInQueryShrink;
  417. }
  418. return $res;
  419. }
  420. }