DiskKeyCacheAcceptanceTest.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. require_once 'Swift/ByteStream/ArrayByteStream.php';
  3. require_once 'Swift/KeyCache/DiskKeyCache.php';
  4. require_once 'Swift/KeyCache/SimpleKeyCacheInputStream.php';
  5. require_once 'Swift/KeyCache.php';
  6. class Swift_KeyCache_DiskKeyCacheAcceptanceTest extends UnitTestCase
  7. {
  8. private $_cache;
  9. private $_key1;
  10. private $_key2;
  11. public function skip()
  12. {
  13. $this->skipUnless(
  14. SWIFT_TMP_DIR, 'Cannot run test without a writable directory to use (' .
  15. 'define SWIFT_TMP_DIR in tests/config.php if you wish to run this test)'
  16. );
  17. }
  18. public function setUp()
  19. {
  20. $this->_key1 = uniqid(microtime(true), true);
  21. $this->_key2 = uniqid(microtime(true), true);
  22. $this->_cache = new Swift_KeyCache_DiskKeyCache(
  23. new Swift_KeyCache_SimpleKeyCacheInputStream(),
  24. SWIFT_TMP_DIR
  25. );
  26. }
  27. public function testStringDataCanBeSetAndFetched()
  28. {
  29. $this->_cache->setString(
  30. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  31. );
  32. $this->assertEqual('test', $this->_cache->getString($this->_key1, 'foo'));
  33. }
  34. public function testStringDataCanBeOverwritten()
  35. {
  36. $this->_cache->setString(
  37. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  38. );
  39. $this->_cache->setString(
  40. $this->_key1, 'foo', 'whatever', Swift_KeyCache::MODE_WRITE
  41. );
  42. $this->assertEqual('whatever', $this->_cache->getString($this->_key1, 'foo'));
  43. }
  44. public function testStringDataCanBeAppended()
  45. {
  46. $this->_cache->setString(
  47. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  48. );
  49. $this->_cache->setString(
  50. $this->_key1, 'foo', 'ing', Swift_KeyCache::MODE_APPEND
  51. );
  52. $this->assertEqual('testing', $this->_cache->getString($this->_key1, 'foo'));
  53. }
  54. public function testHasKeyReturnValue()
  55. {
  56. $this->assertFalse($this->_cache->hasKey($this->_key1, 'foo'));
  57. $this->_cache->setString(
  58. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  59. );
  60. $this->assertTrue($this->_cache->hasKey($this->_key1, 'foo'));
  61. }
  62. public function testNsKeyIsWellPartitioned()
  63. {
  64. $this->_cache->setString(
  65. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  66. );
  67. $this->_cache->setString(
  68. $this->_key2, 'foo', 'ing', Swift_KeyCache::MODE_WRITE
  69. );
  70. $this->assertEqual('test', $this->_cache->getString($this->_key1, 'foo'));
  71. $this->assertEqual('ing', $this->_cache->getString($this->_key2, 'foo'));
  72. }
  73. public function testItemKeyIsWellPartitioned()
  74. {
  75. $this->_cache->setString(
  76. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  77. );
  78. $this->_cache->setString(
  79. $this->_key1, 'bar', 'ing', Swift_KeyCache::MODE_WRITE
  80. );
  81. $this->assertEqual('test', $this->_cache->getString($this->_key1, 'foo'));
  82. $this->assertEqual('ing', $this->_cache->getString($this->_key1, 'bar'));
  83. }
  84. public function testByteStreamCanBeImported()
  85. {
  86. $os = new Swift_ByteStream_ArrayByteStream();
  87. $os->write('abcdef');
  88. $this->_cache->importFromByteStream(
  89. $this->_key1, 'foo', $os, Swift_KeyCache::MODE_WRITE
  90. );
  91. $this->assertEqual('abcdef', $this->_cache->getString($this->_key1, 'foo'));
  92. }
  93. public function testByteStreamCanBeAppended()
  94. {
  95. $os1 = new Swift_ByteStream_ArrayByteStream();
  96. $os1->write('abcdef');
  97. $os2 = new Swift_ByteStream_ArrayByteStream();
  98. $os2->write('xyzuvw');
  99. $this->_cache->importFromByteStream(
  100. $this->_key1, 'foo', $os1, Swift_KeyCache::MODE_APPEND
  101. );
  102. $this->_cache->importFromByteStream(
  103. $this->_key1, 'foo', $os2, Swift_KeyCache::MODE_APPEND
  104. );
  105. $this->assertEqual('abcdefxyzuvw', $this->_cache->getString($this->_key1, 'foo'));
  106. }
  107. public function testByteStreamAndStringCanBeAppended()
  108. {
  109. $this->_cache->setString(
  110. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_APPEND
  111. );
  112. $os = new Swift_ByteStream_ArrayByteStream();
  113. $os->write('abcdef');
  114. $this->_cache->importFromByteStream(
  115. $this->_key1, 'foo', $os, Swift_KeyCache::MODE_APPEND
  116. );
  117. $this->assertEqual('testabcdef', $this->_cache->getString($this->_key1, 'foo'));
  118. }
  119. public function testDataCanBeExportedToByteStream()
  120. {
  121. $this->_cache->setString(
  122. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  123. );
  124. $is = new Swift_ByteStream_ArrayByteStream();
  125. $this->_cache->exportToByteStream($this->_key1, 'foo', $is);
  126. $string = '';
  127. while (false !== $bytes = $is->read(8192))
  128. {
  129. $string .= $bytes;
  130. }
  131. $this->assertEqual('test', $string);
  132. }
  133. public function testKeyCanBeCleared()
  134. {
  135. $this->_cache->setString(
  136. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  137. );
  138. $this->assertTrue($this->_cache->hasKey($this->_key1, 'foo'));
  139. $this->_cache->clearKey($this->_key1, 'foo');
  140. $this->assertFalse($this->_cache->hasKey($this->_key1, 'foo'));
  141. }
  142. public function testNsKeyCanBeCleared()
  143. {
  144. $this->_cache->setString(
  145. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  146. );
  147. $this->_cache->setString(
  148. $this->_key1, 'bar', 'xyz', Swift_KeyCache::MODE_WRITE
  149. );
  150. $this->assertTrue($this->_cache->hasKey($this->_key1, 'foo'));
  151. $this->assertTrue($this->_cache->hasKey($this->_key1, 'bar'));
  152. $this->_cache->clearAll($this->_key1);
  153. $this->assertFalse($this->_cache->hasKey($this->_key1, 'foo'));
  154. $this->assertFalse($this->_cache->hasKey($this->_key1, 'bar'));
  155. }
  156. public function testKeyCacheInputStream()
  157. {
  158. $is = $this->_cache->getInputByteStream($this->_key1, 'foo');
  159. $is->write('abc');
  160. $is->write('xyz');
  161. $this->assertEqual('abcxyz', $this->_cache->getString($this->_key1, 'foo'));
  162. }
  163. }