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.

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