LocalSpec.php 4.6 KB

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