UtilsTest.php 15 KB

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