PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/mikey179/vfsstream/src/test/php/org/bovigo/vfs/vfsStreamWrapperFileTestCase.php

https://gitlab.com/Japang-Jawara/jawara-penilaian
PHP | 457 lines | 263 code | 34 blank | 160 comment | 0 complexity | c47fe51031fd5df448027f9ceb669bf4 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of vfsStream.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @package org\bovigo\vfs
  9. */
  10. namespace org\bovigo\vfs;
  11. require_once __DIR__ . '/vfsStreamWrapperBaseTestCase.php';
  12. /**
  13. * Test for org\bovigo\vfs\vfsStreamWrapper.
  14. */
  15. class vfsStreamWrapperFileTestCase extends vfsStreamWrapperBaseTestCase
  16. {
  17. /**
  18. * assert that file_get_contents() delivers correct file contents
  19. *
  20. * @test
  21. */
  22. public function file_get_contents()
  23. {
  24. $this->assertEquals('baz2', file_get_contents($this->baz2URL));
  25. $this->assertEquals('baz 1', file_get_contents($this->baz1URL));
  26. $this->assertFalse(@file_get_contents($this->barURL));
  27. $this->assertFalse(@file_get_contents($this->fooURL));
  28. }
  29. /**
  30. * @test
  31. * @group permissions
  32. * @group bug_15
  33. */
  34. public function file_get_contentsNonReadableFile()
  35. {
  36. vfsStreamWrapper::register();
  37. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
  38. vfsStream::newFile('new.txt', 0000)->at(vfsStreamWrapper::getRoot())->withContent('content');
  39. $this->assertEquals('', @file_get_contents(vfsStream::url('root/new.txt')));
  40. }
  41. /**
  42. * assert that file_put_contents() delivers correct file contents
  43. *
  44. * @test
  45. */
  46. public function file_put_contentsExistingFile()
  47. {
  48. $this->assertEquals(14, file_put_contents($this->baz2URL, 'baz is not bar'));
  49. $this->assertEquals('baz is not bar', $this->baz2->getContent());
  50. $this->assertEquals(6, file_put_contents($this->baz1URL, 'foobar'));
  51. $this->assertEquals('foobar', $this->baz1->getContent());
  52. $this->assertFalse(@file_put_contents($this->barURL, 'This does not work.'));
  53. $this->assertFalse(@file_put_contents($this->fooURL, 'This does not work, too.'));
  54. }
  55. /**
  56. * @test
  57. * @group permissions
  58. * @group bug_15
  59. */
  60. public function file_put_contentsExistingFileNonWritableDirectory()
  61. {
  62. vfsStreamWrapper::register();
  63. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  64. vfsStream::newFile('new.txt')->at(vfsStreamWrapper::getRoot())->withContent('content');
  65. $this->assertEquals(15, @file_put_contents(vfsStream::url('root/new.txt'), 'This does work.'));
  66. $this->assertEquals('This does work.', file_get_contents(vfsStream::url('root/new.txt')));
  67. }
  68. /**
  69. * @test
  70. * @group permissions
  71. * @group bug_15
  72. */
  73. public function file_put_contentsExistingNonWritableFile()
  74. {
  75. vfsStreamWrapper::register();
  76. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
  77. vfsStream::newFile('new.txt', 0400)->at(vfsStreamWrapper::getRoot())->withContent('content');
  78. $this->assertFalse(@file_put_contents(vfsStream::url('root/new.txt'), 'This does not work.'));
  79. $this->assertEquals('content', file_get_contents(vfsStream::url('root/new.txt')));
  80. }
  81. /**
  82. * assert that file_put_contents() delivers correct file contents
  83. *
  84. * @test
  85. */
  86. public function file_put_contentsNonExistingFile()
  87. {
  88. $this->assertEquals(14, file_put_contents($this->fooURL . '/baznot.bar', 'baz is not bar'));
  89. $this->assertEquals(3, count($this->foo->getChildren()));
  90. $this->assertEquals(14, file_put_contents($this->barURL . '/baznot.bar', 'baz is not bar'));
  91. $this->assertEquals(2, count($this->bar->getChildren()));
  92. }
  93. /**
  94. * @test
  95. * @group permissions
  96. * @group bug_15
  97. */
  98. public function file_put_contentsNonExistingFileNonWritableDirectory()
  99. {
  100. vfsStreamWrapper::register();
  101. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  102. $this->assertFalse(@file_put_contents(vfsStream::url('root/new.txt'), 'This does not work.'));
  103. $this->assertFalse(file_exists(vfsStream::url('root/new.txt')));
  104. }
  105. /**
  106. * using a file pointer should work without any problems
  107. *
  108. * @test
  109. */
  110. public function usingFilePointer()
  111. {
  112. $fp = fopen($this->baz1URL, 'r');
  113. $this->assertEquals(0, ftell($fp));
  114. $this->assertFalse(feof($fp));
  115. $this->assertEquals(0, fseek($fp, 2));
  116. $this->assertEquals(2, ftell($fp));
  117. $this->assertEquals(0, fseek($fp, 1, SEEK_CUR));
  118. $this->assertEquals(3, ftell($fp));
  119. $this->assertEquals(0, fseek($fp, 1, SEEK_END));
  120. $this->assertEquals(6, ftell($fp));
  121. $this->assertTrue(feof($fp));
  122. $this->assertEquals(0, fseek($fp, 2));
  123. $this->assertFalse(feof($fp));
  124. $this->assertEquals(2, ftell($fp));
  125. $this->assertEquals('z', fread($fp, 1));
  126. $this->assertEquals(3, ftell($fp));
  127. $this->assertEquals(' 1', fread($fp, 8092));
  128. $this->assertEquals(5, ftell($fp));
  129. $this->assertTrue(fclose($fp));
  130. }
  131. /**
  132. * assert is_file() returns correct result
  133. *
  134. * @test
  135. */
  136. public function is_file()
  137. {
  138. $this->assertFalse(is_file($this->fooURL));
  139. $this->assertFalse(is_file($this->barURL));
  140. $this->assertTrue(is_file($this->baz1URL));
  141. $this->assertTrue(is_file($this->baz2URL));
  142. $this->assertFalse(is_file($this->fooURL . '/another'));
  143. $this->assertFalse(is_file(vfsStream::url('another')));
  144. }
  145. /**
  146. * @test
  147. * @group issue7
  148. * @group issue13
  149. */
  150. public function issue13CanNotOverwriteFiles()
  151. {
  152. $vfsFile = vfsStream::url('foo/overwrite.txt');
  153. file_put_contents($vfsFile, 'test');
  154. file_put_contents($vfsFile, 'd');
  155. $this->assertEquals('d', file_get_contents($vfsFile));
  156. }
  157. /**
  158. * @test
  159. * @group issue7
  160. * @group issue13
  161. */
  162. public function appendContentIfOpenedWithModeA()
  163. {
  164. $vfsFile = vfsStream::url('foo/overwrite.txt');
  165. file_put_contents($vfsFile, 'test');
  166. $fp = fopen($vfsFile, 'ab');
  167. fwrite($fp, 'd');
  168. fclose($fp);
  169. $this->assertEquals('testd', file_get_contents($vfsFile));
  170. }
  171. /**
  172. * @test
  173. * @group issue7
  174. * @group issue13
  175. */
  176. public function canOverwriteNonExistingFileWithModeX()
  177. {
  178. $vfsFile = vfsStream::url('foo/overwrite.txt');
  179. $fp = fopen($vfsFile, 'xb');
  180. fwrite($fp, 'test');
  181. fclose($fp);
  182. $this->assertEquals('test', file_get_contents($vfsFile));
  183. }
  184. /**
  185. * @test
  186. * @group issue7
  187. * @group issue13
  188. */
  189. public function canNotOverwriteExistingFileWithModeX()
  190. {
  191. $vfsFile = vfsStream::url('foo/overwrite.txt');
  192. file_put_contents($vfsFile, 'test');
  193. $this->assertFalse(@fopen($vfsFile, 'xb'));
  194. $this->assertEquals('test', file_get_contents($vfsFile));
  195. }
  196. /**
  197. * @test
  198. * @group issue7
  199. * @group issue13
  200. */
  201. public function canNotOpenNonExistingFileReadonly()
  202. {
  203. $this->assertFalse(@fopen(vfsStream::url('foo/doesNotExist.txt'), 'rb'));
  204. }
  205. /**
  206. * @test
  207. * @group issue7
  208. * @group issue13
  209. */
  210. public function canNotOpenNonExistingFileReadAndWrite()
  211. {
  212. $this->assertFalse(@fopen(vfsStream::url('foo/doesNotExist.txt'), 'rb+'));
  213. }
  214. /**
  215. * @test
  216. * @group issue7
  217. * @group issue13
  218. */
  219. public function canNotOpenWithIllegalMode()
  220. {
  221. $this->assertFalse(@fopen($this->baz2URL, 'invalid'));
  222. }
  223. /**
  224. * @test
  225. * @group issue7
  226. * @group issue13
  227. */
  228. public function canNotWriteToReadOnlyFile()
  229. {
  230. $fp = fopen($this->baz2URL, 'rb');
  231. $this->assertEquals('baz2', fread($fp, 4096));
  232. $this->assertEquals(0, fwrite($fp, 'foo'));
  233. fclose($fp);
  234. $this->assertEquals('baz2', file_get_contents($this->baz2URL));
  235. }
  236. /**
  237. * @test
  238. * @group issue7
  239. * @group issue13
  240. */
  241. public function canNotReadFromWriteOnlyFileWithModeW()
  242. {
  243. $fp = fopen($this->baz2URL, 'wb');
  244. $this->assertEquals('', fread($fp, 4096));
  245. $this->assertEquals(3, fwrite($fp, 'foo'));
  246. fseek($fp, 0);
  247. $this->assertEquals('', fread($fp, 4096));
  248. fclose($fp);
  249. $this->assertEquals('foo', file_get_contents($this->baz2URL));
  250. }
  251. /**
  252. * @test
  253. * @group issue7
  254. * @group issue13
  255. */
  256. public function canNotReadFromWriteOnlyFileWithModeA()
  257. {
  258. $fp = fopen($this->baz2URL, 'ab');
  259. $this->assertEquals('', fread($fp, 4096));
  260. $this->assertEquals(3, fwrite($fp, 'foo'));
  261. fseek($fp, 0);
  262. $this->assertEquals('', fread($fp, 4096));
  263. fclose($fp);
  264. $this->assertEquals('baz2foo', file_get_contents($this->baz2URL));
  265. }
  266. /**
  267. * @test
  268. * @group issue7
  269. * @group issue13
  270. */
  271. public function canNotReadFromWriteOnlyFileWithModeX()
  272. {
  273. $vfsFile = vfsStream::url('foo/modeXtest.txt');
  274. $fp = fopen($vfsFile, 'xb');
  275. $this->assertEquals('', fread($fp, 4096));
  276. $this->assertEquals(3, fwrite($fp, 'foo'));
  277. fseek($fp, 0);
  278. $this->assertEquals('', fread($fp, 4096));
  279. fclose($fp);
  280. $this->assertEquals('foo', file_get_contents($vfsFile));
  281. }
  282. /**
  283. * @test
  284. * @group permissions
  285. * @group bug_15
  286. */
  287. public function canNotRemoveFileFromDirectoryWithoutWritePermissions()
  288. {
  289. vfsStreamWrapper::register();
  290. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  291. vfsStream::newFile('new.txt')->at(vfsStreamWrapper::getRoot());
  292. $this->assertFalse(unlink(vfsStream::url('root/new.txt')));
  293. $this->assertTrue(file_exists(vfsStream::url('root/new.txt')));
  294. }
  295. /**
  296. * @test
  297. * @group issue_30
  298. */
  299. public function truncatesFileWhenOpenedWithModeW()
  300. {
  301. $vfsFile = vfsStream::url('foo/overwrite.txt');
  302. file_put_contents($vfsFile, 'test');
  303. $fp = fopen($vfsFile, 'wb');
  304. $this->assertEquals('', file_get_contents($vfsFile));
  305. fclose($fp);
  306. }
  307. /**
  308. * @test
  309. * @group issue_30
  310. */
  311. public function createsNonExistingFileWhenOpenedWithModeC()
  312. {
  313. $vfsFile = vfsStream::url('foo/tobecreated.txt');
  314. $fp = fopen($vfsFile, 'cb');
  315. fwrite($fp, 'some content');
  316. $this->assertTrue($this->foo->hasChild('tobecreated.txt'));
  317. fclose($fp);
  318. $this->assertEquals('some content', file_get_contents($vfsFile));
  319. }
  320. /**
  321. * @test
  322. * @group issue_30
  323. */
  324. public function createsNonExistingFileWhenOpenedWithModeCplus()
  325. {
  326. $vfsFile = vfsStream::url('foo/tobecreated.txt');
  327. $fp = fopen($vfsFile, 'cb+');
  328. fwrite($fp, 'some content');
  329. $this->assertTrue($this->foo->hasChild('tobecreated.txt'));
  330. fclose($fp);
  331. $this->assertEquals('some content', file_get_contents($vfsFile));
  332. }
  333. /**
  334. * @test
  335. * @group issue_30
  336. */
  337. public function doesNotTruncateFileWhenOpenedWithModeC()
  338. {
  339. $vfsFile = vfsStream::url('foo/overwrite.txt');
  340. file_put_contents($vfsFile, 'test');
  341. $fp = fopen($vfsFile, 'cb');
  342. $this->assertEquals('test', file_get_contents($vfsFile));
  343. fclose($fp);
  344. }
  345. /**
  346. * @test
  347. * @group issue_30
  348. */
  349. public function setsPointerToStartWhenOpenedWithModeC()
  350. {
  351. $vfsFile = vfsStream::url('foo/overwrite.txt');
  352. file_put_contents($vfsFile, 'test');
  353. $fp = fopen($vfsFile, 'cb');
  354. $this->assertEquals(0, ftell($fp));
  355. fclose($fp);
  356. }
  357. /**
  358. * @test
  359. * @group issue_30
  360. */
  361. public function doesNotTruncateFileWhenOpenedWithModeCplus()
  362. {
  363. $vfsFile = vfsStream::url('foo/overwrite.txt');
  364. file_put_contents($vfsFile, 'test');
  365. $fp = fopen($vfsFile, 'cb+');
  366. $this->assertEquals('test', file_get_contents($vfsFile));
  367. fclose($fp);
  368. }
  369. /**
  370. * @test
  371. * @group issue_30
  372. */
  373. public function setsPointerToStartWhenOpenedWithModeCplus()
  374. {
  375. $vfsFile = vfsStream::url('foo/overwrite.txt');
  376. file_put_contents($vfsFile, 'test');
  377. $fp = fopen($vfsFile, 'cb+');
  378. $this->assertEquals(0, ftell($fp));
  379. fclose($fp);
  380. }
  381. /**
  382. * @test
  383. */
  384. public function cannotOpenExistingNonwritableFileWithModeA()
  385. {
  386. $this->baz1->chmod(0400);
  387. $this->assertFalse(@fopen($this->baz1URL, 'a'));
  388. }
  389. /**
  390. * @test
  391. */
  392. public function cannotOpenExistingNonwritableFileWithModeW()
  393. {
  394. $this->baz1->chmod(0400);
  395. $this->assertFalse(@fopen($this->baz1URL, 'w'));
  396. }
  397. /**
  398. * @test
  399. */
  400. public function cannotOpenNonReadableFileWithModeR()
  401. {
  402. $this->baz1->chmod(0);
  403. $this->assertFalse(@fopen($this->baz1URL, 'r'));
  404. }
  405. /**
  406. * @test
  407. */
  408. public function cannotRenameToNonWritableDir()
  409. {
  410. $this->bar->chmod(0);
  411. $this->assertFalse(@rename($this->baz2URL, vfsStream::url('foo/bar/baz3')));
  412. }
  413. /**
  414. * @test
  415. * @group issue_38
  416. */
  417. public function cannotReadFileFromNonReadableDir()
  418. {
  419. $this->markTestSkipped("Issue #38.");
  420. $this->bar->chmod(0);
  421. $this->assertFalse(@file_get_contents($this->baz1URL));
  422. }
  423. }