ArrayKeyCacheAcceptanceTest.php 5.4KB

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