AdapterCacheTests.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. use League\Flysystem\Cached\Storage\Adapter;
  3. use PHPUnit\Framework\TestCase;
  4. class AdapterCacheTests extends TestCase
  5. {
  6. public function testLoadFail()
  7. {
  8. $adapter = Mockery::mock('League\Flysystem\AdapterInterface');
  9. $adapter->shouldReceive('has')->once()->with('file.json')->andReturn(false);
  10. $cache = new Adapter($adapter, 'file.json', 10);
  11. $cache->load();
  12. $this->assertFalse($cache->isComplete('', false));
  13. }
  14. public function testLoadExpired()
  15. {
  16. $response = ['contents' => json_encode([[], ['' => true], 1234567890]), 'path' => 'file.json'];
  17. $adapter = Mockery::mock('League\Flysystem\AdapterInterface');
  18. $adapter->shouldReceive('has')->once()->with('file.json')->andReturn(true);
  19. $adapter->shouldReceive('read')->once()->with('file.json')->andReturn($response);
  20. $adapter->shouldReceive('delete')->once()->with('file.json');
  21. $cache = new Adapter($adapter, 'file.json', 10);
  22. $cache->load();
  23. $this->assertFalse($cache->isComplete('', false));
  24. }
  25. public function testLoadSuccess()
  26. {
  27. $response = ['contents' => json_encode([[], ['' => true], 9876543210]), 'path' => 'file.json'];
  28. $adapter = Mockery::mock('League\Flysystem\AdapterInterface');
  29. $adapter->shouldReceive('has')->once()->with('file.json')->andReturn(true);
  30. $adapter->shouldReceive('read')->once()->with('file.json')->andReturn($response);
  31. $cache = new Adapter($adapter, 'file.json', 10);
  32. $cache->load();
  33. $this->assertTrue($cache->isComplete('', false));
  34. }
  35. public function testSaveExists()
  36. {
  37. $response = json_encode([[], [], null]);
  38. $adapter = Mockery::mock('League\Flysystem\AdapterInterface');
  39. $adapter->shouldReceive('has')->once()->with('file.json')->andReturn(true);
  40. $adapter->shouldReceive('update')->once()->with('file.json', $response, Mockery::any());
  41. $cache = new Adapter($adapter, 'file.json', null);
  42. $cache->save();
  43. }
  44. public function testSaveNew()
  45. {
  46. $response = json_encode([[], [], null]);
  47. $adapter = Mockery::mock('League\Flysystem\AdapterInterface');
  48. $adapter->shouldReceive('has')->once()->with('file.json')->andReturn(false);
  49. $adapter->shouldReceive('write')->once()->with('file.json', $response, Mockery::any());
  50. $cache = new Adapter($adapter, 'file.json', null);
  51. $cache->save();
  52. }
  53. public function testStoreContentsRecursive()
  54. {
  55. $adapter = Mockery::mock('League\Flysystem\AdapterInterface');
  56. $adapter->shouldReceive('has')->once()->with('file.json')->andReturn(false);
  57. $adapter->shouldReceive('write')->once()->with('file.json', Mockery::any(), Mockery::any());
  58. $cache = new Adapter($adapter, 'file.json', null);
  59. $contents = [
  60. ['path' => 'foo/bar', 'dirname' => 'foo'],
  61. ['path' => 'afoo/bang', 'dirname' => 'afoo'],
  62. ];
  63. $cache->storeContents('foo', $contents, true);
  64. $this->assertTrue($cache->isComplete('foo', true));
  65. $this->assertFalse($cache->isComplete('afoo', true));
  66. }
  67. public function testDeleteDir()
  68. {
  69. $cache_data = [
  70. 'foo' => ['path' => 'foo', 'type' => 'dir', 'dirname' => ''],
  71. 'foo/bar' => ['path' => 'foo/bar', 'type' => 'file', 'dirname' => 'foo'],
  72. 'foobaz' => ['path' => 'foobaz', 'type' => 'file', 'dirname' => ''],
  73. ];
  74. $response = [
  75. 'contents' => json_encode([$cache_data, [], null]),
  76. 'path' => 'file.json',
  77. ];
  78. $adapter = Mockery::mock('League\Flysystem\AdapterInterface');
  79. $adapter->shouldReceive('has')->zeroOrMoreTimes()->with('file.json')->andReturn(true);
  80. $adapter->shouldReceive('read')->once()->with('file.json')->andReturn($response);
  81. $adapter->shouldReceive('update')->once()->with('file.json', Mockery::any(), Mockery::any())->andReturn(true);
  82. $cache = new Adapter($adapter, 'file.json', null);
  83. $cache->load();
  84. $cache->deleteDir('foo', true);
  85. $this->assertSame(1, count($cache->listContents('', true)));
  86. }
  87. }