LocalSpec.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. use org\bovigo\vfs\vfsStream;
  4. use PhpSpec\ObjectBehavior;
  5. class LocalSpec extends ObjectBehavior
  6. {
  7. function let()
  8. {
  9. vfsStream::setup('test');
  10. vfsStream::copyFromFileSystem(__DIR__.'/MockFilesystem');
  11. $this->beConstructedWith(vfsStream::url('test'));
  12. }
  13. function it_is_adapter()
  14. {
  15. $this->shouldHaveType('Gaufrette\Adapter');
  16. }
  17. function it_is_checksum_calculator()
  18. {
  19. $this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
  20. }
  21. function it_is_a_mime_type_provider()
  22. {
  23. $this->shouldHaveType('Gaufrette\Adapter\MimeTypeProvider');
  24. }
  25. function it_gets_the_file_mime_type()
  26. {
  27. $this->mimeType('filename')->shouldReturn('text/plain');
  28. }
  29. function it_is_stream_factory()
  30. {
  31. $this->shouldHaveType('Gaufrette\Adapter\StreamFactory');
  32. }
  33. function it_reads_file()
  34. {
  35. $this->read('filename')->shouldReturn("content\n");
  36. }
  37. function it_writes_file()
  38. {
  39. $this->write('filename', 'some content')->shouldReturn(12);
  40. }
  41. function it_renames_file()
  42. {
  43. $this->rename('filename', 'aaa/filename2')->shouldReturn(true);
  44. }
  45. function it_checks_if_file_exists()
  46. {
  47. $this->exists('filename')->shouldReturn(true);
  48. $this->exists('filename1')->shouldReturn(false);
  49. }
  50. function it_fetches_keys()
  51. {
  52. $expectedKeys = array('filename', 'dir', 'dir/file');
  53. sort($expectedKeys);
  54. $this->keys()->shouldReturn($expectedKeys);
  55. }
  56. function it_fetches_mtime()
  57. {
  58. $mtime = filemtime(vfsStream::url('test/filename'));
  59. $this->mtime('filename')->shouldReturn($mtime);
  60. }
  61. function it_deletes_file()
  62. {
  63. $this->delete('filename')->shouldReturn(true);
  64. $this->delete('filename1')->shouldReturn(false);
  65. }
  66. function it_checks_if_given_key_is_directory()
  67. {
  68. $this->isDirectory('dir')->shouldReturn(true);
  69. $this->isDirectory('filename')->shouldReturn(false);
  70. }
  71. function it_creates_local_stream()
  72. {
  73. $this->createStream('filename')->shouldReturnAnInstanceOf('Gaufrette\Stream\Local');
  74. }
  75. function it_does_not_allow_to_read_path_above_main_file_directory()
  76. {
  77. $this
  78. ->shouldThrow(new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', vfsStream::url('filename'))))
  79. ->duringRead('../filename')
  80. ;
  81. $this
  82. ->shouldThrow(new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', vfsStream::url('filename'))))
  83. ->duringExists('../filename')
  84. ;
  85. }
  86. function it_fails_when_directory_does_not_exists()
  87. {
  88. $this->beConstructedWith(vfsStream::url('other'));
  89. $this
  90. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  91. ->duringRead('filename')
  92. ;
  93. $this
  94. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  95. ->duringWrite('filename', 'some content')
  96. ;
  97. $this
  98. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  99. ->duringRename('filename', 'otherFilename')
  100. ;
  101. $this
  102. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  103. ->duringExists('filename')
  104. ;
  105. $this
  106. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  107. ->duringKeys()
  108. ;
  109. $this
  110. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  111. ->duringMtime('filename')
  112. ;
  113. $this
  114. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  115. ->duringDelete('filename')
  116. ;
  117. $this
  118. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  119. ->duringIsDirectory('filename')
  120. ;
  121. $this
  122. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  123. ->duringCreateStream('filename')
  124. ;
  125. $this
  126. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  127. ->duringChecksum('filename')
  128. ;
  129. $this
  130. ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
  131. ->duringMimeType('filename')
  132. ;
  133. }
  134. function it_creates_directory_when_does_not_exists()
  135. {
  136. $this->beConstructedWith(vfsStream::url('test/other'), true);
  137. $this->exists('/')->shouldReturn(true);
  138. }
  139. }