UtilsTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. }
  93. public function testEmpty()
  94. {
  95. $this->assertTrue(Utils::_empty(''));
  96. $this->assertFalse(Utils::_empty('not empty'));
  97. }
  98. public function testEqualString()
  99. {
  100. $this->assertTrue(Utils::equalString('a', 'a'));
  101. $this->assertFalse(Utils::equalString('a', 'b'));
  102. }
  103. public function testEqualNumber()
  104. {
  105. $this->assertTrue(Utils::equalNumber(1, 1));
  106. $this->assertFalse(Utils::equalNumber(1, 2));
  107. }
  108. public function testIsUnset()
  109. {
  110. $this->assertTrue(Utils::isUnset($a));
  111. $b = 1;
  112. $this->assertFalse(Utils::isUnset($b));
  113. }
  114. public function testStringifyMapValue()
  115. {
  116. $this->assertEquals([], Utils::stringifyMapValue(null));
  117. $this->assertEquals([
  118. 'foo' => 'bar',
  119. 'null' => '',
  120. 'true' => 'true',
  121. 'false' => 'false',
  122. 'number' => '1000',
  123. ], Utils::stringifyMapValue([
  124. 'foo' => 'bar',
  125. 'null' => null,
  126. 'true' => true,
  127. 'false' => false,
  128. 'number' => 1000,
  129. ]));
  130. }
  131. public function testAnyifyMapValue()
  132. {
  133. $this->assertEquals([
  134. 'foo' => 'bar',
  135. 'null' => null,
  136. 'true' => true,
  137. 'false' => false,
  138. 'number' => 1000,
  139. ], Utils::anyifyMapValue([
  140. 'foo' => 'bar',
  141. 'null' => null,
  142. 'true' => true,
  143. 'false' => false,
  144. 'number' => 1000,
  145. ]));
  146. }
  147. public function testAssertAsBoolean()
  148. {
  149. $this->assertTrue(Utils::assertAsBoolean(true));
  150. $this->assertFalse(Utils::assertAsBoolean('true'));
  151. }
  152. public function testAssertAsString()
  153. {
  154. $this->assertTrue(Utils::assertAsString('123'));
  155. $this->assertFalse(Utils::assertAsString(123));
  156. }
  157. public function testAssertAsBytes()
  158. {
  159. // failed because $var is not array
  160. $this->assertFalse(Utils::assertAsBytes('test'));
  161. // failed because $var is map not array
  162. $this->assertFalse(Utils::assertAsBytes(['foo' => 1]));
  163. // failed because item value is not int
  164. $this->assertFalse(Utils::assertAsBytes(['1']));
  165. // failed because item value is out off range
  166. $this->assertFalse(Utils::assertAsBytes([256]));
  167. // success
  168. $this->assertTrue(Utils::assertAsBytes([1, 2, 3]));
  169. $this->assertTrue(Utils::assertAsBytes(Utils::toBytes('string')));
  170. }
  171. public function testAssertAsNumber()
  172. {
  173. $this->assertTrue(Utils::assertAsNumber(123));
  174. $this->assertFalse(Utils::assertAsNumber('string'));
  175. }
  176. public function testAssertAsMap()
  177. {
  178. $this->expectException(\InvalidArgumentException::class);
  179. $this->expectExceptionMessage('It is not a map value.');
  180. Utils::assertAsMap('is not array');
  181. try {
  182. $map = ['foo' => 'bar'];
  183. $this->assertEquals($map, Utils::assertAsMap($map));
  184. } catch (\Exception $e) {
  185. // should not be here
  186. $this->assertTrue(false);
  187. }
  188. }
  189. public function testAssertAsArray()
  190. {
  191. $this->expectException(\InvalidArgumentException::class);
  192. $this->expectExceptionMessage('It is not a array value.');
  193. Utils::assertAsArray('is not array');
  194. try {
  195. $map = ['foo'];
  196. $this->assertEquals($map, Utils::assertAsArray($map));
  197. } catch (\Exception $e) {
  198. // should not be here
  199. $this->assertTrue(false);
  200. }
  201. }
  202. public function testGetUserAgent()
  203. {
  204. $this->assertTrue(false !== strpos(Utils::getUserAgent('CustomUserAgent'), 'CustomUserAgent'));
  205. }
  206. public function testIs2xx()
  207. {
  208. $this->assertTrue(Utils::is2xx(200));
  209. $this->assertFalse(Utils::is2xx(300));
  210. }
  211. public function testIs3xx()
  212. {
  213. $this->assertTrue(Utils::is3xx(300));
  214. $this->assertFalse(Utils::is3xx(400));
  215. }
  216. public function testIs4xx()
  217. {
  218. $this->assertTrue(Utils::is4xx(400));
  219. $this->assertFalse(Utils::is4xx(500));
  220. }
  221. public function testIs5xx()
  222. {
  223. $this->assertTrue(Utils::is5xx(500));
  224. $this->assertFalse(Utils::is5xx(600));
  225. }
  226. public function testToMap()
  227. {
  228. $from = new RequestTest();
  229. $from->query = new RequestTestQuery([
  230. 'booleanParamInQuery' => true,
  231. 'mapParamInQuery' => [1, 2, 3],
  232. ]);
  233. $this->assertTrue($from->query->booleanParamInQuery);
  234. $this->assertEquals([1, 2, 3], $from->query->mapParamInQuery);
  235. $target = new RequestShrinkTest([]);
  236. $this->convert($from, $target);
  237. $this->assertEquals([
  238. 'BooleanParamInQuery' => true,
  239. 'MapParamInQuery' => [1, 2, 3],
  240. ], $target->query->toMap());
  241. $target->query->mapParamInQueryShrink = json_encode($from->query->mapParamInQuery);
  242. $this->assertEquals([
  243. 'BooleanParamInQuery' => true,
  244. 'MapParamInQuery' => '[1,2,3]',
  245. ], Utils::toMap($target->query));
  246. }
  247. public function testSleep()
  248. {
  249. $before = microtime(true) * 1000;
  250. Utils::sleep(1000);
  251. $after = microtime(true) * 1000;
  252. $sub = $after - $before;
  253. $this->assertTrue(990 <= $sub && $sub <= 1100);
  254. }
  255. public function testToArray()
  256. {
  257. $model = new RequestTest();
  258. $model->query = 'foo';
  259. $this->assertEquals([
  260. ['query' => 'foo'],
  261. ], Utils::toArray([$model]));
  262. $subModel = new RequestTest();
  263. $subModel->query = 'bar';
  264. $model->query = $subModel;
  265. $this->assertEquals([
  266. ['query' => ['query' => 'bar']],
  267. ], Utils::toArray([$model]));
  268. }
  269. public function testAssertAsReadable()
  270. {
  271. $s0 = Utils::assertAsReadable('string content');
  272. $this->assertTrue($s0 instanceof Stream);
  273. $s1 = Utils::assertAsReadable($s0);
  274. $this->assertEquals($s1, $s0);
  275. $this->expectException(\InvalidArgumentException::class);
  276. $this->expectExceptionMessage('It is not a stream value.');
  277. Utils::assertAsReadable(0);
  278. }
  279. private function convert($body, &$content)
  280. {
  281. $class = new \ReflectionClass($body);
  282. foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
  283. $name = $property->getName();
  284. if (!$property->isStatic()) {
  285. $value = $property->getValue($body);
  286. if ($value instanceof StreamInterface) {
  287. continue;
  288. }
  289. $content->{$name} = $value;
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * @internal
  296. * @coversNothing
  297. */
  298. class RequestTest extends Model
  299. {
  300. /**
  301. * @var RequestTestQuery
  302. */
  303. public $query;
  304. }
  305. /**
  306. * @internal
  307. * @coversNothing
  308. */
  309. class RequestShrinkTest extends Model
  310. {
  311. /**
  312. * @var RequestTestShrinkQuery
  313. */
  314. public $query;
  315. }
  316. class RequestTestQuery extends Model
  317. {
  318. /**
  319. * @description test
  320. *
  321. * @var bool
  322. */
  323. public $booleanParamInQuery;
  324. /**
  325. * @description test
  326. *
  327. * @var array
  328. */
  329. public $mapParamInQuery;
  330. protected $_name = [
  331. 'booleanParamInQuery' => 'BooleanParamInQuery',
  332. 'mapParamInQuery' => 'MapParamInQuery',
  333. ];
  334. public function toMap()
  335. {
  336. $res = [];
  337. if (null !== $this->booleanParamInQuery) {
  338. $res['BooleanParamInQuery'] = $this->booleanParamInQuery;
  339. }
  340. if (null !== $this->mapParamInQuery) {
  341. $res['MapParamInQuery'] = $this->mapParamInQuery;
  342. }
  343. return $res;
  344. }
  345. }
  346. class RequestTestShrinkQuery extends Model
  347. {
  348. /**
  349. * @description test
  350. *
  351. * @var float
  352. */
  353. public $booleanParamInQuery;
  354. /**
  355. * @description test
  356. *
  357. * @var string
  358. */
  359. public $mapParamInQueryShrink;
  360. protected $_name = [
  361. 'booleanParamInQuery' => 'BooleanParamInQuery',
  362. 'mapParamInQueryShrink' => 'MapParamInQuery',
  363. ];
  364. public function toMap()
  365. {
  366. $res = [];
  367. if (null !== $this->booleanParamInQuery) {
  368. $res['BooleanParamInQuery'] = $this->booleanParamInQuery;
  369. }
  370. if (null !== $this->mapParamInQueryShrink) {
  371. $res['MapParamInQuery'] = $this->mapParamInQueryShrink;
  372. }
  373. return $res;
  374. }
  375. }