CacheTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Cache test class
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2012 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Test\Cache
  9. */
  10. class GitPHP_CacheTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testFile()
  13. {
  14. $cache = new GitPHP_Cache(new GitPHP_Cache_File(GITPHP_CACHEDIR . 'objects', 0, false));
  15. $cache->Clear();
  16. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  17. $cache->Set('testkey1|testkey2', 'testvalue1');
  18. $this->assertTrue($cache->Exists('testkey1|testkey2'));
  19. $this->assertEquals('testvalue1', $cache->Get('testkey1|testkey2'));
  20. $this->assertFalse($cache->Get('testkey3|testkey4'));
  21. $cache->Set('testkey3|testkey4', 'testvalue2');
  22. $cache->Delete('testkey1|testkey2');
  23. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  24. $this->assertTrue($cache->Exists('testkey3|testkey4'));
  25. $cache->Clear();
  26. $this->assertFalse($cache->Exists('testkey3|testkey4'));
  27. }
  28. public function testFileLifetime()
  29. {
  30. $cache = new GitPHP_Cache(new GitPHP_Cache_File(GITPHP_CACHEDIR . 'objects', 0, false));
  31. $cache->Clear();
  32. $cache->Set('testkey1|testkey2', 'testvalue1', 1);
  33. sleep(2);
  34. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  35. $cache->SetLifetime(1);
  36. $cache->Set('testkey3|testkey4', 'testvalue2');
  37. sleep(2);
  38. $this->assertFalse($cache->Get('testkey3|testkey4'));
  39. }
  40. public function testFileCompressed()
  41. {
  42. $cache = new GitPHP_Cache(new GitPHP_Cache_File(GITPHP_CACHEDIR . 'objects', 19, false));
  43. $cache->Clear();
  44. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  45. $cache->Set('testkey1|testkey2', '12345678');
  46. $this->assertTrue($cache->Exists('testkey1|testkey2'));
  47. $this->assertEquals('12345678', $cache->Get('testkey1|testkey2'));
  48. $this->assertFalse($cache->Get('testkey3|testkey4'));
  49. $cache->Set('testkey3|testkey4', '12345678901234567890');
  50. $this->assertTrue($cache->Exists('testkey3|testkey4'));
  51. $this->assertEquals('12345678901234567890', $cache->Get('testkey3|testkey4'));
  52. $cache->Delete('testkey1|testkey2');
  53. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  54. $this->assertTrue($cache->Exists('testkey3|testkey4'));
  55. $cache->Clear();
  56. $this->assertFalse($cache->Exists('testkey3|testkey4'));
  57. }
  58. public function testFileIgbinary()
  59. {
  60. if (!function_exists('igbinary_serialize')) {
  61. $this->markTestSkipped();
  62. return;
  63. }
  64. $cache = new GitPHP_Cache(new GitPHP_Cache_File(GITPHP_CACHEDIR . 'objects', 0, true));
  65. $cache->Clear();
  66. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  67. $cache->Set('testkey1|testkey2', '12345678');
  68. $this->assertTrue($cache->Exists('testkey1|testkey2'));
  69. $this->assertEquals('12345678', $cache->Get('testkey1|testkey2'));
  70. $this->assertFalse($cache->Get('testkey3|testkey4'));
  71. $cache->Set('testkey3|testkey4', '12345678901234567890');
  72. $this->assertTrue($cache->Exists('testkey3|testkey4'));
  73. $this->assertEquals('12345678901234567890', $cache->Get('testkey3|testkey4'));
  74. $cache->Delete('testkey1|testkey2');
  75. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  76. $this->assertTrue($cache->Exists('testkey3|testkey4'));
  77. $cache->Clear();
  78. $this->assertFalse($cache->Exists('testkey3|testkey4'));
  79. }
  80. public function testMemcache()
  81. {
  82. if (!class_exists('Memcache')) {
  83. $this->markTestSkipped();
  84. return;
  85. }
  86. $cache = new GitPHP_Cache(new GitPHP_Cache_Memcache(array(array('127.0.0.1', 11211))));
  87. $cache->Clear();
  88. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  89. $cache->Set('testkey1|testkey2', 'testvalue1');
  90. $this->assertTrue($cache->Exists('testkey1|testkey2'));
  91. $this->assertEquals('testvalue1', $cache->Get('testkey1|testkey2'));
  92. $this->assertFalse($cache->Get('testkey3|testkey4'));
  93. $cache->Set('testkey3|testkey4', 'testvalue2');
  94. $cache->Delete('testkey1|testkey2');
  95. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  96. $this->assertTrue($cache->Exists('testkey3|testkey4'));
  97. $cache->Clear();
  98. $this->assertFalse($cache->Exists('testkey3|testkey4'));
  99. }
  100. public function testMemcacheLifetime()
  101. {
  102. if (!class_exists('Memcache')) {
  103. $this->markTestSkipped();
  104. return;
  105. }
  106. $cache = new GitPHP_Cache(new GitPHP_Cache_Memcache(array(array('127.0.0.1', 11211))));
  107. $cache->Clear();
  108. $cache->Set('testkey1|testkey2', 'testvalue1', 1);
  109. sleep(2);
  110. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  111. $cache->SetLifetime(1);
  112. $cache->Set('testkey3|testkey4', 'testvalue2');
  113. sleep(2);
  114. $this->assertFalse($cache->Get('testkey3|testkey4'));
  115. }
  116. public function testMemcached()
  117. {
  118. if (!class_exists('Memcached')) {
  119. $this->markTestSkipped();
  120. return;
  121. }
  122. $cache = new GitPHP_Cache(new GitPHP_Cache_Memcached(array(array('127.0.0.1', 11211))));
  123. $cache->Clear();
  124. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  125. $cache->Set('testkey1|testkey2', 'testvalue1');
  126. $this->assertTrue($cache->Exists('testkey1|testkey2'));
  127. $this->assertEquals('testvalue1', $cache->Get('testkey1|testkey2'));
  128. $this->assertFalse($cache->Get('testkey3|testkey4'));
  129. $cache->Set('testkey3|testkey4', 'testvalue2');
  130. $cache->Delete('testkey1|testkey2');
  131. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  132. $this->assertTrue($cache->Exists('testkey3|testkey4'));
  133. $cache->Clear();
  134. $this->assertFalse($cache->Exists('testkey3|testkey4'));
  135. }
  136. public function testMemcachedLifetime()
  137. {
  138. if (!class_exists('Memcached')) {
  139. $this->markTestSkipped();
  140. return;
  141. }
  142. $cache = new GitPHP_Cache(new GitPHP_Cache_Memcached(array(array('127.0.0.1', 11211))));
  143. $cache->Clear();
  144. $cache->Set('testkey1|testkey2', 'testvalue1', 1);
  145. sleep(2);
  146. $this->assertFalse($cache->Exists('testkey1|testkey2'));
  147. $cache->SetLifetime(1);
  148. $cache->Set('testkey3|testkey4', 'testvalue2');
  149. sleep(2);
  150. $this->assertFalse($cache->Get('testkey3|testkey4'));
  151. }
  152. }