ModelTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use Limen\Redisun\Examples\HashModel;
  4. use Limen\Redisun\Examples\ListModel;
  5. use Limen\Redisun\Examples\StringModel;
  6. use Limen\Redisun\Examples\ZsetModel;
  7. use Limen\Redisun\Examples\SetModel;
  8. /**
  9. * Class ModelTest
  10. *
  11. * @author LI Mengxiang <limengxiang876@gmail.com>
  12. */
  13. class ModelTest extends TestCase
  14. {
  15. public function testQueryKeys()
  16. {
  17. try {
  18. $model = new StringModel();
  19. $range = range(1,20);
  20. $keys = [];
  21. foreach ($range as $i) {
  22. $model->insert([
  23. 'id' => $i,
  24. 'name' => 'martin',
  25. ],'22');
  26. $keys[] = "redisun:$i:string:martin";
  27. }
  28. $value = $model->newQuery()->where('id', 1)->getKeys();
  29. $this->assertEquals([
  30. "redisun:1:string:martin",
  31. ], $value);
  32. $value = $model->newQuery()->whereIn('id', [1,2])->getKeys();
  33. $this->assertEquals([
  34. "redisun:1:string:martin",
  35. "redisun:2:string:martin",
  36. ], $value);
  37. $value = $model->newQuery()
  38. ->whereIn('id', [1,2,3,4,5,6])
  39. ->orderBy('id')
  40. ->take(5)
  41. ->getKeys();
  42. $this->assertEquals([
  43. "redisun:1:string:martin",
  44. "redisun:2:string:martin",
  45. "redisun:3:string:martin",
  46. "redisun:4:string:martin",
  47. "redisun:5:string:martin",
  48. ], $value);
  49. $model->newQuery()->whereIn('id', $range)->delete();
  50. $this->assertEquals([], $model->all());
  51. } catch (Exception $e) {
  52. throw $e;
  53. } finally {
  54. $model->newQuery()->whereIn('id', $range)->delete();
  55. }
  56. }
  57. public function testHashModel()
  58. {
  59. $a = [
  60. 'name' => 'martin',
  61. 'age' => '22',
  62. 'height' => '175',
  63. 'nation' => 'China',
  64. ];
  65. $b = [
  66. 'name' => 'nathan',
  67. 'age' => '23',
  68. 'height' => '176',
  69. 'nation' => 'China',
  70. ];
  71. $model = new HashModel();
  72. $model->create(1, $a);
  73. $model->create(2, $b);
  74. $value = $model->newQuery()->where('id', 1)->first();
  75. $this->assertEquals($a, $value);
  76. $this->assertEquals($model->find(1), $a);
  77. $values = $model->findBatch([1,2]);
  78. $this->assertEquals(2, count($values));
  79. $this->assertEquals($a, $values[1]);
  80. $this->assertEquals($b, $values[2]);
  81. $values = $model->all();
  82. $this->assertTrue(in_array($a, $values));
  83. $this->assertTrue(in_array($b, $values));
  84. $data = $model->newQuery()->whereIn('id', [1,2])->orderBy('id', 'desc')->get();
  85. $this->assertEquals([$b, $a], array_values($data));
  86. $data = $model->newQuery()->whereIn('id', [1,2])->orderBy('id', 'desc')->take(1)->get();
  87. $this->assertEquals(1, count($data));
  88. $this->assertEquals($b, $data['redisun:2:hash']);
  89. $updated = [];
  90. $updated['age'] = '24';
  91. $model->newQuery()->where('id', 1)->update($updated);
  92. $value = $model->newQuery()->where('id', 1)->first();
  93. $this->assertEquals($updated, $value);
  94. $model->destroy(1);
  95. $this->assertEquals($model->find(1), []);
  96. $model->updateBatch([1,2], $a);
  97. $this->assertEquals($model->find(1), $a);
  98. $this->assertEquals($model->find(2), $a);
  99. $model->destroyBatch([1,2]);
  100. $this->assertEquals($model->find(2), []);
  101. $model->newQuery()->whereIn('id', [1,2])->delete();
  102. $this->assertEquals($model->all(), []);
  103. }
  104. public function testListModel()
  105. {
  106. $list = [1,2,3];
  107. $model = new ListModel();
  108. $model->create(1, [1,2,3]);
  109. $this->assertEquals($model->find(1), $list);
  110. $list[] = 4;
  111. $model->where('id', 1)->update($list);
  112. $this->assertEquals($model->find(1), $list);
  113. $model->where('id', 1)->delete();
  114. $this->assertEquals($model->find(1), []);
  115. $this->assertEquals($model->all(), []);
  116. }
  117. public function testStringModel()
  118. {
  119. $value = 'martin-walk';
  120. $model = new StringModel();
  121. $model->insert([
  122. 'id' => 1,
  123. 'name' => 'martin'
  124. ], $value);
  125. $this->assertEquals($value, $model->where('id', 1)->where('name', 'martin')->first());
  126. $this->assertEquals($value, $model->where('id', 1)->first());
  127. $this->assertEquals($value, $model->where('name', 'martin')->first());
  128. $value = ucfirst($value);
  129. $model->where('id', 1)->update($value);
  130. $this->assertEquals($value, $model->where('id', 1)->first(1));
  131. $model->where('id', 1)->delete();
  132. $this->assertEquals($model->where('id', 1)->first(), null);
  133. $this->assertEquals($model->all(), []);
  134. }
  135. public function testZsetModel()
  136. {
  137. $zset = [
  138. 'google' => 10000,
  139. 'amazon' => 8000,
  140. 'apple' => 20000,
  141. 'alibaba' => 2000,
  142. ];
  143. $model = new ZsetModel();
  144. $model->create(1, $zset);
  145. asort($zset);
  146. $this->assertEquals($model->find(1), array_keys($zset));
  147. unset($zset['alibaba']);
  148. $model->where('id', 1)->update($zset);
  149. $this->assertEquals($model->find(1), array_keys($zset));
  150. $model->destroy(1);
  151. $this->assertEquals($model->find(1), []);
  152. $this->assertEquals($model->all(), []);
  153. }
  154. public function testSetModel()
  155. {
  156. $set = [
  157. 'alibaba',
  158. 'google',
  159. 'amazon',
  160. 'apple',
  161. ];
  162. sort($set);
  163. $model = new SetModel();
  164. $model->create(1, $set);
  165. $value = $model->find(1);
  166. sort($value);
  167. $this->assertEquals($value, $set);
  168. array_pop($set);
  169. $model->where('id', 1)->update($set);
  170. $value = $model->find(1);
  171. sort($value);
  172. $this->assertEquals($value, $set);
  173. $model->destroy(1);
  174. $this->assertEquals($model->find(1), []);
  175. $this->assertEquals($model->all(), []);
  176. }
  177. public function testAggregation()
  178. {
  179. $model = new StringModel();
  180. $model->insert([
  181. 'id' => 1,
  182. 'name' => 'martin',
  183. ],10);
  184. $model->insert([
  185. 'id' => 2,
  186. 'name' => 'martin',
  187. ],20);
  188. $model->insert([
  189. 'id' => 3,
  190. 'name' => 'martin',
  191. ],30);
  192. $this->assertEquals(60, $model->newQuery()->sum());
  193. $this->assertEquals(10, $model->newQuery()->min());
  194. $this->assertEquals(30, $model->newQuery()->max());
  195. $this->assertEquals(3, $model->newQuery()->count());
  196. $this->assertEquals(1, $model->newQuery()->where('id',1)->count());
  197. $this->assertEquals(3, $model->newQuery()->where('name', 'martin')->count());
  198. $this->assertEquals(0, $model->newQuery()->where('name', 'maria')->count());
  199. $model->newQuery()->whereIn('id', [1,2,3])->where('name', 'martin')->delete();
  200. $this->assertEquals($model->all(), []);
  201. }
  202. public function testSort()
  203. {
  204. $model = new StringModel();
  205. $array = [
  206. '10','20','30',
  207. ];
  208. $model->insert([
  209. 'id' => 1,
  210. 'name' => 'maria',
  211. ],$array[0]);
  212. $model->insert([
  213. 'id' => 2,
  214. 'name' => 'maria',
  215. ],$array[1]);
  216. $model->insert([
  217. 'id' => 3,
  218. 'name' => 'maria',
  219. ],$array[2]);
  220. $this->assertEquals($array, $model->newQuery()->sort('asc'));
  221. $this->assertEquals(array_reverse($array), $model->newQuery()->sort('desc'));
  222. $model->newQuery()->whereIn('id', [1,2,3])->where('name', 'maria')->delete();
  223. $this->assertEquals($model->all(), []);
  224. }
  225. public function testTtl()
  226. {
  227. $ttl = 2;
  228. // StringModel
  229. $model = new StringModel();
  230. $model->insert([
  231. 'id' => 1,
  232. 'name' => 'maria',
  233. ], 'maria', $ttl);
  234. $this->assertEquals($ttl, $model->newQuery()->where('id',1)->where('name','maria')->ttl());
  235. $model->newQuery()->where('id',1)->where('name','maria')->update('mary');
  236. $this->assertGreaterThanOrEqual(0, $model->newQuery()->where('id',1)->where('name','maria')->ttl());
  237. $this->assertLessThanOrEqual($ttl, $model->newQuery()->where('id',1)->where('name','maria')->ttl());
  238. sleep($ttl + 1);
  239. $this->assertEquals([], $model->newQuery()->where('id',1)->where('name','maria')->get());
  240. $this->assertNull($model->newQuery()->where('id',1)->where('name','maria')->first());
  241. // HashModel
  242. $model = new HashModel();
  243. $model->create(1, [
  244. 'name' => 'maria',
  245. 'age' => 25,
  246. ], $ttl);
  247. $model->create(2, [
  248. 'name' => 'maria',
  249. 'age' => 25,
  250. ], $ttl + 1);
  251. $this->assertEquals($ttl, $model->newQuery()->where('id',1)->ttl());
  252. $model->where('id', 1)->update([
  253. 'age' => 26,
  254. ]);
  255. $this->assertEquals($ttl, $model->newQuery()->where('id',1)->ttl());
  256. $model->updateBatch([1,2], [
  257. 'age' => 27
  258. ]);
  259. $this->assertEquals($ttl, $model->newQuery()->where('id',1)->ttl());
  260. $this->assertEquals($ttl + 1, $model->newQuery()->where('id',2)->ttl());
  261. sleep($ttl + 1);
  262. $this->assertEquals([], $model->newQuery()->where('id',1)->get());
  263. // SetModel
  264. $model = new SetModel();
  265. $model->create(1, [
  266. 'martin',
  267. 'maria'
  268. ], $ttl);
  269. $this->assertEquals($ttl, $model->newQuery()->where('id',1)->ttl());
  270. $model->where('id', 1)->update([
  271. 'martin',
  272. 'maria',
  273. 'cathrine',
  274. ]);
  275. $this->assertGreaterThanOrEqual(0, $model->newQuery()->where('id',1)->ttl());
  276. $this->assertLessThanOrEqual($ttl, $model->newQuery()->where('id',1)->ttl());
  277. sleep($ttl + 1);
  278. $this->assertEquals([], $model->newQuery()->where('id',1)->get());
  279. $this->assertEquals([], $model->find(1));
  280. // ZsetModel
  281. $model = new ZsetModel();
  282. $model->create(1, [
  283. 'martin' => 1,
  284. 'maria' => 2,
  285. ], $ttl);
  286. $this->assertequals($ttl, $model->newQuery()->where('id',1)->ttl());
  287. $model->where('id', 1)->update([
  288. 'martin' => 2,
  289. 'maria' => 3,
  290. 'cathrine' => 1,
  291. ]);
  292. $this->assertGreaterThanOrEqual(0, $model->newQuery()->where('id',1)->ttl());
  293. $this->assertLessThanOrEqual($ttl, $model->newQuery()->where('id',1)->ttl());
  294. sleep($ttl + 1);
  295. $this->assertEquals([], $model->newQuery()->where('id',1)->get());
  296. // ListModel
  297. $model = new ListModel();
  298. $model->create(1, [
  299. 'martin',
  300. 'maria',
  301. ], $ttl);
  302. $this->assertequals($ttl, $model->newQuery()->where('id',1)->ttl());
  303. $model->where('id', 1)->update([
  304. 'martin',
  305. 'maria',
  306. 'cathrine',
  307. ]);
  308. $this->assertGreaterThanOrEqual(0, $model->newQuery()->where('id',1)->ttl());
  309. $this->assertLessThanOrEqual($ttl, $model->newQuery()->where('id',1)->ttl());
  310. sleep($ttl + 1);
  311. $this->assertEquals([], $model->newQuery()->where('id',1)->get());
  312. $model->create(1, [
  313. 'martin',
  314. 'maria',
  315. ], $ttl);
  316. $ttl++;
  317. $model->newQuery()->where('id', 1)->update(['maria', 'martin'], $ttl);
  318. $this->assertequals($ttl, $model->newQuery()->where('id',1)->ttl());
  319. $model->newQuery()->where('id',1)->delete();
  320. $this->assertEquals([], $model->all());
  321. }
  322. public function testNativeMethods()
  323. {
  324. $numbers = [1,2,3,4,5,6,100,200,300];
  325. $listModel = new ListModel();
  326. $listModel->newQuery()->where('id', 1)->rpush($numbers);
  327. $this->assertEquals($listModel->newQuery()->where('id', 1)->first(), $numbers);
  328. $listModel->newQuery()->where('id', 2)->lpush($numbers);
  329. $this->assertEquals($listModel->newQuery()->where('id', 2)->first(), array_reverse($numbers));
  330. // clean up
  331. $listModel->newQuery()->whereIn('id', [1,2])->delete();
  332. $this->assertEquals($listModel->newQuery()->whereIn('id', [1,2])->count(), 0);
  333. $set = ['alibaba', 'google', 'amazon', 'apple',];
  334. $model = new SetModel();
  335. $model->newQuery()->where('id', 1)->sadd($set);
  336. $value = $model->find(1);
  337. $this->assertTrue($this->compareSet($value, $set));
  338. $model->newQuery()->where('id', 1)->srem($set);
  339. $this->assertFalse((bool)$model->find(1));
  340. }
  341. protected function compareSet($a, $b)
  342. {
  343. if (count($a) !== count($b)) {
  344. return false;
  345. }
  346. foreach ($a as $v) {
  347. if (!in_array($v, $b)) {
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. }