FileSpec.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace spec\Gaufrette;
  3. use PhpSpec\ObjectBehavior;
  4. class FileSpec extends ObjectBehavior
  5. {
  6. /**
  7. * @param \Gaufrette\Filesystem $filesystem
  8. */
  9. function let($filesystem)
  10. {
  11. $this->beConstructedWith('filename', $filesystem);
  12. }
  13. function it_is_initializable()
  14. {
  15. $this->shouldHaveType('Gaufrette\File');
  16. }
  17. function it_gives_access_to_key()
  18. {
  19. $this->getKey()->shouldReturn('filename');
  20. }
  21. /**
  22. * @param \Gaufrette\Filesystem $filesystem
  23. */
  24. function it_gets_content($filesystem)
  25. {
  26. $filesystem->read('filename')->shouldBeCalled()->willReturn('Some content');
  27. $this->getContent()->shouldReturn('Some content');
  28. }
  29. /**
  30. * @param \Gaufrette\Filesystem $filesystem
  31. */
  32. function it_gets_mtime($filesystem)
  33. {
  34. $filesystem->mtime('filename')->shouldBeCalled()->willReturn(1358797854);
  35. $this->getMtime()->shouldReturn(1358797854);
  36. }
  37. /**
  38. * @param \Gaufrette\Filesystem $filesystem
  39. * @param \spec\Gaufrette\MetadataAdapter $adapter
  40. */
  41. function it_pass_metadata_when_write_content($filesystem, $adapter)
  42. {
  43. $metadata = array('id' => '123');
  44. $adapter->setMetadata('filename', $metadata)->shouldBeCalled();
  45. $filesystem->write('filename', 'some content', true)->willReturn(12);
  46. $filesystem->getAdapter()->willReturn($adapter);
  47. $this->setContent('some content', $metadata);
  48. }
  49. /**
  50. * @param \Gaufrette\Filesystem $filesystem
  51. * @param \spec\Gaufrette\MetadataAdapter $adapter
  52. */
  53. function it_pass_metadata_when_read_content($filesystem, $adapter)
  54. {
  55. $metadata = array('id' => '123');
  56. $adapter->setMetadata('filename', $metadata)->shouldBeCalled();
  57. $filesystem->read('filename')->willReturn('some content');
  58. $filesystem->getAdapter()->willReturn($adapter);
  59. $this->getContent($metadata);
  60. }
  61. /**
  62. * @param \Gaufrette\Filesystem $filesystem
  63. * @param \spec\Gaufrette\MetadataAdapter $adapter
  64. */
  65. function it_pass_metadata_when_delete_content($filesystem, $adapter)
  66. {
  67. $metadata = array('id' => '123');
  68. $adapter->setMetadata('filename', $metadata)->shouldBeCalled();
  69. $filesystem->delete('filename')->willReturn(true);
  70. $filesystem->getAdapter()->willReturn($adapter);
  71. $this->delete($metadata);
  72. }
  73. /**
  74. * @param \Gaufrette\Filesystem $filesystem
  75. * @param \spec\Gaufrette\MetadataAdapter $adapter
  76. */
  77. function it_sets_content_of_file($filesystem, $adapter)
  78. {
  79. $adapter->setMetadata('filename', array())->shouldNotBeCalled();
  80. $filesystem->getAdapter()->willReturn($adapter);
  81. $filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21);
  82. $this->setContent('some content')->shouldReturn(21);
  83. $this->getContent('filename')->shouldReturn('some content');
  84. }
  85. function it_sets_key_as_name_by_default()
  86. {
  87. $this->getName()->shouldReturn('filename');
  88. }
  89. function it_sets_name()
  90. {
  91. $this->setName('name');
  92. $this->getName()->shouldReturn('name');
  93. }
  94. /**
  95. * @param \Gaufrette\Filesystem $filesystem
  96. */
  97. function it_sets_size_for_new_file($filesystem)
  98. {
  99. $filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21);
  100. $this->setContent('some content');
  101. $this->getSize()->shouldReturn(21);
  102. }
  103. /**
  104. * @param \Gaufrette\Filesystem $filesystem
  105. */
  106. function it_calculates_size_from_content($filesystem)
  107. {
  108. $filesystem->read('filename')->shouldBeCalled()->willReturn('some content');
  109. $this->getSize()->shouldReturn(12);
  110. }
  111. /**
  112. * @param \Gaufrette\Filesystem $filesystem
  113. */
  114. function it_allows_to_set_size($filesystem)
  115. {
  116. $filesystem->read('filename')->shouldNotBeCalled();
  117. $this->setSize(21);
  118. $this->getSize()->shouldReturn(21);
  119. }
  120. /**
  121. * @param \Gaufrette\Filesystem $filesystem
  122. */
  123. function it_gets_zero_size_when_file_not_found($filesystem)
  124. {
  125. $filesystem->read('filename')->willThrow(new \Gaufrette\Exception\FileNotFound('filename'));
  126. $this->getSize()->shouldReturn(0);
  127. }
  128. /**
  129. * @param \Gaufrette\Filesystem $filesystem
  130. */
  131. function it_check_if_file_with_key_exists_in_filesystem($filesystem)
  132. {
  133. $filesystem->has('filename')->willReturn(true);
  134. $this->exists()->shouldReturn(true);
  135. $filesystem->has('filename')->willReturn(false);
  136. $this->exists()->shouldReturn(false);
  137. }
  138. /**
  139. * @param \Gaufrette\Filesystem $filesystem
  140. */
  141. function it_deletes_file_from_filesystem($filesystem)
  142. {
  143. $filesystem->delete('filename')->shouldBeCalled()->willReturn(true);
  144. $this->delete()->shouldReturn(true);
  145. }
  146. }
  147. interface MetadataAdapter extends \Gaufrette\Adapter,
  148. \Gaufrette\Adapter\MetadataSupporter
  149. {}