BaseObjectTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php declare(strict_types=1);
  2. namespace Toolkit\StdlibTest\Obj;
  3. use Toolkit\StdlibTest\BaseLibTestCase;
  4. /**
  5. * @author inhere
  6. */
  7. class BaseObjectTest extends BaseLibTestCase
  8. {
  9. public function testUseSnake(): void
  10. {
  11. // use snake
  12. $po = new ADemoPo2(['one_two' => 'abc']);
  13. $this->assertEquals('abc', $po->oneTwo);
  14. // load real name
  15. $po->load(['oneTwo' => 'b235', 'notExists' => '234', 'str' => '']);
  16. $this->assertEquals('b235', $po->oneTwo);
  17. }
  18. public function testArray(): void
  19. {
  20. // array map
  21. $po = new ADemoPo2([
  22. 'arr' => [
  23. 'int' => 1,
  24. 'int2' => 0,
  25. 'str' => 'abc',
  26. 'str1' => '',
  27. 'arr' => [],
  28. 'bol' => false,
  29. ],
  30. ]);
  31. $data = $po->toCleaned();
  32. vdump($data);
  33. $this->assertArrayHasKey('arr', $data);
  34. $arr = $data['arr'];
  35. $this->assertArrayNotHasKey('int2', $arr);
  36. $this->assertArrayNotHasKey('str1', $arr);
  37. $po->arr = ['a', '', 'c'];
  38. $data = $po->toCleaned();
  39. vdump($data);
  40. $this->assertArrayHasKey('arr', $data);
  41. $arr = $data['arr'];
  42. $this->assertCount(3, $arr);
  43. }
  44. public function testSubArray(): void
  45. {
  46. // use snake
  47. $po = new ADemoPo2([
  48. 'str' => 'abc',
  49. 'obj' => new ADemoPo2([
  50. 'int' => 1,
  51. 'int2' => 0,
  52. 'str' => 'abc',
  53. 'str1' => '',
  54. 'arr' => [],
  55. ]),
  56. 'arrList' => [
  57. [
  58. 'sid' => 2070,
  59. 'points' => 0,
  60. 'give' => 0,
  61. 'comment' => 0,
  62. 'recharge' => 12299,
  63. 'invitation' => 0,
  64. 'rechargeGive' => 1,
  65. ],
  66. ],
  67. ]);
  68. $this->assertEquals('abc', $po->str);
  69. vdump($data = $po->toCleaned());
  70. $this->assertArrayHasKey('str', $data);
  71. $this->assertArrayHasKey('obj', $data);
  72. $this->assertArrayNotHasKey('int2', $data['obj']);
  73. $this->assertArrayNotHasKey('str1', $data['obj']);
  74. // sub array
  75. $this->assertArrayHasKey('arrList', $data);
  76. $this->assertNotEmpty($subItem = $data['arrList'][0]);
  77. $this->assertArrayHasKey('sid', $subItem);
  78. $this->assertArrayNotHasKey('give', $subItem);
  79. $this->assertArrayNotHasKey('comment', $subItem);
  80. $po->load([
  81. 'objList' => [
  82. new ADemoPo2([
  83. 'int' => 1,
  84. 'int2' => 0,
  85. 'str' => 'def',
  86. 'str1' => '',
  87. 'arr' => [],
  88. ]),
  89. ],
  90. ]);
  91. vdump($data = $po->toCleaned());
  92. // sub object
  93. $this->assertArrayHasKey('objList', $data);
  94. $this->assertNotEmpty($subItem = $data['objList'][0]);
  95. $this->assertArrayHasKey('str', $subItem);
  96. $this->assertArrayNotHasKey('str1', $subItem);
  97. }
  98. public function testUseJsonMap(): void
  99. {
  100. // use snake
  101. $po = new ADemoPo2(['one_two' => 'abc']);
  102. $this->assertEquals('abc', $po->oneTwo);
  103. // use alias name
  104. $po->load(['ot' => 'd34'], ['ot' => 'oneTwo']);
  105. $this->assertEquals('d34', $po->oneTwo);
  106. // use alias name in class _aliasMap
  107. $po->load(['ot2' => 'a23']);
  108. $this->assertEquals('a23', $po->oneTwo2);
  109. // to array map
  110. $arr = $po->setValue('str', '')->toMap();
  111. vdump(get_object_vars($po), $arr);
  112. $this->assertArrayHasKey('str', $arr);
  113. $arr = $po->toMap(true);
  114. vdump($arr);
  115. $this->assertArrayHasKey('oneTwo', $arr);
  116. $this->assertArrayHasKey('oneTwo2', $arr);
  117. $this->assertArrayNotHasKey('str', $arr);
  118. // to aliased
  119. $arr = $po->toAliased(true);
  120. vdump($arr);
  121. $this->assertArrayHasKey('oneTwo', $arr);
  122. $this->assertArrayHasKey('ot2', $arr);
  123. $this->assertArrayNotHasKey('oneTwo2', $arr);
  124. $this->assertArrayNotHasKey('str', $arr);
  125. }
  126. }