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.

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