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.

206 lines
4.9 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. /**
  30. * @expectedException InitializationException
  31. * @expectedExceptionMessage Configuration must have a
  32. * @TODO: MemcacheCache::__construct - empty config array passes with no host params
  33. */
  34. public function testWrongConfig()
  35. {
  36. $config = array('some' => array());
  37. $memcache = new MemcacheCache($config);
  38. }
  39. public function testAddGet()
  40. {
  41. $memcache = new MemcacheCache(array());
  42. $this->assertTrue($memcache->add('key', 'value'));
  43. $this->assertEquals('value', $memcache->get('key'));
  44. $this->assertFalse($memcache->add('key', 'newvalue'));
  45. }
  46. public function testIncrementDecrement()
  47. {
  48. $memcache = new MemcacheCache(array());
  49. $memcache->add('one', 1);
  50. $memcache->add('two', 2);
  51. $memcache->add('three', 'three');
  52. $this->assertTrue($memcache->increment('one'));
  53. $this->assertEquals(2, $memcache->get('one'));
  54. $this->assertTrue($memcache->decrement('two'));
  55. $this->assertEquals(1, $memcache->get('two'));
  56. $this->assertFalse($memcache->decrement('three'));
  57. }
  58. public function testDelete()
  59. {
  60. $memcache = new MemcacheCache(array());
  61. $memcache->add('one', 1);
  62. $memcache->add('two', 2);
  63. $memcache->del('one');
  64. $this->assertEquals(2, $memcache->get('two'));
  65. $this->assertFalse($memcache->get('one'));
  66. }
  67. public function testFlush()
  68. {
  69. $memcache = new MemcacheCache(array());
  70. $memcache->add('one', 1);
  71. $memcache->add('two', 2);
  72. $memcache->add('three', 'three');
  73. $this->assertEquals('three', 'three');
  74. $memcache->flush();
  75. $this->assertFalse($memcache->get('three'));
  76. $this->assertFalse($memcache->get('one'));
  77. }
  78. public function testSetReplace()
  79. {
  80. $memcache = new MemcacheCache(array());
  81. $memcache->add('one', 1);
  82. $memcache->add('two', 2);
  83. $memcache->add('three', 'three');
  84. $memcache->set('one', 30);
  85. $memcache->replace('three', 3);
  86. $this->assertEquals(30, $memcache->get('one'));
  87. $this->assertEquals(3, $memcache->get('three'));
  88. }
  89. protected function newCallback($className)
  90. {
  91. switch ($className) {
  92. case 'Memcache':
  93. return 'TestCache';
  94. default:
  95. return $className;
  96. }
  97. }
  98. public function tearDown()
  99. {
  100. unset_new_overload();
  101. }
  102. }
  103. class TestCache
  104. {
  105. private $vals = array();
  106. public function addServer($host, $port)
  107. {
  108. return true;
  109. }
  110. public function add($key, $value, $some, $expire = 0)
  111. {
  112. if(!isset($this->vals[$key])) {
  113. $this->vals[$key] = $value;
  114. return true;
  115. }
  116. return false;
  117. }
  118. public function decrement($key, $decrement = 1)
  119. {
  120. if (isset($this->vals[$key]) && is_int($this->vals[$key])) {
  121. $this->vals[$key]--;
  122. return true;
  123. }
  124. return false;
  125. }
  126. public function delete($key)
  127. {
  128. if (isset($this->vals[$key])) {
  129. unset($this->vals[$key]);
  130. }
  131. return true;
  132. }
  133. public function flush()
  134. {
  135. $this->vals = array();
  136. }
  137. public function get($key)
  138. {
  139. if (isset($this->vals[$key])) {
  140. return $this->vals[$key];
  141. }
  142. return false;
  143. }
  144. public function increment($key, $increment = 1)
  145. {
  146. if (isset($this->vals[$key]) && is_int($this->vals[$key])) {
  147. $this->vals[$key]++;
  148. return true;
  149. }
  150. return false;
  151. }
  152. public function replace($key, $value, $any = null, $expire = 0)
  153. {
  154. if (isset($this->vals[$key])) {
  155. $this->vals[$key] = $value;
  156. return true;
  157. }
  158. return false;
  159. }
  160. public function set($key, $value, $any = null, $expire = 0)
  161. {
  162. $this->vals[$key] = $value;
  163. return true;
  164. }
  165. }