You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.7 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-25
  8. *
  9. * Unit tests for MemcacheCache class
  10. */
  11. require_once dirname(__FILE__) . '/../../cache/Cache.php';
  12. require_once dirname(__FILE__) . '/../../cache/MemcacheCache.php';
  13. class MemcacheCacheTest extends PHPUnit_Framework_TestCase
  14. {
  15. protected function setUp()
  16. {
  17. $this->getMock('Memcache');
  18. set_new_overload(array($this, 'newCallback'));
  19. }
  20. public function testConstruct()
  21. {
  22. $config = array('key_salt' => 'some', 'host' => array('hostname' => 'localhost', 'port' => 8080));
  23. $memcache = new MemcacheCache($config);
  24. $this->assertAttributeInstanceOf('TestCache', 'connection', $memcache);
  25. }
  26. /**
  27. * @expectedException Exception
  28. * @expectedExceptionMessage Configuration must have a
  29. * @TODO: MemcacheCache::__construct - empty config array passes with no host params
  30. */
  31. public function testWrongConfig()
  32. {
  33. $config = array('some' => array());
  34. $memcache = new MemcacheCache($config);
  35. }
  36. public function testAddGet()
  37. {
  38. $memcache = new MemcacheCache(array());
  39. $this->assertTrue($memcache->add('key', 'value'));
  40. $this->assertEquals('value', $memcache->get('key'));
  41. $this->assertFalse($memcache->add('key', 'newvalue'));
  42. }
  43. public function testIncrementDecrement()
  44. {
  45. $memcache = new MemcacheCache(array());
  46. $memcache->add('one', 1);
  47. $memcache->add('two', 2);
  48. $memcache->add('three', 'three');
  49. $this->assertTrue($memcache->increment('one'));
  50. $this->assertEquals(2, $memcache->get('one'));
  51. $this->assertTrue($memcache->decrement('two'));
  52. $this->assertEquals(1, $memcache->get('two'));
  53. $this->assertFalse($memcache->decrement('three'));
  54. }
  55. public function testDelete()
  56. {
  57. $memcache = new MemcacheCache(array());
  58. $memcache->add('one', 1);
  59. $memcache->add('two', 2);
  60. $memcache->del('one');
  61. $this->assertEquals(2, $memcache->get('two'));
  62. $this->assertFalse($memcache->get('one'));
  63. }
  64. public function testFlush()
  65. {
  66. $memcache = new MemcacheCache(array());
  67. $memcache->add('one', 1);
  68. $memcache->add('two', 2);
  69. $memcache->add('three', 'three');
  70. $this->assertEquals('three', 'three');
  71. $memcache->flush();
  72. $this->assertFalse($memcache->get('three'));
  73. $this->assertFalse($memcache->get('one'));
  74. }
  75. public function testSetReplace()
  76. {
  77. $memcache = new MemcacheCache(array());
  78. $memcache->add('one', 1);
  79. $memcache->add('two', 2);
  80. $memcache->add('three', 'three');
  81. $memcache->set('one', 30);
  82. $memcache->replace('three', 3);
  83. $this->assertEquals(30, $memcache->get('one'));
  84. $this->assertEquals(3, $memcache->get('three'));
  85. }
  86. protected function newCallback($className)
  87. {
  88. switch ($className) {
  89. case 'Memcache':
  90. return 'TestCache';
  91. default:
  92. return $className;
  93. }
  94. }
  95. public function tearDown()
  96. {
  97. unset_new_overload();
  98. }
  99. }
  100. class TestCache
  101. {
  102. private $vals = array();
  103. public function addServer($host, $port)
  104. {
  105. return true;
  106. }
  107. public function add($key, $value, $some, $expire = 0)
  108. {
  109. if(!isset($this->vals[$key])) {
  110. $this->vals[$key] = $value;
  111. return true;
  112. }
  113. return false;
  114. }
  115. public function decrement($key, $decrement = 1)
  116. {
  117. if (isset($this->vals[$key]) && is_int($this->vals[$key])) {
  118. $this->vals[$key]--;
  119. return true;
  120. }
  121. return false;
  122. }
  123. public function delete($key)
  124. {
  125. if (isset($this->vals[$key])) {
  126. unset($this->vals[$key]);
  127. }
  128. return true;
  129. }
  130. public function flush()
  131. {
  132. $this->vals = array();
  133. }
  134. public function get($key)
  135. {
  136. if (isset($this->vals[$key])) {
  137. return $this->vals[$key];
  138. }
  139. return false;
  140. }
  141. public function increment($key, $increment = 1)
  142. {
  143. if (isset($this->vals[$key]) && is_int($this->vals[$key])) {
  144. $this->vals[$key]++;
  145. return true;
  146. }
  147. return false;
  148. }
  149. public function replace($key, $value, $any = null, $expire = 0)
  150. {
  151. if (isset($this->vals[$key])) {
  152. $this->vals[$key] = $value;
  153. return true;
  154. }
  155. return false;
  156. }
  157. public function set($key, $value, $any = null, $expire = 0)
  158. {
  159. $this->vals[$key] = $value;
  160. return true;
  161. }
  162. }