FileSpec.php 5.4 KB

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