RelationToArrayTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * 关联查询、toArray
  4. * User: Siam
  5. * Date: 2019/11/15
  6. * Time: 17:32
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\ORM\Db\Config;
  10. use EasySwoole\ORM\Db\Connection;
  11. use EasySwoole\ORM\DbManager;
  12. use PHPUnit\Framework\TestCase;
  13. use EasySwoole\ORM\Tests\models\TestUserListModel;
  14. use EasySwoole\ORM\Tests\models\TestRelationModel;
  15. class RelationToArrayTest extends TestCase
  16. {
  17. /**
  18. * @var $connection Connection
  19. */
  20. protected $connection;
  21. protected $tableName = 'user_test_list';
  22. protected function setUp(): void
  23. {
  24. parent::setUp(); // TODO: Change the autogenerated stub
  25. $config = new Config(MYSQL_CONFIG);
  26. $this->connection = new Connection($config);
  27. DbManager::getInstance()->addConnection($this->connection);
  28. $connection = DbManager::getInstance()->getConnection();
  29. $this->assertTrue($connection === $this->connection);
  30. }
  31. public function testAdd()
  32. {
  33. $test_user_model = TestRelationModel::create();
  34. $test_user_model->name = 'siam_relation';
  35. $test_user_model->age = 21;
  36. $test_user_model->addTime = "2019-11-15 17:36:34";
  37. $test_user_model->state = 2;
  38. $test_user_model->save();
  39. $user_list = TestUserListModel::create();
  40. $user_list->name = 'siam_relation';
  41. $user_list->age = 21;
  42. $user_list->addTime = "2019-11-15 17:37:20";
  43. $user_list->state = 1;
  44. $user_list->save();
  45. }
  46. public function testGet()
  47. {
  48. $test_user_model = TestRelationModel::create()->get([
  49. 'name' => 'siam_relation'
  50. ]);
  51. $relation = $test_user_model->user_list();
  52. $this->assertInstanceOf(TestUserListModel::class, $relation);
  53. $toArray = $test_user_model->toArray(false, false);
  54. $this->assertNotEmpty($toArray['user_list']);
  55. $this->assertIsArray($toArray['user_list']);
  56. }
  57. /**
  58. * 在invoke中触发关联
  59. * @throws \Throwable
  60. */
  61. public function testGetInvokeRelation()
  62. {
  63. DbManager::getInstance()->invoke(function ($client) {
  64. $testUserModel = TestRelationModel::invoke($client)->with('user_list')->get();
  65. $this->assertNotEmpty($testUserModel['user_list']);
  66. $this->assertInstanceOf(TestUserListModel::class, $testUserModel['user_list']);
  67. });
  68. }
  69. /**
  70. * field筛选 toArray
  71. * @throws \EasySwoole\Mysqli\Exception\Exception
  72. * @throws \EasySwoole\ORM\Exception\Exception
  73. * @throws \Throwable
  74. */
  75. public function testFieldFilterToArray()
  76. {
  77. $test_user_model = TestRelationModel::create()->get([
  78. 'name' => 'siam_relation'
  79. ]);
  80. $relation = $test_user_model->user_list();
  81. $this->assertInstanceOf(TestUserListModel::class, $relation);
  82. $toArray = $test_user_model->field(['user_list'])->toArray(false, false);
  83. $this->assertEquals(1, count($toArray));
  84. $this->assertNotEmpty($toArray['user_list']);
  85. $this->assertIsArray($toArray['user_list']);
  86. }
  87. /**
  88. * hidden隐藏
  89. * @throws
  90. */
  91. public function testHiddenFilterToArray()
  92. {
  93. $test_user_model = TestRelationModel::create()->get([
  94. 'name' => 'siam_relation'
  95. ]);
  96. $test_user_model->user_list();
  97. $toArray = $test_user_model->hidden('user_list')->toArray(false, false);
  98. $this->assertEquals(5, count($toArray));
  99. $this->assertArrayNotHasKey("user_list", $toArray);
  100. }
  101. /**
  102. * append 追加非模型属性字段(自定义获取器)
  103. * @throws
  104. */
  105. public function testAppendFilterToArray()
  106. {
  107. $test_user_model = TestRelationModel::create()->get([
  108. 'name' => 'siam_relation'
  109. ]);
  110. $test_user_model->user_list();
  111. $toArray = $test_user_model->append(['append_one'])->toArray(false, false);
  112. $this->assertEquals(7, count($toArray));
  113. $this->assertArrayHasKey("append_one", $toArray);
  114. }
  115. /**
  116. * visible 筛选显示
  117. * @throws
  118. */
  119. public function testVisibleFilterToArray()
  120. {
  121. $test_user_model = TestRelationModel::create()->get([
  122. 'name' => 'siam_relation'
  123. ]);
  124. $test_user_model->user_list();
  125. $toArray = $test_user_model->visible(['name'])->toArray(false, false);
  126. $this->assertEquals(1, count($toArray));
  127. }
  128. public function testJson()
  129. {
  130. $test_user_model = TestRelationModel::create()->get([
  131. 'name' => 'siam_relation'
  132. ]);
  133. $relation = $test_user_model->user_list();
  134. // echo json_encode($test_user_model);
  135. $this->assertIsString(json_encode($test_user_model));
  136. }
  137. /**
  138. * 测试 指定字段 json
  139. * @throws
  140. */
  141. public function testVisibleJson()
  142. {
  143. $test_user_model = TestRelationModel::create()->get([
  144. 'name' => 'siam_relation'
  145. ])->visible(['name']);
  146. $relation = $test_user_model->user_list();
  147. $array = json_decode(json_encode($test_user_model), true);
  148. $this->assertEquals(1, count($array));
  149. $this->assertIsString(json_encode($test_user_model));
  150. }
  151. public function testDeleteAll()
  152. {
  153. $res = TestRelationModel::create()->destroy(null, true);
  154. $this->assertIsInt($res);
  155. $res = TestUserListModel::create()->destroy(null, true);
  156. $this->assertIsInt($res);
  157. }
  158. public function testAddHasMany()
  159. {
  160. $test_user_model = TestRelationModel::create();
  161. $test_user_model->name = 'siam';
  162. $test_user_model->age = 20;
  163. $test_user_model->addTime = "2019-11-15 17:36:34";
  164. $test_user_model->state = 2;
  165. $test_user_model->save();
  166. $user_list = TestUserListModel::create();
  167. $user_list->name = 'siam';
  168. $user_list->age = 22;
  169. $user_list->addTime = "2019-11-15 17:37:20";
  170. $user_list->state = 1;
  171. $user_list->save();
  172. $user_list = TestUserListModel::create();
  173. $user_list->name = 'siam';
  174. $user_list->age = 21;
  175. $user_list->addTime = "2019-11-15 17:37:20";
  176. $user_list->state = 1;
  177. $user_list->save();
  178. }
  179. public function testHasMany()
  180. {
  181. $test_user_model = TestRelationModel::create()->get([
  182. 'name' => 'siam'
  183. ]);
  184. $hasMany = $test_user_model->has_many();
  185. $this->assertEquals(2, count($hasMany));
  186. $this->assertInstanceOf(TestUserListModel::class, $hasMany[1]);
  187. }
  188. public function testHasManyWhere()
  189. {
  190. $test_user_model = TestRelationModel::create()->get([
  191. 'name' => 'siam'
  192. ]);
  193. $hasMany = $test_user_model->has_many_where();
  194. $this->assertEquals(1, count($hasMany));
  195. $this->assertEquals(21, $hasMany[0]->age);
  196. $this->assertInstanceOf(TestUserListModel::class, $hasMany[0]);
  197. }
  198. public function testGetWith()
  199. {
  200. $test = TestRelationModel::create()->with(['user_list', 'has_many'])->get([
  201. 'name' => 'siam'
  202. ]);
  203. $this->assertNotEmpty($test['user_list']);
  204. $this->assertInstanceOf(TestUserListModel::class, $test['user_list']);
  205. $this->assertEquals(2, count($test['has_many']));
  206. $this->assertInstanceOf(TestUserListModel::class, $test['has_many'][1]);
  207. }
  208. public function testAllWith()
  209. {
  210. $test = TestRelationModel::create()->with(['user_list', 'has_many'])->all();
  211. $this->assertNotEmpty($test[0]['user_list']);
  212. $this->assertInstanceOf(TestUserListModel::class, $test[0]['user_list']);
  213. $this->assertEquals(2, count($test[0]['has_many']));
  214. $this->assertInstanceOf(TestUserListModel::class, $test[0]['has_many'][1]);
  215. }
  216. // 需要的field没有在fields中 自动补充
  217. public function testGetWithNoneField()
  218. {
  219. $test = TestRelationModel::create()->field(['id', 'age'])->with(['user_list', 'has_many'])->get([
  220. 'name' => 'siam'
  221. ]);
  222. $this->assertNotEmpty($test['user_list']);
  223. $this->assertNotEmpty($test['name']);
  224. $this->assertInstanceOf(TestUserListModel::class, $test['user_list']);
  225. $this->assertEquals(2, count($test['has_many']));
  226. $this->assertInstanceOf(TestUserListModel::class, $test['has_many'][1]);
  227. }
  228. public function testAllWithNOneField()
  229. {
  230. $test = TestRelationModel::create()->field(['id'])->with(['user_list', 'has_many'])->all();
  231. $this->assertNotEmpty($test[0]['user_list']);
  232. $this->assertNotEmpty($test[0]['name']);
  233. $this->assertInstanceOf(TestUserListModel::class, $test[0]['user_list']);
  234. $this->assertEquals(2, count($test[0]['has_many']));
  235. $this->assertInstanceOf(TestUserListModel::class, $test[0]['has_many'][1]);
  236. }
  237. public function testDeleteAllHasMany()
  238. {
  239. $res = TestRelationModel::create()->destroy(null, true);
  240. $this->assertIsInt($res);
  241. $res = TestUserListModel::create()->destroy(null, true);
  242. $this->assertIsInt($res);
  243. }
  244. }