ArrayTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * @CreateTime: 2019/9/9 下午11:28
  4. * @Author: huizhang <tuzisir@163.com>
  5. * @Copyright: copyright(2019) Easyswoole all rights reserved
  6. * @Description: SplArray 单元测试
  7. */
  8. namespace EasySwoole\Spl\Test;
  9. use PHPUnit\Framework\TestCase;
  10. use EasySwoole\Spl\SplArray;
  11. class ArrayTest extends TestCase {
  12. /**
  13. * 设置参数
  14. *
  15. * @return SplArray
  16. * CreateTime: 2019/9/10 下午11:30
  17. */
  18. public function testSet() {
  19. $data = [
  20. 'fruit' => [
  21. 'apple' => 2,
  22. 'orange' => 1,
  23. 'grape' => 4
  24. ],
  25. 'color' => [
  26. 'red' => 12,
  27. 'blue' => 8,
  28. 'green' => 6
  29. ]
  30. ];
  31. $splArrayObj = new SplArray($data);
  32. $splArrayObj->set('fruit.apple', 3);
  33. $this->assertEquals(3, $splArrayObj->get('fruit.apple'));
  34. return $splArrayObj;
  35. }
  36. /**
  37. * 获取参数
  38. *
  39. * @depends clone testSet
  40. * @param SplArray $splArrayObj
  41. */
  42. public function testGet( SplArray $splArrayObj) {
  43. // 测试第一层的key
  44. $this->assertEquals(
  45. [
  46. 'red' => 12,
  47. 'blue' => 8,
  48. 'green' => 6
  49. ],
  50. $splArrayObj->get('color')
  51. );
  52. // 测试第二层的key
  53. $this->assertEquals(
  54. 12,
  55. $splArrayObj->get('color.red')
  56. );
  57. }
  58. /**
  59. * 转字符
  60. *
  61. * @depends clone testSet
  62. * CreateTime: 2019/9/10 下午11:29
  63. */
  64. public function testTostring(SplArray $splArrayObj) {
  65. $this->assertJsonStringEqualsJsonString(
  66. json_encode($splArrayObj, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
  67. $splArrayObj->__toString()
  68. );
  69. }
  70. /**
  71. * 数组的复制
  72. *
  73. * @depends clone testSet
  74. * @param SplArray $splArrayObj
  75. * CreateTime: 2019/9/10 下午11:37
  76. */
  77. public function testGetArrayCopy(SplArray $splArrayObj) {
  78. $this->assertEquals(
  79. [
  80. 'fruit' => [
  81. 'apple' => 3,
  82. 'orange' => 1,
  83. 'grape' => 4
  84. ],
  85. 'color' => [
  86. 'red' => 12,
  87. 'blue' => 8,
  88. 'green' => 6
  89. ]
  90. ],
  91. $splArrayObj->getArrayCopy()
  92. );
  93. }
  94. /**
  95. * 销毁数组元素
  96. *
  97. * @depends clone testSet
  98. * @param $splArrayObj SplArray
  99. * CreateTime: 2019/9/10 下午11:44
  100. */
  101. public function testUnset(SplArray $splArrayObj) {
  102. // 销毁red元素
  103. $splArrayObj->unset('color.red');
  104. $this->assertEquals(
  105. [
  106. 'fruit' => [
  107. 'apple' => 3,
  108. 'orange' => 1,
  109. 'grape' => 4
  110. ],
  111. 'color' => [
  112. 'blue' => 8,
  113. 'green' => 6
  114. ]
  115. ],
  116. $splArrayObj->getArrayCopy()
  117. );
  118. // 销毁color元素
  119. $splArrayObj->unset('color');
  120. $this->assertEquals(
  121. [
  122. 'fruit' => [
  123. 'apple' => 3,
  124. 'orange' => 1,
  125. 'grape' => 4
  126. ]
  127. ],
  128. $splArrayObj->getArrayCopy()
  129. );
  130. }
  131. /**
  132. * 去除某个数据项(unset和delete方法其实是实现统一效果,因考虑旧版本用户使用情况,故而保留。)
  133. *
  134. * @depends clone testSet
  135. * @param SplArray $splArrayObj
  136. */
  137. public function testDelete(SplArray $splArrayObj) {
  138. $splArrayObj->delete('color');
  139. $this->assertEquals(
  140. [
  141. 'fruit' => [
  142. 'apple' => 3,
  143. 'orange' => 1,
  144. 'grape' => 4
  145. ]
  146. ],
  147. $splArrayObj->getArrayCopy()
  148. );
  149. }
  150. /**
  151. * 数组值唯一
  152. *
  153. * CreateTime: 2019/9/10 下午11:55
  154. * @param SplArray $splArrayObj
  155. * @return bool
  156. */
  157. public function testUnique() {
  158. $splArrayObj = new SplArray(
  159. [
  160. 'name1' => 'es',
  161. 'name2' => 'es'
  162. ]
  163. );
  164. $this->assertEquals(
  165. ['name1'=>'es']
  166. , $splArrayObj->unique()->getArrayCopy());
  167. }
  168. /**
  169. * 获取数组中重复的值
  170. *
  171. * @depends clone testSet
  172. * CreateTime: 2019/9/11 上午12:22
  173. * @return bool
  174. */
  175. public function testMultiple() {
  176. $splArrayObj = new SplArray(
  177. [
  178. 'name1' => 'es',
  179. 'name2' => 'es'
  180. ]
  181. );
  182. $this->assertEquals(['name2'=>'es'], $splArrayObj->multiple()->getArrayCopy());
  183. }
  184. /**
  185. * 进行排序并保持索引关系
  186. *
  187. * @depends clone testSet
  188. * CreateTime: 2019/9/11 上午12:32
  189. * @param SplArray $splArrayObj
  190. */
  191. public function testAsort(SplArray $splArrayObj) {
  192. $splArrayObj->asort();
  193. $this->assertEquals(
  194. [
  195. 'color' => [
  196. 'red' => 12,
  197. 'blue' => 8,
  198. 'green' => 6
  199. ],
  200. 'fruit' => [
  201. 'apple' => 3,
  202. 'orange' => 1,
  203. 'grape' => 4
  204. ]
  205. ]
  206. ,$splArrayObj->getArrayCopy()
  207. );
  208. }
  209. /**
  210. * 按照键名排序
  211. *
  212. * @depends clone testSet
  213. * CreateTime: 2019/9/11 上午12:35
  214. * @param SplArray $splArrayObj
  215. */
  216. public function testKsort(SplArray $splArrayObj) {
  217. $splArrayObj->ksort();
  218. $this->assertEquals(
  219. [
  220. 'color' => [
  221. 'red' => 12,
  222. 'blue' => 8,
  223. 'green' => 6
  224. ],
  225. 'fruit' => [
  226. 'apple' => 3,
  227. 'orange' => 1,
  228. 'grape' => 4
  229. ]
  230. ],
  231. $splArrayObj->getArrayCopy()
  232. );
  233. }
  234. /**
  235. * 排序
  236. *
  237. * @depends clone testSet
  238. * CreateTime: 2019/9/11 上午12:35
  239. * @param SplArray $splArrayObj
  240. */
  241. public function testSort(SplArray $splArrayObj) {
  242. $this->assertEquals(
  243. [
  244. [
  245. 'red' => 12,
  246. 'blue' => 8,
  247. 'green' => 6
  248. ],
  249. [
  250. 'apple' => 3,
  251. 'orange' => 1,
  252. 'grape' => 4
  253. ]
  254. ],
  255. $splArrayObj->sort()->getArrayCopy()
  256. );
  257. }
  258. /**
  259. * 取得某一列
  260. *
  261. * @depends clone testSet
  262. * CreateTime: 2019/9/11 上午12:35
  263. * @param SplArray $splArrayObj
  264. * @return bool
  265. */
  266. public function testColumn(SplArray $splArrayObj) {
  267. $this->assertEquals(
  268. [12],
  269. $splArrayObj->column('red')->getArrayCopy()
  270. );
  271. }
  272. /**
  273. * 交换数组中的键和值
  274. */
  275. public function testFlip() {
  276. $splArrayObj = new SplArray([
  277. 'es' => 'easyswoole'
  278. ]);
  279. $this->assertEquals(
  280. [
  281. 'easyswoole' => 'es'
  282. ],
  283. $splArrayObj->flip()->getArrayCopy()
  284. );
  285. }
  286. /**
  287. * 过滤数组数据
  288. */
  289. public function testFilter() {
  290. $splArrayObj = new SplArray(
  291. [
  292. 'apple' => 2,
  293. 'orange' => 1,
  294. 'grape' => 2,
  295. 'pear' => 4,
  296. 'banana' => 8
  297. ]
  298. );
  299. // 获取设置的键名
  300. $this->assertEquals(
  301. [
  302. 'apple' => 2,
  303. 'pear' => 4
  304. ],
  305. $splArrayObj->filter('apple,pear', false)->getArrayCopy()
  306. );
  307. // 排除设置的键名
  308. $this->assertEquals(
  309. [
  310. 'apple' => 2,
  311. 'pear' => 4
  312. ],
  313. $splArrayObj->filter('orange,grape,banana', true)->getArrayCopy()
  314. );
  315. }
  316. /**
  317. * 获取数组索引
  318. *
  319. * @depends clone testSet
  320. * @param SplArray $splArrayObj
  321. */
  322. public function testKeys(SplArray $splArrayObj) {
  323. $this->assertEquals(
  324. ['red', 'blue', 'green'],
  325. $splArrayObj->keys('color')
  326. );
  327. }
  328. /**
  329. * 获取数组中所有的值
  330. *
  331. * @depends clone testSet
  332. * @param SplArray $splArrayObj
  333. */
  334. public function testValues(SplArray $splArrayObj) {
  335. $this->assertEquals(
  336. [
  337. [
  338. 'apple' => 3,
  339. 'orange' => 1,
  340. 'grape' => 4
  341. ],
  342. [
  343. 'red' => 12,
  344. 'blue' => 8,
  345. 'green' => 6
  346. ]
  347. ],
  348. $splArrayObj->values()->getArrayCopy()
  349. );
  350. }
  351. /**
  352. * 清空数据
  353. *
  354. * @depends clone testSet
  355. * @param SplArray $splArrayObj
  356. */
  357. public function testFlush(SplArray $splArrayObj) {
  358. $this->assertEquals(
  359. [],
  360. $splArrayObj->flush()->getArrayCopy()
  361. );
  362. }
  363. /**
  364. * 重新加载数据
  365. *
  366. * @depends clone testSet
  367. * @param SplArray $splArrayObj
  368. */
  369. public function testLoadArray(SplArray $splArrayObj) {
  370. $this->assertEquals(
  371. [
  372. 'name' => 'easyswoole'
  373. ],
  374. $splArrayObj->loadArray(
  375. [
  376. 'name' => 'easyswoole'
  377. ]
  378. )->getArrayCopy()
  379. );
  380. }
  381. /**
  382. * 转化成xml
  383. *
  384. * @depends clone testSet
  385. * @param SplArray $splArrayObj
  386. */
  387. public function testToXML(SplArray $splArrayObj) {
  388. $this->assertEquals(
  389. "<xml><fruit><apple>3</apple><orange>1</orange><grape>4</grape></fruit><color><red>12</red><blue>8</blue><green>6</green></color></xml>\n",
  390. $splArrayObj->toXML()
  391. );
  392. }
  393. public function testMulti()
  394. {
  395. $splArray = new SplArray(
  396. [
  397. 'a'=>[
  398. "sum"=>'a1',
  399. [
  400. "sum"=>'s1',
  401. ],
  402. [
  403. "sum"=>'s2',
  404. ],
  405. ],
  406. 'b'=>'b',
  407. 'c'=>[
  408. "sum"=>'c1'
  409. ],
  410. ]
  411. );
  412. $this->assertEquals([
  413. null,'s1','s2'
  414. ],$splArray->get('a.*.sum'));
  415. $this->assertEquals([
  416. 'a1',null,'c1'
  417. ],$splArray->get('*.sum'));
  418. $this->assertEquals([
  419. 'a1',
  420. [
  421. "sum"=>'s1',
  422. ],
  423. [
  424. "sum"=>'s2',
  425. ],
  426. ],$splArray->get('a.*'));
  427. }
  428. }