CacheFileStoreTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\FileStore;
  4. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Support\Carbon;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. class CacheFileStoreTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. Carbon::setTestNow(Carbon::now());
  15. }
  16. protected function tearDown(): void
  17. {
  18. parent::tearDown();
  19. Carbon::setTestNow(null);
  20. }
  21. public function testNullIsReturnedIfFileDoesntExist()
  22. {
  23. $files = $this->mockFilesystem();
  24. $files->expects($this->once())->method('get')->will($this->throwException(new FileNotFoundException));
  25. $store = new FileStore($files, __DIR__);
  26. $value = $store->get('foo');
  27. $this->assertNull($value);
  28. }
  29. public function testUnserializableFileContentGetDeleted()
  30. {
  31. $files = $this->mockFilesystem();
  32. $hash = sha1('foo');
  33. $cachePath = __DIR__.'/'.substr($hash, 0, 2).'/'.substr($hash, 2, 2).'/'.$hash;
  34. $files->expects($this->once())->method('get')->willReturn('9999999999-I_am_unserializableee: \(~_~)/');
  35. $files->expects($this->once())->method('exists')->with($this->equalTo($cachePath))->willReturn(true);
  36. $files->expects($this->once())->method('delete')->with($this->equalTo($cachePath));
  37. $value = (new FileStore($files, __DIR__))->get('foo');
  38. $this->assertNull($value);
  39. }
  40. public function testPutCreatesMissingDirectories()
  41. {
  42. $files = $this->mockFilesystem();
  43. $hash = sha1('foo');
  44. $contents = '0000000000';
  45. $full_dir = __DIR__.'/'.substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  46. $files->expects($this->once())->method('makeDirectory')->with($this->equalTo($full_dir), $this->equalTo(0777), $this->equalTo(true));
  47. $files->expects($this->once())->method('put')->with($this->equalTo($full_dir.'/'.$hash))->willReturn(strlen($contents));
  48. $store = new FileStore($files, __DIR__);
  49. $result = $store->put('foo', $contents, 0);
  50. $this->assertTrue($result);
  51. }
  52. public function testPutWillConsiderZeroAsEternalTime()
  53. {
  54. $files = $this->mockFilesystem();
  55. $hash = sha1('O--L / key');
  56. $filePath = __DIR__.'/'.substr($hash, 0, 2).'/'.substr($hash, 2, 2).'/'.$hash;
  57. $ten9s = '9999999999'; // The "forever" time value.
  58. $fileContents = $ten9s.serialize('gold');
  59. $exclusiveLock = true;
  60. $files->expects($this->once())->method('put')->with(
  61. $this->equalTo($filePath),
  62. $this->equalTo($fileContents),
  63. $this->equalTo($exclusiveLock) // Ensure we do lock the file while putting.
  64. )->willReturn(strlen($fileContents));
  65. (new FileStore($files, __DIR__))->put('O--L / key', 'gold', 0);
  66. }
  67. public function testPutWillConsiderBigValuesAsEternalTime()
  68. {
  69. $files = $this->mockFilesystem();
  70. $hash = sha1('O--L / key');
  71. $filePath = __DIR__.'/'.substr($hash, 0, 2).'/'.substr($hash, 2, 2).'/'.$hash;
  72. $ten9s = '9999999999'; // The "forever" time value.
  73. $fileContents = $ten9s.serialize('gold');
  74. $files->expects($this->once())->method('put')->with(
  75. $this->equalTo($filePath),
  76. $this->equalTo($fileContents),
  77. );
  78. (new FileStore($files, __DIR__))->put('O--L / key', 'gold', (int) $ten9s + 1);
  79. }
  80. public function testExpiredItemsReturnNullAndGetDeleted()
  81. {
  82. $files = $this->mockFilesystem();
  83. $contents = '0000000000';
  84. $files->expects($this->once())->method('get')->willReturn($contents);
  85. $store = $this->getMockBuilder(FileStore::class)->onlyMethods(['forget'])->setConstructorArgs([$files, __DIR__])->getMock();
  86. $store->expects($this->once())->method('forget');
  87. $value = $store->get('foo');
  88. $this->assertNull($value);
  89. }
  90. public function testValidItemReturnsContents()
  91. {
  92. $files = $this->mockFilesystem();
  93. $contents = '9999999999'.serialize('Hello World');
  94. $files->expects($this->once())->method('get')->willReturn($contents);
  95. $store = new FileStore($files, __DIR__);
  96. $this->assertSame('Hello World', $store->get('foo'));
  97. }
  98. public function testStoreItemProperlyStoresValues()
  99. {
  100. $files = $this->mockFilesystem();
  101. $store = $this->getMockBuilder(FileStore::class)->onlyMethods(['expiration'])->setConstructorArgs([$files, __DIR__])->getMock();
  102. $store->expects($this->once())->method('expiration')->with($this->equalTo(10))->willReturn(1111111111);
  103. $contents = '1111111111'.serialize('Hello World');
  104. $hash = sha1('foo');
  105. $cache_dir = substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  106. $files->expects($this->once())->method('put')->with($this->equalTo(__DIR__.'/'.$cache_dir.'/'.$hash), $this->equalTo($contents))->willReturn(strlen($contents));
  107. $result = $store->put('foo', 'Hello World', 10);
  108. $this->assertTrue($result);
  109. }
  110. public function testStoreItemProperlySetsPermissions()
  111. {
  112. $files = m::mock(Filesystem::class);
  113. $files->shouldIgnoreMissing();
  114. $store = $this->getMockBuilder(FileStore::class)->onlyMethods(['expiration'])->setConstructorArgs([$files, __DIR__, 0644])->getMock();
  115. $hash = sha1('foo');
  116. $cache_dir = substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  117. $files->shouldReceive('put')->withArgs([__DIR__.'/'.$cache_dir.'/'.$hash, m::any(), m::any()])->andReturnUsing(function ($name, $value) {
  118. return strlen($value);
  119. });
  120. $files->shouldReceive('chmod')->withArgs([__DIR__.'/'.$cache_dir.'/'.$hash])->andReturnValues(['0600', '0644'])->times(3);
  121. $files->shouldReceive('chmod')->withArgs([__DIR__.'/'.$cache_dir.'/'.$hash, 0644])->andReturn([true])->once();
  122. $result = $store->put('foo', 'foo', 10);
  123. $this->assertTrue($result);
  124. $result = $store->put('foo', 'bar', 10);
  125. $this->assertTrue($result);
  126. $result = $store->put('foo', 'baz', 10);
  127. $this->assertTrue($result);
  128. m::close();
  129. }
  130. public function testStoreItemDirectoryProperlySetsPermissions()
  131. {
  132. $files = m::mock(Filesystem::class);
  133. $files->shouldIgnoreMissing();
  134. $store = $this->getMockBuilder(FileStore::class)->onlyMethods(['expiration'])->setConstructorArgs([$files, __DIR__, 0606])->getMock();
  135. $hash = sha1('foo');
  136. $cache_parent_dir = substr($hash, 0, 2);
  137. $cache_dir = $cache_parent_dir.'/'.substr($hash, 2, 2);
  138. $files->shouldReceive('put')->withArgs([__DIR__.'/'.$cache_dir.'/'.$hash, m::any(), m::any()])->andReturnUsing(function ($name, $value) {
  139. return strlen($value);
  140. });
  141. $files->shouldReceive('exists')->withArgs([__DIR__.'/'.$cache_dir])->andReturn(false)->once();
  142. $files->shouldReceive('makeDirectory')->withArgs([__DIR__.'/'.$cache_dir, 0777, true, true])->once();
  143. $files->shouldReceive('chmod')->withArgs([__DIR__.'/'.$cache_parent_dir])->andReturn(['0600'])->once();
  144. $files->shouldReceive('chmod')->withArgs([__DIR__.'/'.$cache_parent_dir, 0606])->andReturn([true])->once();
  145. $files->shouldReceive('chmod')->withArgs([__DIR__.'/'.$cache_dir])->andReturn(['0600'])->once();
  146. $files->shouldReceive('chmod')->withArgs([__DIR__.'/'.$cache_dir, 0606])->andReturn([true])->once();
  147. $result = $store->put('foo', 'foo', 10);
  148. $this->assertTrue($result);
  149. m::close();
  150. }
  151. public function testForeversAreStoredWithHighTimestamp()
  152. {
  153. $files = $this->mockFilesystem();
  154. $contents = '9999999999'.serialize('Hello World');
  155. $hash = sha1('foo');
  156. $cache_dir = substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  157. $files->expects($this->once())->method('put')->with($this->equalTo(__DIR__.'/'.$cache_dir.'/'.$hash), $this->equalTo($contents))->willReturn(strlen($contents));
  158. $store = new FileStore($files, __DIR__);
  159. $result = $store->forever('foo', 'Hello World', 10);
  160. $this->assertTrue($result);
  161. }
  162. public function testForeversAreNotRemovedOnIncrement()
  163. {
  164. $files = $this->mockFilesystem();
  165. $contents = '9999999999'.serialize('Hello World');
  166. $store = new FileStore($files, __DIR__);
  167. $store->forever('foo', 'Hello World');
  168. $store->increment('foo');
  169. $files->expects($this->once())->method('get')->willReturn($contents);
  170. $this->assertSame('Hello World', $store->get('foo'));
  171. }
  172. public function testIncrementCanAtomicallyJump()
  173. {
  174. $files = $this->mockFilesystem();
  175. $initialValue = '9999999999'.serialize(1);
  176. $valueAfterIncrement = '9999999999'.serialize(4);
  177. $store = new FileStore($files, __DIR__);
  178. $files->expects($this->once())->method('get')->willReturn($initialValue);
  179. $hash = sha1('foo');
  180. $cache_dir = substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  181. $files->expects($this->once())->method('put')->with($this->equalTo(__DIR__.'/'.$cache_dir.'/'.$hash), $this->equalTo($valueAfterIncrement));
  182. $store->increment('foo', 3);
  183. }
  184. public function testIncrementDoesNotExtendCacheLife()
  185. {
  186. $files = $this->mockFilesystem();
  187. $expiration = Carbon::now()->addSeconds(50)->getTimestamp();
  188. $initialValue = $expiration.serialize(1);
  189. $valueAfterIncrement = $expiration.serialize(2);
  190. $store = new FileStore($files, __DIR__);
  191. $files->expects($this->once())->method('get')->willReturn($initialValue);
  192. $hash = sha1('foo');
  193. $cache_dir = substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  194. $files->expects($this->once())->method('put')->with($this->equalTo(__DIR__.'/'.$cache_dir.'/'.$hash), $this->equalTo($valueAfterIncrement));
  195. $store->increment('foo');
  196. }
  197. public function testRemoveDeletesFileDoesntExist()
  198. {
  199. $files = $this->mockFilesystem();
  200. $hash = sha1('foobull');
  201. $cache_dir = substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  202. $files->expects($this->once())->method('exists')->with($this->equalTo(__DIR__.'/'.$cache_dir.'/'.$hash))->willReturn(false);
  203. $store = new FileStore($files, __DIR__);
  204. $store->forget('foobull');
  205. }
  206. public function testRemoveDeletesFile()
  207. {
  208. $files = $this->mockFilesystem();
  209. $hash = sha1('foobar');
  210. $cache_dir = substr($hash, 0, 2).'/'.substr($hash, 2, 2);
  211. $store = new FileStore($files, __DIR__);
  212. $store->put('foobar', 'Hello Baby', 10);
  213. $files->expects($this->once())->method('exists')->with($this->equalTo(__DIR__.'/'.$cache_dir.'/'.$hash))->willReturn(true);
  214. $files->expects($this->once())->method('delete')->with($this->equalTo(__DIR__.'/'.$cache_dir.'/'.$hash));
  215. $store->forget('foobar');
  216. }
  217. public function testFlushCleansDirectory()
  218. {
  219. $files = $this->mockFilesystem();
  220. $files->expects($this->once())->method('isDirectory')->with($this->equalTo(__DIR__))->willReturn(true);
  221. $files->expects($this->once())->method('directories')->with($this->equalTo(__DIR__))->willReturn(['foo']);
  222. $files->expects($this->once())->method('deleteDirectory')->with($this->equalTo('foo'))->willReturn(true);
  223. $store = new FileStore($files, __DIR__);
  224. $result = $store->flush();
  225. $this->assertTrue($result, 'Flush failed');
  226. }
  227. public function testFlushFailsDirectoryClean()
  228. {
  229. $files = $this->mockFilesystem();
  230. $files->expects($this->once())->method('isDirectory')->with($this->equalTo(__DIR__))->willReturn(true);
  231. $files->expects($this->once())->method('directories')->with($this->equalTo(__DIR__))->willReturn(['foo']);
  232. $files->expects($this->once())->method('deleteDirectory')->with($this->equalTo('foo'))->willReturn(false);
  233. $store = new FileStore($files, __DIR__);
  234. $result = $store->flush();
  235. $this->assertFalse($result, 'Flush should not have cleared directories');
  236. }
  237. public function testFlushIgnoreNonExistingDirectory()
  238. {
  239. $files = $this->mockFilesystem();
  240. $files->expects($this->once())->method('isDirectory')->with($this->equalTo(__DIR__.'--wrong'))->willReturn(false);
  241. $store = new FileStore($files, __DIR__.'--wrong');
  242. $result = $store->flush();
  243. $this->assertFalse($result, 'Flush should not clean directory');
  244. }
  245. protected function mockFilesystem()
  246. {
  247. return $this->createMock(Filesystem::class);
  248. }
  249. }