GetsetTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Author: LI Mengxiang
  4. * Email: limengxiang876@gmail.com
  5. * Date: 2018/6/17
  6. */
  7. use PHPUnit\Framework\TestCase;
  8. use Limen\Redisun\Examples\StringModel;
  9. class GetsetTest extends TestCase
  10. {
  11. public function testZset()
  12. {
  13. try {
  14. $model = new \Limen\Redisun\Examples\ZsetModel();
  15. $model->create(1, ['google' => 18]);
  16. $oldValue = $model->where('id', 1)->getAndSet(['ms' => 19]);
  17. $this->assertEquals($oldValue, ['google']);
  18. $value = $model->where('id', 1)->first();
  19. $this->assertEquals($value, ['ms']);
  20. } catch (Exception $e) {
  21. throw $e;
  22. } finally {
  23. $model->destroy(1);
  24. }
  25. }
  26. public function testSet()
  27. {
  28. try {
  29. $model = new \Limen\Redisun\Examples\SetModel();
  30. $model->create(1, [1,2,3]);
  31. $oldValue = $model->where('id', 1)->getAndSet([1,2,3,4]);
  32. $this->assertEquals($oldValue, [1,2,3]);
  33. $value = $model->where('id', 1)->first();
  34. $this->assertEquals($value, [1,2,3,4]);
  35. } catch (Exception $e) {
  36. throw $e;
  37. } finally {
  38. $model->destroy(1);
  39. }
  40. }
  41. public function testList()
  42. {
  43. try {
  44. $model = new \Limen\Redisun\Examples\ListModel();
  45. $model->create(1, [1,2,3]);
  46. $oldValue = $model->where('id', 1)->getAndSet([1,2,3,4]);
  47. $this->assertEquals($oldValue, [1,2,3]);
  48. $value = $model->where('id', 1)->first();
  49. $this->assertEquals($value, [1,2,3,4]);
  50. } catch (Exception $e) {
  51. throw $e;
  52. } finally {
  53. $model->destroy(1);
  54. }
  55. }
  56. public function testHash()
  57. {
  58. try {
  59. $person = [
  60. 'name' => 'maria',
  61. 'age' => 22,
  62. ];
  63. $model = new \Limen\Redisun\Examples\HashModel();
  64. $model->create(1, $person);
  65. $oldValue = $model->where('id', 1)->getAndSet(['name' => 'maria']);
  66. $this->assertEquals($oldValue, $person);
  67. $value = $model->find(1);
  68. $this->assertEquals($value, ['name' => 'maria']);
  69. } catch (Exception $e) {
  70. throw $e;
  71. } finally {
  72. $model->destroy(1);
  73. }
  74. }
  75. public function testString()
  76. {
  77. try {
  78. $model = new StringModel();
  79. $model->insert(
  80. [
  81. 'id' => 1,
  82. 'name' => 'maria',
  83. ],
  84. 'mymaria',
  85. 120
  86. );
  87. $oldValue = $model->newQuery()->where('id', 1)
  88. ->where('name', 'maria')
  89. ->getAndSet('mymaria1', 130);
  90. $this->assertEquals($oldValue, 'mymaria');
  91. $ttl = $model->newQuery()->where('id', 1)
  92. ->where('name', 'maria')
  93. ->ttl();
  94. $this->assertEquals($ttl, 130);
  95. $value = $model->newQuery()->where('id', 1)
  96. ->where('name', 'maria')
  97. ->first();
  98. $this->assertEquals($value, 'mymaria1');
  99. } catch (Exception $e) {
  100. throw $e;
  101. } finally {
  102. $model->newQuery()->where('id', 1)
  103. ->where('name', 'maria')
  104. ->delete();
  105. }
  106. }
  107. }