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

https://github.com/wiltave/vfsStream · PHP · 571 lines · 358 code · 38 blank · 175 comment · 0 complexity · ecf74fa2cc710430e3967cb23efbdf2b MD5 · raw file

  1. <?php
  2. /**
  3. * Test for org::bovigo::vfs::vfsStreamWrapper.
  4. *
  5. * @package bovigo_vfs
  6. * @subpackage test
  7. */
  8. require_once 'org/bovigo/vfs/vfsStream.php';
  9. require_once 'PHPUnit/Framework.php';
  10. require_once dirname(__FILE__) . '/vfsStreamWrapperBaseTestCase.php';
  11. /**
  12. * Test for org::bovigo::vfs::vfsStreamWrapper.
  13. *
  14. * @package bovigo_vfs
  15. * @subpackage test
  16. */
  17. class vfsStreamWrapperTestCase extends vfsStreamWrapperBaseTestCase
  18. {
  19. /**
  20. * ensure that a call to vfsStreamWrapper::register() resets the stream
  21. *
  22. * Implemented after a request by David Zülke.
  23. *
  24. * @test
  25. */
  26. public function resetByRegister()
  27. {
  28. $this->assertSame($this->foo, vfsStreamWrapper::getRoot());
  29. vfsStreamWrapper::register();
  30. $this->assertNull(vfsStreamWrapper::getRoot());
  31. }
  32. /**
  33. * @test
  34. * @since 0.11.0
  35. */
  36. public function setRootReturnsRoot()
  37. {
  38. vfsStreamWrapper::register();
  39. $root = vfsStream::newDirectory('root');
  40. $this->assertSame($root, vfsStreamWrapper::setRoot($root));
  41. }
  42. /**
  43. * assure that filesize is returned correct
  44. *
  45. * @test
  46. */
  47. public function filesize()
  48. {
  49. $this->assertEquals(0, filesize($this->fooURL));
  50. $this->assertEquals(0, filesize($this->fooURL . '/.'));
  51. $this->assertEquals(0, filesize($this->barURL));
  52. $this->assertEquals(0, filesize($this->barURL . '/.'));
  53. $this->assertEquals(4, filesize($this->baz2URL));
  54. $this->assertEquals(5, filesize($this->baz1URL));
  55. }
  56. /**
  57. * assert that file_exists() delivers correct result
  58. *
  59. * @test
  60. */
  61. public function file_exists()
  62. {
  63. $this->assertTrue(file_exists($this->fooURL));
  64. $this->assertTrue(file_exists($this->fooURL . '/.'));
  65. $this->assertTrue(file_exists($this->barURL));
  66. $this->assertTrue(file_exists($this->barURL . '/.'));
  67. $this->assertTrue(file_exists($this->baz1URL));
  68. $this->assertTrue(file_exists($this->baz2URL));
  69. $this->assertFalse(file_exists($this->fooURL . '/another'));
  70. $this->assertFalse(file_exists(vfsStream::url('another')));
  71. }
  72. /**
  73. * assert that filemtime() delivers correct result
  74. *
  75. * @test
  76. */
  77. public function filemtime()
  78. {
  79. $this->assertEquals(100, filemtime($this->fooURL));
  80. $this->assertEquals(100, filemtime($this->fooURL . '/.'));
  81. $this->assertEquals(200, filemtime($this->barURL));
  82. $this->assertEquals(200, filemtime($this->barURL . '/.'));
  83. $this->assertEquals(300, filemtime($this->baz1URL));
  84. $this->assertEquals(400, filemtime($this->baz2URL));
  85. }
  86. /**
  87. * assert that unlink() removes files and directories
  88. *
  89. * @test
  90. */
  91. public function unlink()
  92. {
  93. $this->assertTrue(unlink($this->baz2URL));
  94. $this->assertFalse(file_exists($this->baz2URL)); // make sure statcache was cleared
  95. $this->assertEquals(array($this->bar), $this->foo->getChildren());
  96. $this->assertTrue(unlink($this->barURL));
  97. $this->assertFalse(file_exists($this->barURL)); // make sure statcache was cleared
  98. $this->assertEquals(array(), $this->foo->getChildren());
  99. $this->assertFalse(unlink($this->fooURL . '/another'));
  100. $this->assertFalse(unlink(vfsStream::url('another')));
  101. $this->assertEquals(array(), $this->foo->getChildren());
  102. $this->assertTrue(unlink($this->fooURL . '/.'));
  103. $this->assertFalse(file_exists($this->fooURL)); // make sure statcache was cleared
  104. $this->assertNull(vfsStreamWrapper::getRoot());
  105. }
  106. /**
  107. * assert dirname() returns correct directory name
  108. *
  109. * @test
  110. */
  111. public function dirname()
  112. {
  113. $this->assertEquals($this->fooURL, dirname($this->barURL));
  114. $this->assertEquals($this->barURL, dirname($this->baz1URL));
  115. # returns "vfs:" instead of "."
  116. # however this seems not to be fixable because dirname() does not
  117. # call the stream wrapper
  118. #$this->assertEquals(dirname(vfsStream::url('doesNotExist')), '.');
  119. }
  120. /**
  121. * assert basename() returns correct file name
  122. *
  123. * @test
  124. */
  125. public function basename()
  126. {
  127. $this->assertEquals('bar', basename($this->barURL));
  128. $this->assertEquals('baz1', basename($this->baz1URL));
  129. $this->assertEquals('doesNotExist', basename(vfsStream::url('doesNotExist')));
  130. }
  131. /**
  132. * assert is_readable() works correct
  133. *
  134. * @test
  135. */
  136. public function is_readable()
  137. {
  138. $this->assertTrue(is_readable($this->fooURL));
  139. $this->assertTrue(is_readable($this->fooURL . '/.'));
  140. $this->assertTrue(is_readable($this->barURL));
  141. $this->assertTrue(is_readable($this->barURL . '/.'));
  142. $this->assertTrue(is_readable($this->baz1URL));
  143. $this->assertTrue(is_readable($this->baz2URL));
  144. $this->assertFalse(is_readable($this->fooURL . '/another'));
  145. $this->assertFalse(is_readable(vfsStream::url('another')));
  146. $this->foo->chmod(0222);
  147. $this->assertFalse(is_readable($this->fooURL));
  148. $this->baz1->chmod(0222);
  149. $this->assertFalse(is_readable($this->baz1URL));
  150. }
  151. /**
  152. * assert is_writable() works correct
  153. *
  154. * @test
  155. */
  156. public function is_writable()
  157. {
  158. $this->assertTrue(is_writable($this->fooURL));
  159. $this->assertTrue(is_writable($this->fooURL . '/.'));
  160. $this->assertTrue(is_writable($this->barURL));
  161. $this->assertTrue(is_writable($this->barURL . '/.'));
  162. $this->assertTrue(is_writable($this->baz1URL));
  163. $this->assertTrue(is_writable($this->baz2URL));
  164. $this->assertFalse(is_writable($this->fooURL . '/another'));
  165. $this->assertFalse(is_writable(vfsStream::url('another')));
  166. $this->foo->chmod(0444);
  167. $this->assertFalse(is_writable($this->fooURL));
  168. $this->baz1->chmod(0444);
  169. $this->assertFalse(is_writable($this->baz1URL));
  170. }
  171. /**
  172. * assert is_executable() works correct
  173. *
  174. * @test
  175. */
  176. public function is_executable()
  177. {
  178. $this->assertFalse(is_executable($this->baz1URL));
  179. $this->baz1->chmod(0766);
  180. $this->assertTrue(is_executable($this->baz1URL));
  181. $this->assertFalse(is_executable($this->baz2URL));
  182. }
  183. /**
  184. * assert is_executable() works correct
  185. *
  186. * @test
  187. */
  188. public function directoriesAndNonExistingFilesAreNeverExecutable()
  189. {
  190. $this->assertFalse(is_executable($this->fooURL));
  191. $this->assertFalse(is_executable($this->fooURL . '/.'));
  192. $this->assertFalse(is_executable($this->barURL));
  193. $this->assertFalse(is_executable($this->barURL . '/.'));
  194. $this->assertFalse(is_executable($this->fooURL . '/another'));
  195. $this->assertFalse(is_executable(vfsStream::url('another')));
  196. }
  197. /**
  198. * file permissions
  199. *
  200. * @test
  201. * @group permissions
  202. */
  203. public function chmod()
  204. {
  205. $this->assertEquals(40777, decoct(fileperms($this->fooURL)));
  206. $this->assertEquals(40777, decoct(fileperms($this->fooURL . '/.')));
  207. $this->assertEquals(40777, decoct(fileperms($this->barURL)));
  208. $this->assertEquals(40777, decoct(fileperms($this->barURL . '/.')));
  209. $this->assertEquals(100666, decoct(fileperms($this->baz1URL)));
  210. $this->assertEquals(100666, decoct(fileperms($this->baz2URL)));
  211. $this->foo->chmod(0755);
  212. $this->bar->chmod(0700);
  213. $this->baz1->chmod(0644);
  214. $this->baz2->chmod(0600);
  215. $this->assertEquals(40755, decoct(fileperms($this->fooURL)));
  216. $this->assertEquals(40755, decoct(fileperms($this->fooURL . '/.')));
  217. $this->assertEquals(40700, decoct(fileperms($this->barURL)));
  218. $this->assertEquals(40700, decoct(fileperms($this->barURL . '/.')));
  219. $this->assertEquals(100644, decoct(fileperms($this->baz1URL)));
  220. $this->assertEquals(100600, decoct(fileperms($this->baz2URL)));
  221. }
  222. /**
  223. * chmod() does not work with vfsStream URLs
  224. *
  225. * @test
  226. */
  227. public function chmodDoesNotWorkOnVfsStreamUrls()
  228. {
  229. // silence down chmod() because of error message for invalid url
  230. $this->assertFalse(@chmod($this->fooURL, 0755));
  231. $this->assertFalse(@chmod($this->barURL, 0711));
  232. $this->assertFalse(@chmod($this->baz1URL, 0644));
  233. $this->assertFalse(@chmod($this->baz2URL, 0664));
  234. $this->assertEquals(40777, decoct(fileperms($this->fooURL)));
  235. $this->assertEquals(40777, decoct(fileperms($this->barURL)));
  236. $this->assertEquals(100666, decoct(fileperms($this->baz1URL)));
  237. $this->assertEquals(100666, decoct(fileperms($this->baz2URL)));
  238. }
  239. /**
  240. * file owner
  241. *
  242. * @test
  243. * @group permissions
  244. */
  245. public function chown()
  246. {
  247. $this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->fooURL));
  248. $this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->fooURL . '/.'));
  249. $this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->barURL));
  250. $this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->barURL . '/.'));
  251. $this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->baz1URL));
  252. $this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->baz2URL));
  253. $this->foo->chown(vfsStream::OWNER_USER_1);
  254. $this->bar->chown(vfsStream::OWNER_USER_1);
  255. $this->baz1->chown(vfsStream::OWNER_USER_2);
  256. $this->baz2->chown(vfsStream::OWNER_USER_2);
  257. $this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->fooURL));
  258. $this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->fooURL . '/.'));
  259. $this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->barURL));
  260. $this->assertEquals(vfsStream::OWNER_USER_1, fileowner($this->barURL . '/.'));
  261. $this->assertEquals(vfsStream::OWNER_USER_2, fileowner($this->baz1URL));
  262. $this->assertEquals(vfsStream::OWNER_USER_2, fileowner($this->baz2URL));
  263. }
  264. /**
  265. * chown() does not work with vfsStream URLs
  266. *
  267. * @test
  268. */
  269. public function chownDoesNotWorkOnVfsStreamUrls()
  270. {
  271. // silence down chown() because of error message for invalid url
  272. $this->assertFalse(@chown($this->fooURL, vfsStream::OWNER_USER_2));
  273. $this->assertEquals(vfsStream::getCurrentUser(), fileowner($this->fooURL));
  274. }
  275. /**
  276. * file group owner
  277. *
  278. * @test
  279. * @group permissions
  280. */
  281. public function chgrp()
  282. {
  283. $this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->fooURL));
  284. $this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->fooURL . '/.'));
  285. $this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->barURL));
  286. $this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->barURL . '/.'));
  287. $this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->baz1URL));
  288. $this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->baz2URL));
  289. $this->foo->chgrp(vfsStream::GROUP_USER_1);
  290. $this->bar->chgrp(vfsStream::GROUP_USER_1);
  291. $this->baz1->chgrp(vfsStream::GROUP_USER_2);
  292. $this->baz2->chgrp(vfsStream::GROUP_USER_2);
  293. $this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->fooURL));
  294. $this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->fooURL . '/.'));
  295. $this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->barURL));
  296. $this->assertEquals(vfsStream::GROUP_USER_1, filegroup($this->barURL . '/.'));
  297. $this->assertEquals(vfsStream::GROUP_USER_2, filegroup($this->baz1URL));
  298. $this->assertEquals(vfsStream::GROUP_USER_2, filegroup($this->baz2URL));
  299. }
  300. /**
  301. * chgrp() does not work with vfsStream URLs
  302. *
  303. * @test
  304. */
  305. public function chgrpDoesNotWorkOnVfsStreamUrls()
  306. {
  307. // silence down chgrp() because of error message for invalid url
  308. $this->assertFalse(@chgrp($this->fooURL, vfsStream::GROUP_USER_2));
  309. $this->assertEquals(vfsStream::getCurrentGroup(), filegroup($this->fooURL));
  310. }
  311. /**
  312. * @test
  313. * @author Benoit Aubuchon
  314. */
  315. public function renameDirectory()
  316. {
  317. // move foo/bar to foo/baz3
  318. $baz3URL = vfsStream::url('foo/baz3');
  319. $this->assertTrue(rename($this->barURL, $baz3URL));
  320. $this->assertFileExists($baz3URL);
  321. $this->assertFileNotExists($this->barURL);
  322. }
  323. /**
  324. * @test
  325. */
  326. public function renameDirectoryWithDots()
  327. {
  328. // move foo/bar to foo/baz3
  329. $baz3URL = vfsStream::url('foo/baz3');
  330. $this->assertTrue(rename($this->barURL . '/.', $baz3URL));
  331. $this->assertFileExists($baz3URL);
  332. $this->assertFileNotExists($this->barURL);
  333. }
  334. /**
  335. * @test
  336. * @group issue_9
  337. * @since 0.9.0
  338. */
  339. public function renameDirectoryWithDotsInTarget()
  340. {
  341. // move foo/bar to foo/baz3
  342. $baz3URL = vfsStream::url('foo/../foo/baz3/.');
  343. $this->assertTrue(rename($this->barURL . '/.', $baz3URL));
  344. $this->assertFileExists($baz3URL);
  345. $this->assertFileNotExists($this->barURL);
  346. }
  347. /**
  348. * @test
  349. * @author Benoit Aubuchon
  350. */
  351. public function renameDirectoryOverwritingExistingFile()
  352. {
  353. // move foo/bar to foo/baz2
  354. $this->assertTrue(rename($this->barURL, $this->baz2URL));
  355. $this->assertFileExists(vfsStream::url('foo/baz2/baz1'));
  356. $this->assertFileNotExists($this->barURL);
  357. }
  358. /**
  359. * @test
  360. * @expectedException PHPUnit_Framework_Error
  361. */
  362. public function renameFileIntoFile()
  363. {
  364. // foo/baz2 is a file, so it can not be turned into a directory
  365. $baz3URL = vfsStream::url('foo/baz2/baz3');
  366. $this->assertTrue(rename($this->baz1URL, $baz3URL));
  367. $this->assertFileExists($baz3URL);
  368. $this->assertFileNotExists($this->baz1URL);
  369. }
  370. /**
  371. * @test
  372. * @author Benoit Aubuchon
  373. */
  374. public function renameFileToDirectory()
  375. {
  376. // move foo/bar/baz1 to foo/baz3
  377. $baz3URL = vfsStream::url('foo/baz3');
  378. $this->assertTrue(rename($this->baz1URL, $baz3URL));
  379. $this->assertFileExists($this->barURL);
  380. $this->assertFileExists($baz3URL);
  381. $this->assertFileNotExists($this->baz1URL);
  382. }
  383. /**
  384. * assert that trying to rename from a non existing file trigger a warning
  385. *
  386. * @expectedException PHPUnit_Framework_Error
  387. * @test
  388. */
  389. public function renameOnSourceFileNotFound()
  390. {
  391. rename(vfsStream::url('notfound'), $this->baz1URL);
  392. }
  393. /**
  394. * assert that trying to rename to a directory that is not found trigger a warning
  395. * @expectedException PHPUnit_Framework_Error
  396. * @test
  397. */
  398. public function renameOnDestinationDirectoryFileNotFound()
  399. {
  400. rename($this->baz1URL, vfsStream::url('foo/notfound/file2'));
  401. }
  402. /**
  403. * stat() and fstat() should return the same result
  404. *
  405. * @test
  406. */
  407. public function statAndFstatReturnSameResult()
  408. {
  409. $fp = fopen($this->baz2URL, 'r');
  410. $this->assertEquals(stat($this->baz2URL),
  411. fstat($fp)
  412. );
  413. fclose($fp);
  414. }
  415. /**
  416. * stat() returns full data
  417. *
  418. * @test
  419. */
  420. public function statReturnsFullDataForFiles()
  421. {
  422. $this->assertEquals(array(0 => 0,
  423. 1 => 0,
  424. 2 => 0100666,
  425. 3 => 0,
  426. 4 => vfsStream::getCurrentUser(),
  427. 5 => vfsStream::getCurrentGroup(),
  428. 6 => 0,
  429. 7 => 4,
  430. 8 => 400,
  431. 9 => 400,
  432. 10 => 400,
  433. 11 => -1,
  434. 12 => -1,
  435. 'dev' => 0,
  436. 'ino' => 0,
  437. 'mode' => 0100666,
  438. 'nlink' => 0,
  439. 'uid' => vfsStream::getCurrentUser(),
  440. 'gid' => vfsStream::getCurrentGroup(),
  441. 'rdev' => 0,
  442. 'size' => 4,
  443. 'atime' => 400,
  444. 'mtime' => 400,
  445. 'ctime' => 400,
  446. 'blksize' => -1,
  447. 'blocks' => -1
  448. ),
  449. stat($this->baz2URL)
  450. );
  451. }
  452. /**
  453. * @test
  454. */
  455. public function statReturnsFullDataForDirectories()
  456. {
  457. $this->assertEquals(array(0 => 0,
  458. 1 => 0,
  459. 2 => 0040777,
  460. 3 => 0,
  461. 4 => vfsStream::getCurrentUser(),
  462. 5 => vfsStream::getCurrentGroup(),
  463. 6 => 0,
  464. 7 => 0,
  465. 8 => 100,
  466. 9 => 100,
  467. 10 => 100,
  468. 11 => -1,
  469. 12 => -1,
  470. 'dev' => 0,
  471. 'ino' => 0,
  472. 'mode' => 0040777,
  473. 'nlink' => 0,
  474. 'uid' => vfsStream::getCurrentUser(),
  475. 'gid' => vfsStream::getCurrentGroup(),
  476. 'rdev' => 0,
  477. 'size' => 0,
  478. 'atime' => 100,
  479. 'mtime' => 100,
  480. 'ctime' => 100,
  481. 'blksize' => -1,
  482. 'blocks' => -1
  483. ),
  484. stat($this->fooURL)
  485. );
  486. }
  487. /**
  488. * @test
  489. */
  490. public function statReturnsFullDataForDirectoriesWithDot()
  491. {
  492. $this->assertEquals(array(0 => 0,
  493. 1 => 0,
  494. 2 => 0040777,
  495. 3 => 0,
  496. 4 => vfsStream::getCurrentUser(),
  497. 5 => vfsStream::getCurrentGroup(),
  498. 6 => 0,
  499. 7 => 0,
  500. 8 => 100,
  501. 9 => 100,
  502. 10 => 100,
  503. 11 => -1,
  504. 12 => -1,
  505. 'dev' => 0,
  506. 'ino' => 0,
  507. 'mode' => 0040777,
  508. 'nlink' => 0,
  509. 'uid' => vfsStream::getCurrentUser(),
  510. 'gid' => vfsStream::getCurrentGroup(),
  511. 'rdev' => 0,
  512. 'size' => 0,
  513. 'atime' => 100,
  514. 'mtime' => 100,
  515. 'ctime' => 100,
  516. 'blksize' => -1,
  517. 'blocks' => -1
  518. ),
  519. stat($this->fooURL . '/.')
  520. );
  521. }
  522. /**
  523. * @test
  524. * @expectedException PHPUnit_Framework_Error
  525. */
  526. public function openFileWithoutDirectory()
  527. {
  528. vfsStreamWrapper::register();
  529. $this->assertFalse(file_get_contents(vfsStream::url('file.txt')));
  530. }
  531. }
  532. ?>