CacheSpec.php 4.9 KB

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