CollectionTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Siam
  5. * Date: 2020/4/17
  6. * Time: 16:43
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\ORM\AbstractModel;
  10. use EasySwoole\ORM\Collection\Collection;
  11. use EasySwoole\ORM\Db\Config;
  12. use EasySwoole\ORM\Db\Connection;
  13. use EasySwoole\ORM\DbManager;
  14. use EasySwoole\ORM\Tests\models\TestUserListGetterModel;
  15. use EasySwoole\ORM\Tests\models\TestUserListModel;
  16. use PHPUnit\Framework\TestCase;
  17. class CollectionTest extends TestCase
  18. {
  19. /**
  20. * @var $connection Connection
  21. */
  22. protected $connection;
  23. protected $tableName = 'user_test_list';
  24. protected function setUp(): void
  25. {
  26. parent::setUp(); // TODO: Change the autogenerated stub
  27. $config = new Config(MYSQL_CONFIG);
  28. $config->setReturnCollection(true);
  29. $this->connection = new Connection($config);
  30. DbManager::getInstance()->addConnection($this->connection);
  31. $connection = DbManager::getInstance()->getConnection();
  32. $this->assertTrue($connection === $this->connection);
  33. }
  34. /**
  35. * @throws \EasySwoole\ORM\Exception\Exception
  36. * @throws \Throwable
  37. */
  38. public function testAdd()
  39. {
  40. $testUserModel = new TestUserListModel();
  41. $testUserModel->state = 1;
  42. $testUserModel->name = '仙士可';
  43. $testUserModel->age = 100;
  44. $testUserModel->addTime = date('Y-m-d H:i:s');
  45. $data = $testUserModel->save();
  46. $this->assertIsInt($data);
  47. $testUserModel = new TestUserListModel();
  48. $testUserModel->state = 2;
  49. $testUserModel->name = 'Siam';
  50. $testUserModel->age = 18;
  51. $testUserModel->addTime = date('Y-m-d H:i:s');
  52. $data = $testUserModel->save();
  53. $this->assertIsInt($data);
  54. }
  55. public function testAllReturn()
  56. {
  57. $all = TestUserListModel::create()->all();
  58. $this->assertInstanceOf(Collection::class, $all);
  59. return $all;
  60. }
  61. /**
  62. * @depends testAllReturn
  63. * @param $all
  64. */
  65. public function testToArray(Collection $all)
  66. {
  67. $array = $all->toArray();
  68. $this->assertIsArray($array);
  69. $this->assertEquals(2, count($array));
  70. }
  71. /**
  72. * @depends testAllReturn
  73. * @param $all
  74. * @return Collection
  75. */
  76. public function testVisibleToArray(Collection $all)
  77. {
  78. $return = clone $all;
  79. $all->visible(['name']);
  80. $array = $all->toArray();
  81. $this->assertIsArray($array);
  82. $this->assertEquals(2, count($array));
  83. $this->assertEquals(1, count($array[0]));
  84. return $return;
  85. }
  86. public function testHiddenToArray()
  87. {
  88. $all = TestUserListModel::create()->all();
  89. $all->hidden(['name']);
  90. $array = $all->toArray();
  91. $this->assertIsArray($array);
  92. $this->assertEquals(2, count($array));
  93. $this->assertEquals(4, count($array[0]));
  94. $this->assertArrayNotHasKey('name', $array[0]);
  95. }
  96. public function testAppendToArray()
  97. {
  98. $all = TestUserListModel::create()->all();
  99. $all->hidden(['name']);
  100. $all->append(['append_one']);
  101. $array = $all->toArray(false, false);
  102. $this->assertIsArray($array);
  103. $this->assertEquals(2, count($array));
  104. $this->assertEquals(5, count($array[0]));
  105. $this->assertArrayNotHasKey('name', $array[0]);
  106. }
  107. public function testReverse()
  108. {
  109. $all = TestUserListModel::create()->all();
  110. foreach ($all as $model) {
  111. $this->assertInstanceOf(AbstractModel::class, $model);
  112. }
  113. $this->assertEquals("仙士可", $all[0]['name']);
  114. $all = $all->reverse();// 倒置
  115. $this->assertEquals("Siam", $all[0]['name']);
  116. }
  117. public function testDestory()
  118. {
  119. $res = TestUserListModel::create()->destroy(null, true);
  120. $this->assertIsInt($res);
  121. }
  122. public function testToRawArray()
  123. {
  124. $testUserModel = new TestUserListGetterModel();
  125. $testUserModel->state = 1;
  126. $testUserModel->name = 'gaobinzhan';
  127. $testUserModel->age = 20;
  128. $testUserModel->addTime = date('Y-m-d H:i:s');
  129. $ids[] = $testUserModel->save();
  130. $testUserModel = new TestUserListGetterModel();
  131. $testUserModel->state = 1;
  132. $testUserModel->name = 'gaobinzhan';
  133. $testUserModel->age = 20;
  134. $testUserModel->addTime = date('Y-m-d H:i:s');
  135. $ids[] = $testUserModel->save();
  136. $ret = TestUserListGetterModel::create()->where('id', $ids, 'IN')->all();
  137. $this->assertEquals(2, count($ret));
  138. $this->assertEquals('123', $ret->toArray()[0]['addTime']);
  139. $this->assertEquals('2019-11-02 23:48:44', $ret->toRawArray()[0]['addTime']);
  140. TestUserListGetterModel::create()->destroy([], true);
  141. }
  142. }