BeanTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @CreateTime: 2019/9/13 下午02:18
  4. * @Author: huizhang <tuzisir@163.com>
  5. * @Copyright: copyright(2019) Easyswoole all rights reserved
  6. * @Description: SplBean 单元测试
  7. */
  8. namespace EasySwoole\Spl\Test;
  9. use EasySwoole\Spl\Test\Bean\Shops;
  10. use PHPUnit\Framework\TestCase;
  11. use EasySwoole\Spl\Test\Bean\TestBean;
  12. class BeanTest extends TestCase
  13. {
  14. /**
  15. * 获取类所有的public和protected 成员变量
  16. */
  17. function testAllProperty() {
  18. $bean = new TestBean([
  19. 'a'=>'a',
  20. 'b'=>'b',
  21. 'c'=>'c',
  22. 'd_d'=>'d_d'
  23. ]);
  24. $this->assertEquals(
  25. ['a', 'b', 'd_d'],
  26. $bean->allProperty()
  27. );
  28. }
  29. /**
  30. * 过滤并转换成数组数据
  31. */
  32. function testToArray() {
  33. $bean = new TestBean([
  34. 'a'=>'a',
  35. 'b'=>'b',
  36. 'c'=>'c',
  37. 'd_d'=>'d_d'
  38. ]);
  39. $res = $bean->toArray(null, function ($a) {
  40. if (in_array($a, ['d_d'])) {
  41. return $a;
  42. }
  43. });
  44. $this->assertEquals(
  45. ['d_d' => 'd_d'],
  46. $res
  47. );
  48. }
  49. /**
  50. *获取过滤后带有字段别名的数组数据
  51. */
  52. function testToArrayWithMapping() {
  53. $bean = new TestBean([
  54. 'a'=>1,
  55. 'b'=>2,
  56. 'c'=>3,
  57. 'd_d'=>4
  58. ]);
  59. $res = $bean->toArrayWithMapping(['a', 'b', 'd-d'], function ($val) {
  60. return $val;
  61. });
  62. $this->assertEquals(
  63. [
  64. 'a' => 1,
  65. 'b' => 2,
  66. 'd-d' => 4
  67. ],
  68. $res
  69. );
  70. }
  71. /**
  72. * 设置类属性
  73. */
  74. function testArrayToBean()
  75. {
  76. $bean = new TestBean([
  77. 'a'=>'a',
  78. 'b'=>'b',
  79. 'c'=>'c',
  80. 'd_d'=>'d_d'
  81. ]);
  82. $this->assertEquals([
  83. 'a'=>'a',
  84. 'b'=>'b',
  85. 'd_d'=>'d_d'
  86. ],$bean->toArray());
  87. $this->assertEquals([
  88. 'a'=>'a',
  89. 'b'=>'b',
  90. ],$bean->toArray(['a','b']));
  91. $this->assertEquals([
  92. 'a'=>'a',
  93. 'd-d'=>'d_d'
  94. ],$bean->toArrayWithMapping(['a','d-d']));
  95. }
  96. /**
  97. * 设置类成员变量
  98. */
  99. function testAddProperty() {
  100. $bean = new TestBean();
  101. $bean->addProperty('a', 'es');
  102. $bean->addProperty('b', 'es');
  103. $bean->addProperty('d_d', 'es');
  104. $this->assertEquals(
  105. [
  106. 'a' => 'es',
  107. 'b' => 'es',
  108. 'd_d' => 'es',
  109. ],
  110. $bean->toArray()
  111. );
  112. }
  113. /**
  114. * 获取类成员变量值
  115. */
  116. function testGetProperty() {
  117. $bean = new TestBean([
  118. 'a'=>'a',
  119. 'b'=>'b',
  120. 'c'=>'c',
  121. 'd_d'=>'d_d'
  122. ]);
  123. $this->assertEquals('a', $bean->getProperty('a'));
  124. }
  125. /**
  126. * 获取类成员变量集合
  127. */
  128. function testJsonSerialize() {
  129. $bean = new TestBean([
  130. 'a'=>'a',
  131. 'b'=>'b',
  132. 'c'=>'c',
  133. 'd_d'=>'d_d'
  134. ]);
  135. $this->assertEquals(
  136. [
  137. 'a'=>'a',
  138. 'b'=>'b',
  139. 'd_d'=>'d_d'
  140. ],
  141. $bean->jsonSerialize()
  142. );
  143. }
  144. /**
  145. * 初始化操作
  146. */
  147. function testInitialize() {
  148. $bean = new TestBean([
  149. 'a'=>'a',
  150. 'b'=>'b',
  151. 'c'=>'c',
  152. 'd_d'=>'d_d'
  153. ]);
  154. $this->assertEquals(
  155. [
  156. 'a'=>'a',
  157. 'b'=>'b',
  158. 'd_d'=>'d_d'
  159. ],
  160. $bean->jsonSerialize()
  161. );
  162. }
  163. /**
  164. * 设置keyMapping关系,也就是字段别名
  165. */
  166. function testSetKeyMapping() {
  167. $bean = new TestBean([
  168. 'a'=>'a',
  169. 'b'=>'b',
  170. 'c'=>'c',
  171. 'd-d'=>'d'
  172. ]);
  173. $this->assertEquals(
  174. [
  175. 'a'=>'a',
  176. 'b'=>'b',
  177. 'd_d'=>'d'
  178. ],
  179. $bean->jsonSerialize()
  180. );
  181. }
  182. /**
  183. * 设置classMapping关系,也就是关联类
  184. */
  185. // function testSetClassMapping() {
  186. // return true;
  187. // $bean = new TestBean([
  188. // 'a'=>'a',
  189. // 'b'=>'b',
  190. // 'c'=>'c',
  191. // 'd-d'=>'d'
  192. // ]);
  193. // $this->assertEquals(
  194. // Shops::class,
  195. // get_class($bean->jsonSerialize()['shops'])
  196. // );
  197. // }
  198. function testRestore()
  199. {
  200. $bean = new TestBean([
  201. 'a'=>'a',
  202. 'b'=>'b',
  203. 'c'=>'c',
  204. 'd_d'=>'d_d'
  205. ]);
  206. $this->assertEquals([
  207. 'a'=>2,
  208. 'b'=>null,
  209. 'd_d'=>null
  210. ],$bean->restore()->toArray());
  211. $this->assertEquals([
  212. 'a'=>2
  213. ],$bean->restore()->toArray(null,$bean::FILTER_NOT_NULL));
  214. $bean->restore(['a'=>2,'b'=>3]);
  215. }
  216. }