MemoryCacheTests.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. use League\Flysystem\Cached\Storage\Memory;
  3. use League\Flysystem\Util;
  4. use PHPUnit\Framework\TestCase;
  5. class MemoryCacheTests extends TestCase
  6. {
  7. public function testAutosave()
  8. {
  9. $cache = new Memory();
  10. $cache->setAutosave(true);
  11. $this->assertTrue($cache->getAutosave());
  12. $cache->setAutosave(false);
  13. $this->assertFalse($cache->getAutosave());
  14. }
  15. public function testCacheMiss()
  16. {
  17. $cache = new Memory();
  18. $cache->storeMiss('path.txt');
  19. $this->assertFalse($cache->has('path.txt'));
  20. }
  21. public function testIsComplete()
  22. {
  23. $cache = new Memory();
  24. $this->assertFalse($cache->isComplete('dirname', false));
  25. $cache->setComplete('dirname', false);
  26. $this->assertFalse($cache->isComplete('dirname', true));
  27. $cache->setComplete('dirname', true);
  28. $this->assertTrue($cache->isComplete('dirname', true));
  29. }
  30. public function testCleanContents()
  31. {
  32. $cache = new Memory();
  33. $input = [[
  34. 'path' => 'path.txt',
  35. 'visibility' => 'public',
  36. 'invalid' => 'thing',
  37. ]];
  38. $expected = [[
  39. 'path' => 'path.txt',
  40. 'visibility' => 'public',
  41. ]];
  42. $output = $cache->cleanContents($input);
  43. $this->assertEquals($expected, $output);
  44. }
  45. public function testGetForStorage()
  46. {
  47. $cache = new Memory();
  48. $input = [[
  49. 'path' => 'path.txt',
  50. 'visibility' => 'public',
  51. 'type' => 'file',
  52. ]];
  53. $cache->storeContents('', $input, true);
  54. $contents = $cache->listContents('', true);
  55. $cached = [];
  56. foreach ($contents as $item) {
  57. $cached[$item['path']] = $item;
  58. }
  59. $this->assertEquals(json_encode([$cached, ['' => 'recursive']]), $cache->getForStorage());
  60. }
  61. public function testParentCompleteIsUsedDuringHas()
  62. {
  63. $cache = new Memory();
  64. $cache->setComplete('dirname', false);
  65. $this->assertFalse($cache->has('dirname/path.txt'));
  66. }
  67. public function testFlush()
  68. {
  69. $cache = new Memory();
  70. $cache->setComplete('dirname', true);
  71. $cache->updateObject('path.txt', [
  72. 'path' => 'path.txt',
  73. 'visibility' => 'public',
  74. ]);
  75. $cache->flush();
  76. $this->assertFalse($cache->isComplete('dirname', true));
  77. $this->assertNull($cache->has('path.txt'));
  78. }
  79. public function testSetFromStorage()
  80. {
  81. $cache = new Memory();
  82. $json = [[
  83. 'path.txt' => ['path' => 'path.txt', 'type' => 'file'],
  84. ], ['dirname' => 'recursive']];
  85. $jsonString = json_encode($json);
  86. $cache->setFromStorage($jsonString);
  87. $this->assertTrue($cache->has('path.txt'));
  88. $this->assertTrue($cache->isComplete('dirname', true));
  89. }
  90. public function testGetMetadataFail()
  91. {
  92. $cache = new Memory();
  93. $this->assertFalse($cache->getMetadata('path.txt'));
  94. }
  95. public function metaGetterProvider()
  96. {
  97. return [
  98. ['getTimestamp', 'timestamp', 12344],
  99. ['getMimetype', 'mimetype', 'text/plain'],
  100. ['getSize', 'size', 12],
  101. ['getVisibility', 'visibility', 'private'],
  102. ['read', 'contents', '__contents__'],
  103. ];
  104. }
  105. /**
  106. * @dataProvider metaGetterProvider
  107. *
  108. * @param $method
  109. * @param $key
  110. * @param $value
  111. */
  112. public function testMetaGetters($method, $key, $value)
  113. {
  114. $cache = new Memory();
  115. $this->assertFalse($cache->{$method}('path.txt'));
  116. $cache->updateObject('path.txt', $object = [
  117. 'path' => 'path.txt',
  118. 'type' => 'file',
  119. $key => $value,
  120. ] + Util::pathinfo('path.txt'), true);
  121. $this->assertEquals($object, $cache->{$method}('path.txt'));
  122. $this->assertEquals($object, $cache->getMetadata('path.txt'));
  123. }
  124. public function testGetDerivedMimetype()
  125. {
  126. $cache = new Memory();
  127. $cache->updateObject('path.txt', [
  128. 'contents' => 'something',
  129. ]);
  130. $response = $cache->getMimetype('path.txt');
  131. $this->assertEquals('text/plain', $response['mimetype']);
  132. }
  133. public function testCopyFail()
  134. {
  135. $cache = new Memory();
  136. $cache->copy('one', 'two');
  137. $this->assertNull($cache->has('two'));
  138. $this->assertNull($cache->load());
  139. }
  140. public function testStoreContents()
  141. {
  142. $cache = new Memory();
  143. $cache->storeContents('dirname', [
  144. ['path' => 'dirname', 'type' => 'dir'],
  145. ['path' => 'dirname/nested', 'type' => 'dir'],
  146. ['path' => 'dirname/nested/deep', 'type' => 'dir'],
  147. ['path' => 'other/nested/deep', 'type' => 'dir'],
  148. ], true);
  149. $this->isTrue($cache->isComplete('other/nested', true));
  150. }
  151. public function testDelete()
  152. {
  153. $cache = new Memory();
  154. $cache->updateObject('path.txt', ['type' => 'file']);
  155. $this->assertTrue($cache->has('path.txt'));
  156. $cache->delete('path.txt');
  157. $this->assertFalse($cache->has('path.txt'));
  158. }
  159. public function testDeleteDir()
  160. {
  161. $cache = new Memory();
  162. $cache->storeContents('dirname', [
  163. ['path' => 'dirname/path.txt', 'type' => 'file'],
  164. ]);
  165. $this->assertTrue($cache->isComplete('dirname', false));
  166. $this->assertTrue($cache->has('dirname/path.txt'));
  167. $cache->deleteDir('dirname');
  168. $this->assertFalse($cache->isComplete('dirname', false));
  169. $this->assertNull($cache->has('dirname/path.txt'));
  170. }
  171. public function testReadStream()
  172. {
  173. $cache = new Memory();
  174. $this->assertFalse($cache->readStream('path.txt'));
  175. }
  176. public function testRename()
  177. {
  178. $cache = new Memory();
  179. $cache->updateObject('path.txt', ['type' => 'file']);
  180. $cache->rename('path.txt', 'newpath.txt');
  181. $this->assertTrue($cache->has('newpath.txt'));
  182. }
  183. public function testCopy()
  184. {
  185. $cache = new Memory();
  186. $cache->updateObject('path.txt', ['type' => 'file']);
  187. $cache->copy('path.txt', 'newpath.txt');
  188. $this->assertTrue($cache->has('newpath.txt'));
  189. }
  190. public function testComplextListContents()
  191. {
  192. $cache = new Memory();
  193. $cache->storeContents('', [
  194. ['path' => 'dirname', 'type' => 'dir'],
  195. ['path' => 'dirname/file.txt', 'type' => 'file'],
  196. ['path' => 'other', 'type' => 'dir'],
  197. ['path' => 'other/file.txt', 'type' => 'file'],
  198. ['path' => 'other/nested/file.txt', 'type' => 'file'],
  199. ]);
  200. $this->assertCount(3, $cache->listContents('other', true));
  201. }
  202. public function testComplextListContentsWithDeletedFile()
  203. {
  204. $cache = new Memory();
  205. $cache->storeContents('', [
  206. ['path' => 'dirname', 'type' => 'dir'],
  207. ['path' => 'dirname/file.txt', 'type' => 'file'],
  208. ['path' => 'other', 'type' => 'dir'],
  209. ['path' => 'other/file.txt', 'type' => 'file'],
  210. ['path' => 'other/another_file.txt', 'type' => 'file'],
  211. ]);
  212. $cache->delete('other/another_file.txt');
  213. $this->assertCount(4, $cache->listContents('', true));
  214. }
  215. public function testCacheMissIfContentsIsFalse()
  216. {
  217. $cache = new Memory();
  218. $cache->updateObject('path.txt', [
  219. 'path' => 'path.txt',
  220. 'contents' => false,
  221. ], true);
  222. $this->assertFalse($cache->read('path.txt'));
  223. }
  224. }