CacheSpec.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. use Gaufrette\Adapter;
  4. use PhpSpec\ObjectBehavior;
  5. class CacheSpec extends ObjectBehavior
  6. {
  7. /**
  8. * @param \Gaufrette\Adapter $source
  9. * @param \Gaufrette\Adapter $cache
  10. */
  11. function let(Adapter $source, Adapter $cache)
  12. {
  13. $this->beConstructedWith($source, $cache);
  14. }
  15. function it_is_adapter()
  16. {
  17. $this->shouldHaveType('Gaufrette\Adapter');
  18. }
  19. function it_supports_metadata()
  20. {
  21. $this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
  22. }
  23. /**
  24. * @param \spec\Gaufrette\Adapter\CacheTestExtendedAdapter $extendedSource
  25. * @param \spec\Gaufrette\Adapter\CacheTestExtendedAdapter $extendedCache
  26. */
  27. function it_handles_metadata_when_cached_adapters_supports_metadata(
  28. CacheTestExtendedAdapter $extendedSource,
  29. CacheTestExtendedAdapter $extendedCache
  30. )
  31. {
  32. $extendedSource->setMetadata('filename', array('metadata'))->shouldBeCalled();
  33. $extendedCache->setMetadata('filename', array('metadata'))->shouldBeCalled();
  34. $extendedSource->getMetadata('filename')->shouldBeCalled()->willReturn(array('someMetadata'));
  35. $this->beConstructedWith($extendedSource, $extendedCache);
  36. $this->setMetadata('filename', array('metadata'));
  37. $this->getMetadata('filename')->shouldReturn(array('someMetadata'));
  38. }
  39. /**
  40. * @param \Gaufrette\Adapter $source
  41. */
  42. function it_delegates_is_directory_check_to_source(Adapter $source)
  43. {
  44. $source->isDirectory('filename')->shouldBeCalled()->willReturn(true);
  45. $this->isDirectory('filename')->shouldReturn(true);
  46. }
  47. /**
  48. * @param \Gaufrette\Adapter $source
  49. * @param \Gaufrette\Adapter $cache
  50. */
  51. function it_reads_from_cache_adapter(Adapter $source, Adapter $cache)
  52. {
  53. $source->read('filename')->shouldNotBeCalled();
  54. $cache->read('filename')->shouldBeCalled()->willReturn('some content');
  55. $source->mtime('filename')->willReturn(strtotime('2010-10-11'));
  56. $cache->mtime('filename')->willReturn(strtotime('2010-10-12'));
  57. $cache->exists('filename')->willReturn(true);
  58. $this->read('filename')->shouldReturn('some content');
  59. }
  60. /**
  61. * @param \Gaufrette\Adapter $source
  62. * @param \Gaufrette\Adapter $cache
  63. */
  64. function it_update_cache_adapter_when_source_file_is_modified(Adapter $source, Adapter $cache)
  65. {
  66. $source->read('filename')->shouldBeCalled()->willReturn('some other content');
  67. $cache->read('filename')->shouldNotBeCalled();
  68. $cache->write('filename', 'some other content')->shouldBeCalled();
  69. $source->mtime('filename')->willReturn(strtotime('2010-10-11'));
  70. $cache->mtime('filename')->willReturn(strtotime('2010-10-10'));
  71. $cache->exists('filename')->willReturn(true);
  72. $this->read('filename')->shouldReturn('some other content');
  73. }
  74. /**
  75. * @param \Gaufrette\Adapter $source
  76. * @param \Gaufrette\Adapter $cache
  77. */
  78. function it_rename_file_in_source_and_cache(Adapter $source, Adapter $cache)
  79. {
  80. $source->rename('filename', 'filename2')->shouldBeCalled()->willReturn(true);
  81. $cache->rename('filename', 'filename2')->shouldBeCalled()->willReturn(true);
  82. $this->rename('filename', 'filename2')->shouldReturn(true);
  83. }
  84. /**
  85. * @param \Gaufrette\Adapter $source
  86. * @param \Gaufrette\Adapter $cache
  87. */
  88. function it_writes_file_to_source_and_cache(Adapter $source, Adapter $cache)
  89. {
  90. $source->write('filename', 'some content')->shouldBeCalled()->willReturn(12);
  91. $cache->write('filename', 'some content')->shouldBeCalled()->willReturn(12);
  92. $this->write('filename', 'some content')->shouldReturn(12);
  93. }
  94. /**
  95. * @param \Gaufrette\Adapter $source
  96. * @param \Gaufrette\Adapter $cache
  97. */
  98. function it_deletes_file_from_source_and_cache(Adapter $source, Adapter $cache)
  99. {
  100. $source->delete('filename')->shouldBeCalled()->willReturn(true);
  101. $cache->delete('filename')->shouldBeCalled()->willReturn(true);
  102. $this->delete('filename')->shouldReturn(true);
  103. }
  104. /**
  105. * @param \Gaufrette\Adapter $source
  106. */
  107. function it_check_if_exists_in_source(Adapter $source)
  108. {
  109. $source->exists('filename')->shouldBeCalled()->willReturn(true);
  110. $this->exists('filename')->shouldReturn(true);
  111. }
  112. /**
  113. * @param \Gaufrette\Adapter $source
  114. */
  115. function it_get_mtime_from_source(Adapter $source)
  116. {
  117. $source->mtime('filename')->shouldBeCalled()->willReturn(1234);
  118. $this->mtime('filename')->shouldReturn(1234);
  119. }
  120. /**
  121. * @param \Gaufrette\Adapter $source
  122. */
  123. function it_get_keys_from_source(Adapter $source)
  124. {
  125. $source->keys()->willReturn(array('filename2', 'filename1', 'filename'));
  126. $this->keys()->shouldReturn(array('filename', 'filename1', 'filename2'));
  127. }
  128. }
  129. interface CacheTestExtendedAdapter extends \Gaufrette\Adapter,
  130. \Gaufrette\Adapter\ChecksumCalculator,
  131. \Gaufrette\Adapter\MetadataSupporter
  132. {
  133. }