CoherentTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * 连贯操作
  4. * User: Siam
  5. * Date: 2019/10/23 0023
  6. * Time: 22:00
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\Mysqli\Exception\Exception;
  10. use EasySwoole\Mysqli\QueryBuilder;
  11. use EasySwoole\ORM\AbstractModel;
  12. use EasySwoole\ORM\Db\Config;
  13. use EasySwoole\ORM\Db\Connection;
  14. use EasySwoole\ORM\DbManager;
  15. use PHPUnit\Framework\TestCase;
  16. use EasySwoole\ORM\Tests\models\TestUserListModel;
  17. class CoherentTest 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. $this->connection = new Connection($config);
  29. DbManager::getInstance()->addConnection($this->connection);
  30. $connection = DbManager::getInstance()->getConnection();
  31. $this->assertTrue($connection === $this->connection);
  32. }
  33. public function testAdd()
  34. {
  35. $testUserModel = new TestUserListModel();
  36. $testUserModel->state = 1;
  37. $testUserModel->name = '仙士可';
  38. $testUserModel->age = 100;
  39. $testUserModel->addTime = date('Y-m-d H:i:s');
  40. $data = $testUserModel->save();
  41. $this->assertIsInt($data);
  42. $testUserModel = new TestUserListModel();
  43. $testUserModel->state = 2;
  44. $testUserModel->name = 'Siam';
  45. $testUserModel->age = 18;
  46. $testUserModel->addTime = date('Y-m-d H:i:s');
  47. $data = $testUserModel->save();
  48. $this->assertIsInt($data);
  49. $testUserModel = new TestUserListModel();
  50. $testUserModel->state = 2;
  51. $testUserModel->name = 'Siam';
  52. $testUserModel->age = 19;
  53. $testUserModel->addTime = date('Y-m-d H:i:s');
  54. $data = $testUserModel->save();
  55. $this->assertIsInt($data);
  56. }
  57. public function testWhere()
  58. {
  59. $testUserModel = TestUserListModel::create();
  60. $get = $testUserModel->get([
  61. 'state' => 1
  62. ]);
  63. $model = TestUserListModel::create();
  64. $getCoherent = $model->where(['state' => 1])->get();
  65. // model里的where解析
  66. $getCoherent2Model = TestUserListModel::create();
  67. $getCoherent2 = $getCoherent2Model->where(['state' => 2])->get();
  68. $this->assertEquals($get->age, $getCoherent->age);
  69. $this->assertNotEquals($get->age, $getCoherent2->age);
  70. $getCoherent3 = TestUserListModel::create()->where($getCoherent2->id)->get();
  71. $this->assertEquals($getCoherent3->age, $getCoherent3->age);
  72. $getCoherent4 = TestUserListModel::create()->where([$getCoherent2->id, $getCoherent->id])->all();
  73. $this->assertEquals(count($getCoherent4), 2);
  74. // 走builder原生的where
  75. $getCoherent5 = TestUserListModel::create()->where('id', $getCoherent3->id, '=')->get();
  76. $this->assertEquals($getCoherent5->id, $getCoherent3->id);
  77. $getCoherent6 = TestUserListModel::create()->where('id', $getCoherent3->id, '!=')->get();
  78. $this->assertNotEquals($getCoherent6->id, $getCoherent3->id);
  79. // where null
  80. /** @var AbstractModel $model7 */
  81. $model7 = TestUserListModel::create();
  82. $test7 = $model7->where('name', null, 'is')->get();
  83. $this->assertEquals("SELECT * FROM `user_test_list` WHERE `name` is NULL LIMIT 1", $model7->lastQuery()->getLastQuery());
  84. $test7 = $model7->where('name', null, 'is not')->get();
  85. $this->assertEquals("SELECT * FROM `user_test_list` WHERE `name` is not NULL LIMIT 1", $model7->lastQuery()->getLastQuery());
  86. }
  87. public function testGroupAndAll()
  88. {
  89. $group = TestUserListModel::create()->field('sum(age) as age, `name`')->group('name')->all(null);
  90. foreach ($group as $one) {
  91. if ($one->name == 'Siam') {
  92. $this->assertEquals($one->age, 18 + 19);
  93. } else {
  94. $this->assertEquals($one->age, 100);
  95. }
  96. }
  97. }
  98. public function testOrder()
  99. {
  100. $order = TestUserListModel::create()->order('id', 'DESC')->get();
  101. $this->assertEquals($order->age, 19);
  102. }
  103. public function testJoinData()
  104. {
  105. $res = TestUserListModel::create()->field('sum(age) as siam, `name`')->group('name')->all();
  106. $this->assertNotEmpty($res[0]->siam);
  107. $this->assertNotEmpty($res[0]['siam']);
  108. }
  109. public function testFind()
  110. {
  111. $groupDivField = TestUserListModel::create()->field('sum(age), `name`')->group('name')->all();
  112. $this->assertNotEmpty($groupDivField[0]['sum(age)']);
  113. $groupDivField = TestUserListModel::create()->field('sum(age), `name`')->group('name')->get();
  114. $this->assertNotEmpty($groupDivField['sum(age)']);
  115. }
  116. /** 别名时 判断isset */
  117. public function testFieldAlias()
  118. {
  119. $fieldAlias = TestUserListModel::create()->field(['name as teeeee'])->get();
  120. $this->assertTrue(isset($fieldAlias['teeeee']), true);
  121. }
  122. public function testColumn()
  123. {
  124. $res = TestUserListModel::create()->field('`name`, `age`')->order('age')->column();
  125. $this->assertIsArray($res);
  126. $this->assertTrue(count($res) > 0);
  127. $this->assertTrue($res[0] === '仙士可');
  128. $res = TestUserListModel::create()->field('`name`')->order('age')->column();
  129. $this->assertIsArray($res);
  130. $this->assertTrue(count($res) > 0);
  131. $this->assertTrue($res[0] === '仙士可');
  132. $res = TestUserListModel::create()->order('age')->column('name');
  133. $this->assertIsArray($res);
  134. $this->assertTrue(count($res) > 0);
  135. $this->assertTrue($res[0] === '仙士可');
  136. $res = TestUserListModel::create()->field('`name`')->where(['name' => mt_rand()])->order('age')->column('name');
  137. $this->assertTrue(is_null($res));
  138. $res = TestUserListModel::create()->field('`name`')->order('age')->column('age');
  139. $this->assertEquals(100, $res[0]);
  140. $this->assertEquals(19, $res[1]);
  141. $this->assertEquals(18, $res[2]);
  142. }
  143. public function testScalar()
  144. {
  145. $res = TestUserListModel::create()->field('`name`, `age`')->order('age')->scalar();
  146. $this->assertTrue($res === '仙士可');
  147. $res = TestUserListModel::create()->field('`name`')->order('age')->scalar();
  148. $this->assertTrue($res === '仙士可');
  149. $res = TestUserListModel::create()->order('age')->scalar('name');
  150. $this->assertTrue($res === '仙士可');
  151. $res = TestUserListModel::create()->field('`name`')->where(['name' => mt_rand()])->order('age')->scalar('name');
  152. $this->assertTrue(is_null($res));
  153. $res = TestUserListModel::create()->field('`name`')->order('age')->scalar('age');
  154. $this->assertEquals(100, $res);
  155. }
  156. public function testIndexBy()
  157. {
  158. $res = TestUserListModel::create()->order('age')->indexBy('age');
  159. $this->assertTrue(isset($res['100']['name']));
  160. $this->assertTrue($res['100']['name'] === '仙士可');
  161. $this->assertTrue($res['18']['name'] === 'Siam');
  162. $res = TestUserListModel::create()->field('`name`')->where(['name' => mt_rand()])->order('age')->indexBy('name');
  163. $this->assertTrue(is_null($res));
  164. $res = TestUserListModel::create()->field('`name`')->order('age')->indexBy('age');
  165. $this->assertTrue(is_null($res));
  166. }
  167. public function testAlias()
  168. {
  169. $res = TestUserListModel::create()->alias('siam')->where(['siam.name' => '仙士可'])->all();
  170. $this->assertEquals($res[0]->name, '仙士可');
  171. }
  172. public function testMax()
  173. {
  174. $max = TestUserListModel::create()->max('age');
  175. $this->assertEquals($max, 100);
  176. }
  177. public function testMin()
  178. {
  179. $min = TestUserListModel::create()->min('age');
  180. $this->assertEquals($min, 18);
  181. }
  182. public function testCount()
  183. {
  184. $count = TestUserListModel::create()->count();
  185. $this->assertEquals($count, 3);
  186. }
  187. public function testCountZero()
  188. {
  189. $count = TestUserListModel::create()->where('name', 'undefined')->count();
  190. $this->assertEquals($count, 0);
  191. }
  192. public function testAvg()
  193. {
  194. $avg = TestUserListModel::create()->avg('age');
  195. $this->assertEquals($avg, 45.6667);
  196. }
  197. public function testSum()
  198. {
  199. $sum = TestUserListModel::create()->sum('age');
  200. $this->assertEquals($sum, 100 + 18 + 19);
  201. }
  202. public function testWhereUpdate()
  203. {
  204. $res = TestUserListModel::create()->where(['age' => 18])->update([
  205. 'name' => 'Siam18'
  206. ]);
  207. $user = TestUserListModel::create()->where(['age' => 18])->get();
  208. $this->assertEquals($user->name, 'Siam18');
  209. $res = TestUserListModel::create()->where(['age' => 18])->update([
  210. 'name' => 'Siam'
  211. ]);
  212. }
  213. public function testAllUpdate()
  214. {
  215. $res = TestUserListModel::create()->update([
  216. 'name' => 'Siam'
  217. ], null, true);
  218. $this->assertEquals($res, true);
  219. }
  220. /**
  221. * 测试issue inc报错 测试结果正常
  222. * @throws Exception
  223. * @throws \EasySwoole\ORM\Exception\Exception
  224. * @throws \Throwable
  225. */
  226. public function testUpdateInc()
  227. {
  228. $model = TestUserListModel::create();
  229. $res = $model->update([
  230. 'age' => QueryBuilder::inc(2),
  231. ], [
  232. 'age' => 100
  233. ]);
  234. $this->assertEquals($res, true);
  235. $user = TestUserListModel::create()->get([
  236. 'age' => 102
  237. ]);
  238. $this->assertInstanceOf(TestUserListModel::class, $user);
  239. $user->age = QueryBuilder::inc(3);
  240. $res = $user->update();
  241. $this->assertEquals($res, true);
  242. $this->assertEquals(1, $user->lastQueryResult()->getAffectedRows());
  243. }
  244. public function testWhereDelete()
  245. {
  246. $res = TestUserListModel::create()->where([
  247. 'name' => 'Siam'
  248. ])->destroy();
  249. $this->assertEquals($res, true);
  250. }
  251. public function testTempTableName()
  252. {
  253. $model = TestUserListModel::create();
  254. $res = $model->tableName('test_user_model', true)->get();
  255. $this->assertEquals($model->lastQuery()->getLastQuery(), "SELECT * FROM `test_user_model` LIMIT 1");
  256. $res2 = $model->get();
  257. $this->assertEquals($model->lastQuery()->getLastQuery(), "SELECT * FROM `user_test_list` LIMIT 1");
  258. }
  259. public function testDeleteAll()
  260. {
  261. $res = TestUserListModel::create()->destroy(null, true);
  262. $this->assertIsInt($res);
  263. }
  264. }