ArrayKeyCacheTest.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/InputByteStream.php';
  4. require_once 'Swift/OutputByteStream.php';
  5. require_once 'Swift/KeyCache/ArrayKeyCache.php';
  6. require_once 'Swift/KeyCache/KeyCacheInputStream.php';
  7. require_once 'Swift/KeyCache.php';
  8. class Swift_KeyCache_ArrayKeyCacheTest extends Swift_Tests_SwiftUnitTestCase
  9. {
  10. private $_key1 = 'key1';
  11. private $_key2 = 'key2';
  12. public function testStringDataCanBeSetAndFetched()
  13. {
  14. $is = $this->_createKeyCacheInputStream(true);
  15. $cache = $this->_createCache($is);
  16. $cache->setString(
  17. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  18. );
  19. $this->assertEqual('test', $cache->getString($this->_key1, 'foo'));
  20. }
  21. public function testStringDataCanBeOverwritten()
  22. {
  23. $is = $this->_createKeyCacheInputStream(true);
  24. $cache = $this->_createCache($is);
  25. $cache->setString(
  26. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  27. );
  28. $cache->setString(
  29. $this->_key1, 'foo', 'whatever', Swift_KeyCache::MODE_WRITE
  30. );
  31. $this->assertEqual('whatever', $cache->getString($this->_key1, 'foo'));
  32. }
  33. public function testStringDataCanBeAppended()
  34. {
  35. $is = $this->_createKeyCacheInputStream(true);
  36. $cache = $this->_createCache($is);
  37. $cache->setString(
  38. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  39. );
  40. $cache->setString(
  41. $this->_key1, 'foo', 'ing', Swift_KeyCache::MODE_APPEND
  42. );
  43. $this->assertEqual('testing', $cache->getString($this->_key1, 'foo'));
  44. }
  45. public function testHasKeyReturnValue()
  46. {
  47. $is = $this->_createKeyCacheInputStream(true);
  48. $cache = $this->_createCache($is);
  49. $cache->setString(
  50. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  51. );
  52. $this->assertTrue($cache->hasKey($this->_key1, 'foo'));
  53. }
  54. public function testNsKeyIsWellPartitioned()
  55. {
  56. $is = $this->_createKeyCacheInputStream(true);
  57. $cache = $this->_createCache($is);
  58. $cache->setString(
  59. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  60. );
  61. $cache->setString(
  62. $this->_key2, 'foo', 'ing', Swift_KeyCache::MODE_WRITE
  63. );
  64. $this->assertEqual('test', $cache->getString($this->_key1, 'foo'));
  65. $this->assertEqual('ing', $cache->getString($this->_key2, 'foo'));
  66. }
  67. public function testItemKeyIsWellPartitioned()
  68. {
  69. $is = $this->_createKeyCacheInputStream(true);
  70. $cache = $this->_createCache($is);
  71. $cache->setString(
  72. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  73. );
  74. $cache->setString(
  75. $this->_key1, 'bar', 'ing', Swift_KeyCache::MODE_WRITE
  76. );
  77. $this->assertEqual('test', $cache->getString($this->_key1, 'foo'));
  78. $this->assertEqual('ing', $cache->getString($this->_key1, 'bar'));
  79. }
  80. public function testByteStreamCanBeImported()
  81. {
  82. $os = $this->_createOutputStream();
  83. $this->_checking(Expectations::create()
  84. -> one($os)->read(optional()) -> returns('abc')
  85. -> one($os)->read(optional()) -> returns('def')
  86. -> one($os)->read(optional()) -> returns(false)
  87. -> ignoring($os)
  88. );
  89. $is = $this->_createKeyCacheInputStream(true);
  90. $cache = $this->_createCache($is);
  91. $cache->importFromByteStream(
  92. $this->_key1, 'foo', $os, Swift_KeyCache::MODE_WRITE
  93. );
  94. $this->assertEqual('abcdef', $cache->getString($this->_key1, 'foo'));
  95. }
  96. public function testByteStreamCanBeAppended()
  97. {
  98. $os1 = $this->_createOutputStream();
  99. $os2 = $this->_createOutputStream();
  100. $this->_checking(Expectations::create()
  101. -> one($os1)->read(optional()) -> returns('abc')
  102. -> one($os1)->read(optional()) -> returns('def')
  103. -> one($os1)->read(optional()) -> returns(false)
  104. -> ignoring($os1)
  105. -> one($os2)->read(optional()) -> returns('xyz')
  106. -> one($os2)->read(optional()) -> returns('uvw')
  107. -> one($os2)->read(optional()) -> returns(false)
  108. -> ignoring($os2)
  109. );
  110. $is = $this->_createKeyCacheInputStream(true);
  111. $cache = $this->_createCache($is);
  112. $cache->importFromByteStream(
  113. $this->_key1, 'foo', $os1, Swift_KeyCache::MODE_APPEND
  114. );
  115. $cache->importFromByteStream(
  116. $this->_key1, 'foo', $os2, Swift_KeyCache::MODE_APPEND
  117. );
  118. $this->assertEqual('abcdefxyzuvw', $cache->getString($this->_key1, 'foo'));
  119. }
  120. public function testByteStreamAndStringCanBeAppended()
  121. {
  122. $os = $this->_createOutputStream();
  123. $this->_checking(Expectations::create()
  124. -> one($os)->read(optional()) -> returns('abc')
  125. -> one($os)->read(optional()) -> returns('def')
  126. -> one($os)->read(optional()) -> returns(false)
  127. -> ignoring($os)
  128. );
  129. $is = $this->_createKeyCacheInputStream(true);
  130. $cache = $this->_createCache($is);
  131. $cache->setString(
  132. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_APPEND
  133. );
  134. $cache->importFromByteStream(
  135. $this->_key1, 'foo', $os, Swift_KeyCache::MODE_APPEND
  136. );
  137. $this->assertEqual('testabcdef', $cache->getString($this->_key1, 'foo'));
  138. }
  139. public function testDataCanBeExportedToByteStream()
  140. {
  141. //See acceptance test for more detail
  142. $is = $this->_createInputStream();
  143. $this->_checking(Expectations::create()
  144. -> atLeast(1)->of($is)->write(any())
  145. -> ignoring($is)
  146. );
  147. $kcis = $this->_createKeyCacheInputStream(true);
  148. $cache = $this->_createCache($kcis);
  149. $cache->setString(
  150. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  151. );
  152. $cache->exportToByteStream($this->_key1, 'foo', $is);
  153. }
  154. public function testKeyCanBeCleared()
  155. {
  156. $is = $this->_createKeyCacheInputStream(true);
  157. $cache = $this->_createCache($is);
  158. $cache->setString(
  159. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  160. );
  161. $this->assertTrue($cache->hasKey($this->_key1, 'foo'));
  162. $cache->clearKey($this->_key1, 'foo');
  163. $this->assertFalse($cache->hasKey($this->_key1, 'foo'));
  164. }
  165. public function testNsKeyCanBeCleared()
  166. {
  167. $is = $this->_createKeyCacheInputStream(true);
  168. $cache = $this->_createCache($is);
  169. $cache->setString(
  170. $this->_key1, 'foo', 'test', Swift_KeyCache::MODE_WRITE
  171. );
  172. $cache->setString(
  173. $this->_key1, 'bar', 'xyz', Swift_KeyCache::MODE_WRITE
  174. );
  175. $this->assertTrue($cache->hasKey($this->_key1, 'foo'));
  176. $this->assertTrue($cache->hasKey($this->_key1, 'bar'));
  177. $cache->clearAll($this->_key1);
  178. $this->assertFalse($cache->hasKey($this->_key1, 'foo'));
  179. $this->assertFalse($cache->hasKey($this->_key1, 'bar'));
  180. }
  181. // -- Creation methods
  182. private function _createCache($is)
  183. {
  184. return new Swift_KeyCache_ArrayKeyCache($is);
  185. }
  186. private function _createKeyCacheInputStream($stub = false)
  187. {
  188. return $stub
  189. ? $this->_stub('Swift_KeyCache_KeyCacheInputStream')
  190. : $this->_mock('Swift_KeyCache_KeyCacheInputStream')
  191. ;
  192. }
  193. private function _createOutputStream($stub = false)
  194. {
  195. return $stub
  196. ? $this->_stub('Swift_OutputByteStream')
  197. : $this->_mock('Swift_OutputByteStream')
  198. ;
  199. }
  200. private function _createInputStream($stub = false)
  201. {
  202. return $stub
  203. ? $this->_stub('Swift_InputByteStream')
  204. : $this->_mock('Swift_InputByteStream')
  205. ;
  206. }
  207. }