tp6混合easyswoole DLL 多应用模式 修改版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
10 KiB

5 years ago
  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. $this->assertEquals(
  193. [
  194. 'color' => [
  195. 'red' => 12,
  196. 'blue' => 8,
  197. 'green' => 6
  198. ],
  199. 'fruit' => [
  200. 'apple' => 3,
  201. 'orange' => 1,
  202. 'grape' => 4
  203. ]
  204. ]
  205. ,$splArrayObj->asort()->getArrayCopy()
  206. );
  207. }
  208. /**
  209. * 按照键名排序
  210. *
  211. * @depends clone testSet
  212. * CreateTime: 2019/9/11 上午12:35
  213. * @param SplArray $splArrayObj
  214. */
  215. public function testKsort(SplArray $splArrayObj) {
  216. $this->assertEquals(
  217. [
  218. 'color' => [
  219. 'red' => 12,
  220. 'blue' => 8,
  221. 'green' => 6
  222. ],
  223. 'fruit' => [
  224. 'apple' => 3,
  225. 'orange' => 1,
  226. 'grape' => 4
  227. ]
  228. ],
  229. $splArrayObj->ksort()->getArrayCopy()
  230. );
  231. }
  232. /**
  233. * 排序
  234. *
  235. * @depends clone testSet
  236. * CreateTime: 2019/9/11 上午12:35
  237. * @param SplArray $splArrayObj
  238. */
  239. public function testSort(SplArray $splArrayObj) {
  240. $this->assertEquals(
  241. [
  242. [
  243. 'red' => 12,
  244. 'blue' => 8,
  245. 'green' => 6
  246. ],
  247. [
  248. 'apple' => 3,
  249. 'orange' => 1,
  250. 'grape' => 4
  251. ]
  252. ],
  253. $splArrayObj->sort()->getArrayCopy()
  254. );
  255. }
  256. /**
  257. * 取得某一列
  258. *
  259. * @depends clone testSet
  260. * CreateTime: 2019/9/11 上午12:35
  261. * @param SplArray $splArrayObj
  262. * @return bool
  263. */
  264. public function testColumn(SplArray $splArrayObj) {
  265. $this->assertEquals(
  266. [12],
  267. $splArrayObj->column('red')->getArrayCopy()
  268. );
  269. }
  270. /**
  271. * 交换数组中的键和值
  272. */
  273. public function testFlip() {
  274. $splArrayObj = new SplArray([
  275. 'es' => 'easyswoole'
  276. ]);
  277. $this->assertEquals(
  278. [
  279. 'easyswoole' => 'es'
  280. ],
  281. $splArrayObj->flip()->getArrayCopy()
  282. );
  283. }
  284. /**
  285. * 过滤数组数据
  286. */
  287. public function testFilter() {
  288. $splArrayObj = new SplArray(
  289. [
  290. 'apple' => 2,
  291. 'orange' => 1,
  292. 'grape' => 2,
  293. 'pear' => 4,
  294. 'banana' => 8
  295. ]
  296. );
  297. // 获取设置的键名
  298. $this->assertEquals(
  299. [
  300. 'apple' => 2,
  301. 'pear' => 4
  302. ],
  303. $splArrayObj->filter('apple,pear', false)->getArrayCopy()
  304. );
  305. // 排除设置的键名
  306. $this->assertEquals(
  307. [
  308. 'apple' => 2,
  309. 'pear' => 4
  310. ],
  311. $splArrayObj->filter('orange,grape,banana', true)->getArrayCopy()
  312. );
  313. }
  314. /**
  315. * 获取数组索引
  316. *
  317. * @depends clone testSet
  318. * @param SplArray $splArrayObj
  319. */
  320. public function testKeys(SplArray $splArrayObj) {
  321. $this->assertEquals(
  322. ['red', 'blue', 'green'],
  323. $splArrayObj->keys('color')
  324. );
  325. }
  326. /**
  327. * 获取数组中所有的值
  328. *
  329. * @depends clone testSet
  330. * @param SplArray $splArrayObj
  331. */
  332. public function testValues(SplArray $splArrayObj) {
  333. $this->assertEquals(
  334. [
  335. [
  336. 'apple' => 3,
  337. 'orange' => 1,
  338. 'grape' => 4
  339. ],
  340. [
  341. 'red' => 12,
  342. 'blue' => 8,
  343. 'green' => 6
  344. ]
  345. ],
  346. $splArrayObj->values()->getArrayCopy()
  347. );
  348. }
  349. /**
  350. * 清空数据
  351. *
  352. * @depends clone testSet
  353. * @param SplArray $splArrayObj
  354. */
  355. public function testFlush(SplArray $splArrayObj) {
  356. $this->assertEquals(
  357. [],
  358. $splArrayObj->flush()->getArrayCopy()
  359. );
  360. }
  361. /**
  362. * 重新加载数据
  363. *
  364. * @depends clone testSet
  365. * @param SplArray $splArrayObj
  366. */
  367. public function testLoadArray(SplArray $splArrayObj) {
  368. $this->assertEquals(
  369. [
  370. 'name' => 'easyswoole'
  371. ],
  372. $splArrayObj->loadArray(
  373. [
  374. 'name' => 'easyswoole'
  375. ]
  376. )->getArrayCopy()
  377. );
  378. }
  379. /**
  380. * 转化成xml
  381. *
  382. * @depends clone testSet
  383. * @param SplArray $splArrayObj
  384. */
  385. public function testToXML(SplArray $splArrayObj) {
  386. $this->assertEquals(
  387. "<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",
  388. $splArrayObj->toXML()
  389. );
  390. }
  391. }