Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

179 wiersze
4.2 KiB

  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiRedis_Test
  4. *
  5. * 针对 ../../PhalApi/Cache/Redis.php PhalApi_Redis 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20150516
  8. */
  9. require_once dirname(__FILE__) . '/../test_env.php';
  10. if (!class_exists('PhalApi_Cache_Redis')) {
  11. require dirname(__FILE__) . '/../../PhalApi/Cache/Redis.php';
  12. }
  13. class PhpUnderControl_PhalApiRedis_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $phalApiRedis;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $config = array('host' => '127.0.0.1', 'port' => 6379);
  20. $this->phalApiRedis = new PhalApi_Cache_Redis($config);
  21. }
  22. protected function tearDown()
  23. {
  24. }
  25. /**
  26. * @group testSet
  27. */
  28. public function testSet()
  29. {
  30. $key = 'testSetKey';
  31. $value = 'phalapi';
  32. $expire = 2;
  33. $rs = $this->phalApiRedis->set($key, $value, $expire);
  34. }
  35. /**
  36. * @group testGet
  37. */
  38. public function testGet()
  39. {
  40. $key = 'testSetKey';
  41. $rs = $this->phalApiRedis->get($key);
  42. $this->assertEquals('phalapi', $rs);
  43. sleep(3);
  44. $this->assertSame(NULL, $this->phalApiRedis->get($key));
  45. }
  46. /**
  47. * @group testDelete
  48. */
  49. public function testDelete()
  50. {
  51. $key = 'testDeleteKey';
  52. $this->phalApiRedis->set($key, 'net', 3);
  53. $this->assertNotEmpty($this->phalApiRedis->get($key));
  54. $rs = $this->phalApiRedis->delete($key);
  55. $this->assertEmpty($this->phalApiRedis->get($key));
  56. }
  57. /**
  58. * @group testSetnx
  59. */
  60. public function testSetnx()
  61. {
  62. $key = 'testSetnxKey';
  63. $value = array('name' => 'phalapi');
  64. $rs = $this->phalApiRedis->setnx($key, $value);
  65. $this->assertEquals(array('name' => 'phalapi'), $this->phalApiRedis->get($key));
  66. }
  67. /**
  68. * @group testLPush
  69. */
  70. public function testLPush()
  71. {
  72. $key = 'testLPushKey';
  73. $this->phalApiRedis->delete($key);
  74. $rs = $this->phalApiRedis->lPush($key, 1);
  75. $this->assertEquals(1, $rs);
  76. $rs = $this->phalApiRedis->lPush($key, 2);
  77. $this->assertEquals(2, $rs);
  78. $rs = $this->phalApiRedis->lPush($key, 3);
  79. $this->assertEquals(3, $rs);
  80. }
  81. /**
  82. * @group testRPush
  83. */
  84. public function testRPush()
  85. {
  86. $key = 'testRPushKey';
  87. $this->phalApiRedis->delete($key);
  88. $rs = $this->phalApiRedis->rPush($key, 1);
  89. $this->assertEquals(1, $rs);
  90. $rs = $this->phalApiRedis->rPush($key, 'haha~');
  91. $this->assertEquals(2, $rs);
  92. $rs = $this->phalApiRedis->rPush($key, true);
  93. $this->assertEquals(3, $rs);
  94. $rs = $this->phalApiRedis->rPush($key, array('name' => 'phalapi'));
  95. $this->assertEquals(4, $rs);
  96. }
  97. /**
  98. * @group testLPop
  99. */
  100. public function testLPop()
  101. {
  102. $key = 'testLPushKey';
  103. $rs = $this->phalApiRedis->lPop($key);
  104. $this->assertEquals(3, $rs);
  105. $rs = $this->phalApiRedis->lPop($key);
  106. $this->assertEquals(2, $rs);
  107. $rs = $this->phalApiRedis->lPop($key);
  108. $this->assertEquals(1, $rs);
  109. $rs = $this->phalApiRedis->lPop($key);
  110. $this->assertEquals(NULL, $rs);
  111. }
  112. /**
  113. * @group testRPop
  114. */
  115. public function testRPop()
  116. {
  117. $key = 'testRPushKey';
  118. $rs = $this->phalApiRedis->rPop($key);
  119. $this->assertSame(array('name' => 'phalapi'), $rs);
  120. $rs = $this->phalApiRedis->rPop($key);
  121. $this->assertSame(true, $rs);
  122. $rs = $this->phalApiRedis->rPop($key);
  123. $this->assertSame('haha~', $rs);
  124. $rs = $this->phalApiRedis->rPop($key);
  125. $this->assertSame(1, $rs);
  126. $rs = $this->phalApiRedis->rPop($key);
  127. $this->assertSame(NULL, $rs);
  128. }
  129. public function testPushAndPop()
  130. {
  131. $key = 'testPushAndPopKey';
  132. $this->phalApiRedis->delete($key);
  133. $this->phalApiRedis->rPush($key, 'www');
  134. $this->phalApiRedis->rPush($key, 'phalapi');
  135. $this->phalApiRedis->rPush($key, 'net');
  136. $this->assertEquals('www', $this->phalApiRedis->lPop($key));
  137. $this->assertEquals('phalapi', $this->phalApiRedis->lPop($key));
  138. $this->assertEquals('net', $this->phalApiRedis->lPop($key));
  139. }
  140. }