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

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