/src/test/php/org/bovigo/vfs/vfsStreamWrapperFileTestCase.php

https://github.com/acoulton/vfsStream · PHP · 335 lines · 189 code · 23 blank · 123 comment · 0 complexity · 002df7be27c74c46c05902b8e8783ae5 MD5 · raw file

  1. <?php
  2. /**
  3. * Test for org::bovigo::vfs::vfsStreamWrapper.
  4. *
  5. * @author Frank Kleine <mikey@bovigo.org>
  6. * @package bovigo_vfs
  7. * @subpackage test
  8. */
  9. require_once 'org/bovigo/vfs/vfsStream.php';
  10. require_once 'PHPUnit/Framework/TestCase.php';
  11. require_once dirname(__FILE__) . '/vfsStreamWrapperBaseTestCase.php';
  12. /**
  13. * Test for org::bovigo::vfs::vfsStreamWrapper.
  14. *
  15. * @package bovigo_vfs
  16. * @subpackage test
  17. */
  18. class vfsStreamWrapperFileTestCase extends vfsStreamWrapperBaseTestCase
  19. {
  20. /**
  21. * assert that file_get_contents() delivers correct file contents
  22. *
  23. * @test
  24. */
  25. public function file_get_contents()
  26. {
  27. $this->assertEquals('baz2', file_get_contents($this->baz2URL));
  28. $this->assertEquals('baz 1', file_get_contents($this->baz1URL));
  29. $this->assertFalse(@file_get_contents($this->barURL));
  30. $this->assertFalse(@file_get_contents($this->fooURL));
  31. }
  32. /**
  33. * @test
  34. * @group permissions
  35. * @group bug_15
  36. */
  37. public function file_get_contentsNonReadableFile()
  38. {
  39. vfsStreamWrapper::register();
  40. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
  41. vfsStream::newFile('new.txt', 0000)->at(vfsStreamWrapper::getRoot())->withContent('content');
  42. $this->assertEquals('', @file_get_contents(vfsStream::url('root/new.txt')));
  43. }
  44. /**
  45. * assert that file_put_contents() delivers correct file contents
  46. *
  47. * @test
  48. */
  49. public function file_put_contentsExistingFile()
  50. {
  51. $this->assertEquals(14, file_put_contents($this->baz2URL, 'baz is not bar'));
  52. $this->assertEquals('baz is not bar', $this->baz2->getContent());
  53. $this->assertEquals(6, file_put_contents($this->baz1URL, 'foobar'));
  54. $this->assertEquals('foobar', $this->baz1->getContent());
  55. $this->assertFalse(@file_put_contents($this->barURL, 'This does not work.'));
  56. $this->assertFalse(@file_put_contents($this->fooURL, 'This does not work, too.'));
  57. }
  58. /**
  59. * @test
  60. * @group permissions
  61. * @group bug_15
  62. */
  63. public function file_put_contentsExistingFileNonWritableDirectory()
  64. {
  65. vfsStreamWrapper::register();
  66. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  67. vfsStream::newFile('new.txt')->at(vfsStreamWrapper::getRoot())->withContent('content');
  68. $this->assertEquals(15, @file_put_contents(vfsStream::url('root/new.txt'), 'This does work.'));
  69. $this->assertEquals('This does work.', file_get_contents(vfsStream::url('root/new.txt')));
  70. }
  71. /**
  72. * @test
  73. * @group permissions
  74. * @group bug_15
  75. */
  76. public function file_put_contentsExistingNonWritableFile()
  77. {
  78. vfsStreamWrapper::register();
  79. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
  80. vfsStream::newFile('new.txt', 0400)->at(vfsStreamWrapper::getRoot())->withContent('content');
  81. $this->assertFalse(@file_put_contents(vfsStream::url('root/new.txt'), 'This does not work.'));
  82. $this->assertEquals('content', file_get_contents(vfsStream::url('root/new.txt')));
  83. }
  84. /**
  85. * assert that file_put_contents() delivers correct file contents
  86. *
  87. * @test
  88. */
  89. public function file_put_contentsNonExistingFile()
  90. {
  91. $this->assertEquals(14, file_put_contents($this->fooURL . '/baznot.bar', 'baz is not bar'));
  92. $this->assertEquals(3, count($this->foo->getChildren()));
  93. $this->assertEquals(14, file_put_contents($this->barURL . '/baznot.bar', 'baz is not bar'));
  94. $this->assertEquals(2, count($this->bar->getChildren()));
  95. }
  96. /**
  97. * @test
  98. * @group permissions
  99. * @group bug_15
  100. */
  101. public function file_put_contentsNonExistingFileNonWritableDirectory()
  102. {
  103. vfsStreamWrapper::register();
  104. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  105. $this->assertFalse(@file_put_contents(vfsStream::url('root/new.txt'), 'This does not work.'));
  106. $this->assertFalse(file_exists(vfsStream::url('root/new.txt')));
  107. }
  108. /**
  109. * using a file pointer should work without any problems
  110. *
  111. * @test
  112. */
  113. public function usingFilePointer()
  114. {
  115. $fp = fopen($this->baz1URL, 'r');
  116. $this->assertEquals(0, ftell($fp));
  117. $this->assertFalse(feof($fp));
  118. $this->assertEquals(0, fseek($fp, 2));
  119. $this->assertEquals(2, ftell($fp));
  120. $this->assertEquals(0, fseek($fp, 1, SEEK_CUR));
  121. $this->assertEquals(3, ftell($fp));
  122. $this->assertEquals(0, fseek($fp, 1, SEEK_END));
  123. $this->assertEquals(6, ftell($fp));
  124. $this->assertTrue(feof($fp));
  125. $this->assertEquals(0, fseek($fp, 2));
  126. $this->assertFalse(feof($fp));
  127. $this->assertEquals(2, ftell($fp));
  128. $this->assertEquals('z', fread($fp, 1));
  129. $this->assertEquals(3, ftell($fp));
  130. $this->assertEquals(' 1', fread($fp, 8092));
  131. $this->assertEquals(5, ftell($fp));
  132. $this->assertTrue(fclose($fp));
  133. }
  134. /**
  135. * assert is_file() returns correct result
  136. *
  137. * @test
  138. */
  139. public function is_file()
  140. {
  141. $this->assertFalse(is_file($this->fooURL));
  142. $this->assertFalse(is_file($this->barURL));
  143. $this->assertTrue(is_file($this->baz1URL));
  144. $this->assertTrue(is_file($this->baz2URL));
  145. $this->assertFalse(is_file($this->fooURL . '/another'));
  146. $this->assertFalse(is_file(vfsStream::url('another')));
  147. }
  148. /**
  149. * @test
  150. * @group issue7
  151. * @group issue13
  152. */
  153. public function issue13CanNotOverwriteFiles()
  154. {
  155. $vfsFile = vfsStream::url('foo/overwrite.txt');
  156. file_put_contents($vfsFile, 'test');
  157. file_put_contents($vfsFile, 'd');
  158. $this->assertEquals('d', file_get_contents($vfsFile));
  159. }
  160. /**
  161. * @test
  162. * @group issue7
  163. * @group issue13
  164. */
  165. public function appendContentIfOpenedWithModeA()
  166. {
  167. $vfsFile = vfsStream::url('foo/overwrite.txt');
  168. file_put_contents($vfsFile, 'test');
  169. $fp = fopen($vfsFile, 'ab');
  170. fwrite($fp, 'd');
  171. fclose($fp);
  172. $this->assertEquals('testd', file_get_contents($vfsFile));
  173. }
  174. /**
  175. * @test
  176. * @group issue7
  177. * @group issue13
  178. */
  179. public function canOverwriteNonExistingFileWithModeX()
  180. {
  181. $vfsFile = vfsStream::url('foo/overwrite.txt');
  182. $fp = fopen($vfsFile, 'xb');
  183. fwrite($fp, 'test');
  184. fclose($fp);
  185. $this->assertEquals('test', file_get_contents($vfsFile));
  186. }
  187. /**
  188. * @test
  189. * @group issue7
  190. * @group issue13
  191. */
  192. public function canNotOverwriteExistingFileWithModeX()
  193. {
  194. $vfsFile = vfsStream::url('foo/overwrite.txt');
  195. file_put_contents($vfsFile, 'test');
  196. $this->assertFalse(@fopen($vfsFile, 'xb'));
  197. $this->assertEquals('test', file_get_contents($vfsFile));
  198. }
  199. /**
  200. * @test
  201. * @group issue7
  202. * @group issue13
  203. */
  204. public function canNotOpenNonExistingFileReadonly()
  205. {
  206. $this->assertFalse(@fopen(vfsStream::url('foo/doesNotExist.txt'), 'rb'));
  207. }
  208. /**
  209. * @test
  210. * @group issue7
  211. * @group issue13
  212. */
  213. public function canNotOpenNonExistingFileReadAndWrite()
  214. {
  215. $this->assertFalse(@fopen(vfsStream::url('foo/doesNotExist.txt'), 'rb+'));
  216. }
  217. /**
  218. * @test
  219. * @group issue7
  220. * @group issue13
  221. */
  222. public function canNotOpenWithIllegalMode()
  223. {
  224. $this->assertFalse(@fopen($this->baz2URL, 'invalid'));
  225. }
  226. /**
  227. * @test
  228. * @group issue7
  229. * @group issue13
  230. */
  231. public function canNotWriteToReadOnlyFile()
  232. {
  233. $fp = fopen($this->baz2URL, 'rb');
  234. $this->assertEquals('baz2', fread($fp, 4096));
  235. $this->assertEquals(0, fwrite($fp, 'foo'));
  236. fclose($fp);
  237. $this->assertEquals('baz2', file_get_contents($this->baz2URL));
  238. }
  239. /**
  240. * @test
  241. * @group issue7
  242. * @group issue13
  243. */
  244. public function canNotReadFromWriteOnlyFileWithModeW()
  245. {
  246. $fp = fopen($this->baz2URL, 'wb');
  247. $this->assertEquals('', fread($fp, 4096));
  248. $this->assertEquals(3, fwrite($fp, 'foo'));
  249. fseek($fp, 0);
  250. $this->assertEquals('', fread($fp, 4096));
  251. fclose($fp);
  252. $this->assertEquals('foo', file_get_contents($this->baz2URL));
  253. }
  254. /**
  255. * @test
  256. * @group issue7
  257. * @group issue13
  258. */
  259. public function canNotReadFromWriteOnlyFileWithModeA()
  260. {
  261. $fp = fopen($this->baz2URL, 'ab');
  262. $this->assertEquals('', fread($fp, 4096));
  263. $this->assertEquals(3, fwrite($fp, 'foo'));
  264. fseek($fp, 0);
  265. $this->assertEquals('', fread($fp, 4096));
  266. fclose($fp);
  267. $this->assertEquals('baz2foo', file_get_contents($this->baz2URL));
  268. }
  269. /**
  270. * @test
  271. * @group issue7
  272. * @group issue13
  273. */
  274. public function canNotReadFromWriteOnlyFileWithModeX()
  275. {
  276. $vfsFile = vfsStream::url('foo/modeXtest.txt');
  277. $fp = fopen($vfsFile, 'xb');
  278. $this->assertEquals('', fread($fp, 4096));
  279. $this->assertEquals(3, fwrite($fp, 'foo'));
  280. fseek($fp, 0);
  281. $this->assertEquals('', fread($fp, 4096));
  282. fclose($fp);
  283. $this->assertEquals('foo', file_get_contents($vfsFile));
  284. }
  285. /**
  286. * @test
  287. * @group permissions
  288. * @group bug_15
  289. */
  290. public function canNotRemoveFileWithoutWritePermissions()
  291. {
  292. vfsStreamWrapper::register();
  293. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
  294. vfsStream::newFile('new.txt', 0000)->at(vfsStreamWrapper::getRoot());
  295. $this->assertFalse(unlink(vfsStream::url('root/new.txt')));
  296. $this->assertTrue(file_exists(vfsStream::url('root/new.txt')));
  297. }
  298. /**
  299. * @test
  300. * @group permissions
  301. * @group bug_15
  302. */
  303. public function canNotRemoveFileFromDirectoryWithoutWritePermissions()
  304. {
  305. vfsStreamWrapper::register();
  306. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  307. vfsStream::newFile('new.txt')->at(vfsStreamWrapper::getRoot());
  308. $this->assertFalse(unlink(vfsStream::url('root/new.txt')));
  309. $this->assertTrue(file_exists(vfsStream::url('root/new.txt')));
  310. }
  311. }
  312. ?>