PageRenderTime 67ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/TestCase/Filesystem/FolderTest.php

http://github.com/cakephp/cakephp
PHP | 1430 lines | 949 code | 181 blank | 300 comment | 3 complexity | 94ccea192527bf298a1a45b1b0f94eac MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * FolderTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  14. * @link https://cakephp.org CakePHP(tm) Project
  15. * @since 1.2.0
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. namespace Cake\Test\TestCase\Filesystem;
  19. use Cake\Filesystem\File;
  20. use Cake\Filesystem\Folder;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * FolderTest class
  24. */
  25. class FolderTest extends TestCase
  26. {
  27. /**
  28. * setUp clearstatcache() to flush file descriptors.
  29. */
  30. public function setUp(): void
  31. {
  32. parent::setUp();
  33. clearstatcache();
  34. }
  35. /**
  36. * Remove TMP/tests directory to its original state.
  37. */
  38. public function tearDown(): void
  39. {
  40. parent::tearDown();
  41. $cleaner = function ($dir) use (&$cleaner): void {
  42. $files = array_diff(scandir($dir), ['.', '..']);
  43. foreach ($files as $file) {
  44. $path = $dir . DS . $file;
  45. if (is_dir($path)) {
  46. $cleaner($path);
  47. } else {
  48. unlink($path);
  49. }
  50. }
  51. rmdir($dir);
  52. };
  53. if (file_exists(TMP . 'tests')) {
  54. $cleaner(TMP . 'tests');
  55. }
  56. parent::tearDown();
  57. }
  58. /**
  59. * testBasic method
  60. */
  61. public function testBasic(): void
  62. {
  63. $path = __DIR__;
  64. $Folder = new Folder($path);
  65. $result = $Folder->pwd();
  66. $this->assertSame($path, $result);
  67. $result = Folder::addPathElement($path, 'test');
  68. $expected = $path . DS . 'test';
  69. $this->assertSame($expected, $result);
  70. $result = $Folder->cd(ROOT);
  71. $expected = ROOT;
  72. $this->assertSame($expected, $result);
  73. $result = $Folder->cd(ROOT . DS . 'nonexistent');
  74. $this->assertFalse($result);
  75. }
  76. /**
  77. * testInPath method
  78. */
  79. public function testInPath(): void
  80. {
  81. // "/tests/test_app/"
  82. $basePath = TEST_APP;
  83. $Base = new Folder($basePath);
  84. $result = $Base->pwd();
  85. $this->assertSame($basePath, $result);
  86. // is "/" in "/tests/test_app/"
  87. $result = $Base->inPath(realpath(DS), true);
  88. $this->assertFalse($result);
  89. // is "/tests/test_app/" in "/tests/test_app/"
  90. $result = $Base->inPath($basePath, true);
  91. $this->assertTrue($result);
  92. // is "/tests/test_app" in "/tests/test_app/"
  93. $result = $Base->inPath(mb_substr($basePath, 0, -1), true);
  94. $this->assertTrue($result);
  95. // is "/tests/test_app/sub" in "/tests/test_app/"
  96. $result = $Base->inPath($basePath . 'sub', true);
  97. $this->assertTrue($result);
  98. // is "/tests" in "/tests/test_app/"
  99. $result = $Base->inPath(dirname($basePath), true);
  100. $this->assertFalse($result);
  101. // is "/tests/other/(...)tests/test_app" in "/tests/test_app/"
  102. $result = $Base->inPath(TMP . 'tests' . DS . 'other' . DS . $basePath, true);
  103. $this->assertFalse($result);
  104. // is "/tests/test_app/" in "/"
  105. $result = $Base->inPath(realpath(DS));
  106. $this->assertTrue($result);
  107. // is "/tests/test_app/" in "/tests/test_app/"
  108. $result = $Base->inPath($basePath);
  109. $this->assertTrue($result);
  110. // is "/tests/test_app/" in "/tests/test_app"
  111. $result = $Base->inPath(mb_substr($basePath, 0, -1));
  112. $this->assertTrue($result);
  113. // is "/tests/test_app/" in "/tests"
  114. $result = $Base->inPath(dirname($basePath));
  115. $this->assertTrue($result);
  116. // is "/tests/test_app/" in "/tests/test_app/sub"
  117. $result = $Base->inPath($basePath . 'sub');
  118. $this->assertFalse($result);
  119. // is "/other/tests/test_app/" in "/tests/test_app/"
  120. $VirtualBase = new Folder();
  121. $VirtualBase->path = '/other/tests/test_app';
  122. $result = $VirtualBase->inPath('/tests/test_app/');
  123. $this->assertFalse($result);
  124. }
  125. /**
  126. * Data provider for the testInPathInvalidPathArgument test
  127. *
  128. * @return array
  129. */
  130. public function inPathInvalidPathArgumentDataProvider(): array
  131. {
  132. return [
  133. [''],
  134. ['relative/path/'],
  135. ['unknown://stream-wrapper'],
  136. ];
  137. }
  138. /**
  139. * @dataProvider inPathInvalidPathArgumentDataProvider
  140. * @param string $path
  141. */
  142. public function testInPathInvalidPathArgument($path): void
  143. {
  144. $this->expectException(\InvalidArgumentException::class);
  145. $this->expectExceptionMessage('The $path argument is expected to be an absolute path.');
  146. $Folder = new Folder();
  147. $Folder->inPath($path);
  148. }
  149. /**
  150. * test creation of single and multiple paths.
  151. */
  152. public function testCreation(): void
  153. {
  154. $Folder = new Folder(TMP . 'tests');
  155. $result = $Folder->create(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  156. $this->assertTrue($result);
  157. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
  158. rmdir(TMP . 'tests' . DS . 'first' . DS . 'second');
  159. rmdir(TMP . 'tests' . DS . 'first');
  160. $Folder = new Folder(TMP . 'tests');
  161. $result = $Folder->create(TMP . 'tests' . DS . 'first');
  162. $this->assertTrue($result);
  163. }
  164. /**
  165. * test that creation of folders with trailing ds works
  166. */
  167. public function testCreateWithTrailingDs(): void
  168. {
  169. $Folder = new Folder(TMP . 'tests');
  170. $path = TMP . 'tests' . DS . 'trailing' . DS . 'dir' . DS;
  171. $result = $Folder->create($path);
  172. $this->assertTrue($result);
  173. $this->assertDirectoryExists($path, 'Folder was not made');
  174. $Folder = new Folder(TMP . 'tests' . DS . 'trailing');
  175. $this->assertTrue($Folder->delete());
  176. }
  177. /**
  178. * Test that relative paths to create() are added to cwd.
  179. */
  180. public function testCreateRelative(): void
  181. {
  182. $folder = new Folder(TMP);
  183. $path = TMP . 'tests' . DS . 'relative-test';
  184. $result = $folder->create('tests' . DS . 'relative-test');
  185. $this->assertTrue($result, 'should create');
  186. $this->assertDirectoryExists($path, 'Folder was not made');
  187. $folder = new Folder($path);
  188. $folder->delete();
  189. }
  190. /**
  191. * test recursive directory create failure.
  192. */
  193. public function testRecursiveCreateFailure(): void
  194. {
  195. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  196. $path = TMP . 'tests/one';
  197. mkdir($path, 0777, true);
  198. chmod($path, 0444);
  199. try {
  200. $Folder = new Folder($path);
  201. $result = $Folder->create($path . DS . 'two/three');
  202. $this->assertFalse($result);
  203. } catch (\Exception $e) {
  204. $this->assertInstanceOf('PHPUnit\Framework\Error\Error', $e);
  205. }
  206. chmod($path, 0777);
  207. rmdir($path);
  208. }
  209. /**
  210. * testOperations method
  211. */
  212. public function testOperations(): void
  213. {
  214. $path = ROOT . DS . 'templates';
  215. $Folder = new Folder($path);
  216. $result = $Folder->pwd();
  217. $this->assertSame($path, $result);
  218. $new = TMP . 'tests' . DS . 'test_folder_new';
  219. $result = $Folder->create($new);
  220. $this->assertTrue($result);
  221. $copy = TMP . 'tests' . DS . 'test_folder_copy';
  222. $result = $Folder->copy($copy);
  223. $this->assertTrue($result);
  224. $copy = TMP . 'tests' . DS . 'test_folder_copy';
  225. $result = $Folder->copy($copy);
  226. $this->assertTrue($result);
  227. $copy = TMP . 'tests' . DS . 'test_folder_copy';
  228. $result = $Folder->chmod($copy, 0755, false);
  229. $this->assertTrue($result);
  230. $result = $Folder->cd($copy);
  231. $this->assertTrue((bool)$result);
  232. $mv = TMP . 'tests' . DS . 'test_folder_mv';
  233. $result = $Folder->move($mv);
  234. $this->assertTrue($result);
  235. $mv = TMP . 'tests' . DS . 'test_folder_mv_2';
  236. $result = $Folder->move($mv);
  237. $this->assertTrue($result);
  238. $result = $Folder->delete($new);
  239. $this->assertTrue($result);
  240. $result = $Folder->delete($mv);
  241. $this->assertTrue($result);
  242. $result = $Folder->delete($mv);
  243. $this->assertTrue($result);
  244. $new = CONFIG . 'acl.ini';
  245. $result = $Folder->create($new);
  246. $this->assertFalse($result);
  247. $expected = $new . ' is a file';
  248. $result = $Folder->errors();
  249. $this->assertSame($expected, $result[0]);
  250. $new = TMP . 'tests' . DS . 'test_folder_new';
  251. $result = $Folder->create($new);
  252. $this->assertTrue($result);
  253. $result = $Folder->cd($new);
  254. $this->assertTrue((bool)$result);
  255. $result = $Folder->delete();
  256. $this->assertTrue($result);
  257. $Folder = new Folder('nonexistent');
  258. $result = $Folder->pwd();
  259. $this->assertNull($result);
  260. }
  261. /**
  262. * testChmod method
  263. */
  264. public function testChmod(): void
  265. {
  266. $this->skipIf(DS === '\\', 'Folder permissions tests not supported on Windows.');
  267. $path = TMP . 'tests/';
  268. $Folder = new Folder($path);
  269. $subdir = 'test_folder_new';
  270. $new = $path . $subdir;
  271. $this->assertTrue($Folder->create($new));
  272. $this->assertTrue($Folder->create($new . DS . 'test1'));
  273. $this->assertTrue($Folder->create($new . DS . 'test2'));
  274. $filePath = $new . DS . 'test1.php';
  275. $File = new File($filePath);
  276. $this->assertTrue($File->create());
  277. $filePath = $new . DS . 'skip_me.php';
  278. $File = new File($filePath);
  279. $this->assertTrue($File->create());
  280. $this->assertTrue($Folder->chmod($new, 0755, true));
  281. $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
  282. $this->assertSame('0755', $perms);
  283. $this->assertTrue($Folder->chmod($new, 0744, true, ['skip_me.php', 'test2']));
  284. $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
  285. $this->assertSame('0755', $perms);
  286. $perms = substr(sprintf('%o', fileperms($new . DS . 'test1')), -4);
  287. $this->assertSame('0744', $perms);
  288. }
  289. /**
  290. * testRealPathForWebroot method
  291. */
  292. public function testRealPathForWebroot(): void
  293. {
  294. $Folder = new Folder('files' . DS);
  295. $this->assertEquals(realpath('files' . DS), $Folder->path);
  296. }
  297. /**
  298. * testZeroAsDirectory method
  299. */
  300. public function testZeroAsDirectory(): void
  301. {
  302. $path = TMP . 'tests';
  303. $Folder = new Folder($path, true);
  304. $new = $path . '/0';
  305. $this->assertTrue($Folder->create($new));
  306. $result = $Folder->read(true, true);
  307. $this->assertContains('0', $result[0]);
  308. $result = $Folder->read(true, ['logs']);
  309. $this->assertContains('0', $result[0]);
  310. $result = $Folder->delete($new);
  311. $this->assertTrue($result);
  312. }
  313. /**
  314. * test Adding path elements to a path
  315. */
  316. public function testAddPathElement(): void
  317. {
  318. $expected = DS . 'some' . DS . 'dir' . DS . 'another_path';
  319. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
  320. $this->assertSame($expected, $result);
  321. $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
  322. $this->assertSame($expected, $result);
  323. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', ['another_path']);
  324. $this->assertSame($expected, $result);
  325. $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, ['another_path']);
  326. $this->assertSame($expected, $result);
  327. $expected = DS . 'some' . DS . 'dir' . DS . 'another_path' . DS . 'and' . DS . 'another';
  328. $result = Folder::addPathElement(DS . 'some' . DS . 'dir', ['another_path', 'and', 'another']);
  329. $this->assertSame($expected, $result);
  330. }
  331. /**
  332. * testFolderRead method
  333. */
  334. public function testFolderRead(): void
  335. {
  336. $Folder = new Folder(CAKE);
  337. $result = $Folder->read(true, true);
  338. $this->assertContains('Core', $result[0]);
  339. $this->assertContains('Cache', $result[0]);
  340. $Folder = new Folder(TMP . 'nonexistent');
  341. $expected = [[], []];
  342. $result = $Folder->read(true, true);
  343. $this->assertEquals($expected, $result);
  344. }
  345. /**
  346. * testFolderReadWithHiddenFiles method
  347. */
  348. public function testFolderReadWithHiddenFiles(): void
  349. {
  350. $this->skipIf(!is_writable(TMP), 'Cant test Folder::read with hidden files unless the tmp folder is writable.');
  351. $path = TMP . 'tests' . DS;
  352. $Folder = new Folder($path . 'folder_tree_hidden', true, 0777);
  353. mkdir($Folder->path . DS . '.svn');
  354. mkdir($Folder->path . DS . 'some_folder');
  355. touch($Folder->path . DS . 'not_hidden.txt');
  356. touch($Folder->path . DS . '.hidden.txt');
  357. $expected = [
  358. ['some_folder'],
  359. ['not_hidden.txt'],
  360. ];
  361. $result = $Folder->read(true, true);
  362. $this->assertEquals($expected, $result);
  363. $expected = [
  364. [
  365. '.svn',
  366. 'some_folder',
  367. ],
  368. [
  369. '.hidden.txt',
  370. 'not_hidden.txt',
  371. ],
  372. ];
  373. $result = $Folder->read(true);
  374. $this->assertEquals($expected, $result);
  375. }
  376. /**
  377. * testFolderSubdirectories method
  378. */
  379. public function testFolderSubdirectories(): void
  380. {
  381. $path = CAKE . 'Http';
  382. $folder = new Folder($path);
  383. $expected = [
  384. $path . DS . 'Client',
  385. $path . DS . 'Cookie',
  386. $path . DS . 'Exception',
  387. $path . DS . 'Middleware',
  388. $path . DS . 'Session',
  389. ];
  390. $result = $folder->subdirectories();
  391. $this->assertSame([], array_diff($expected, $result));
  392. $result = $folder->subdirectories($path);
  393. $this->assertSame([], array_diff($expected, $result));
  394. $expected = [
  395. 'Client',
  396. 'Cookie',
  397. 'Exception',
  398. 'Middleware',
  399. 'Session',
  400. ];
  401. $result = $folder->subdirectories(null, false);
  402. $this->assertSame([], array_diff($expected, $result));
  403. $result = $folder->subdirectories($path, false);
  404. $this->assertSame([], array_diff($expected, $result));
  405. $expected = [];
  406. $result = $folder->subdirectories('NonExistentPath');
  407. $this->assertSame([], array_diff($expected, $result));
  408. $result = $folder->subdirectories($path . DS . 'Exception');
  409. $this->assertSame([], array_diff($expected, $result));
  410. }
  411. /**
  412. * testFolderTree method
  413. */
  414. public function testFolderTree(): void
  415. {
  416. $Folder = new Folder();
  417. $expected = [
  418. [
  419. CORE_PATH . 'config',
  420. ],
  421. [
  422. CORE_PATH . 'config' . DS . 'config.php',
  423. ],
  424. ];
  425. $result = $Folder->tree(CORE_PATH . 'config', false);
  426. $this->assertSame([], array_diff($expected[0], $result[0]));
  427. $this->assertSame([], array_diff($result[0], $expected[0]));
  428. $result = $Folder->tree(CORE_PATH . 'config', false, 'dir');
  429. $this->assertSame([], array_diff($expected[0], $result));
  430. $this->assertSame([], array_diff($expected[0], $result));
  431. $result = $Folder->tree(CORE_PATH . 'config', false, 'files');
  432. $this->assertSame([], array_diff($expected[1], $result));
  433. $this->assertSame([], array_diff($expected[1], $result));
  434. }
  435. /**
  436. * testFolderTreeWithHiddenFiles method
  437. */
  438. public function testFolderTreeWithHiddenFiles(): void
  439. {
  440. $this->skipIf(!is_writable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
  441. $path = TMP . 'tests' . DS;
  442. $Folder = new Folder($path . 'folder_tree_hidden', true, 0777);
  443. mkdir($Folder->path . DS . '.svn', 0777, true);
  444. touch($Folder->path . DS . '.svn/InHiddenFolder.php');
  445. mkdir($Folder->path . DS . '.svn/inhiddenfolder');
  446. touch($Folder->path . DS . '.svn/inhiddenfolder/NestedInHiddenFolder.php');
  447. touch($Folder->path . DS . 'not_hidden.txt');
  448. touch($Folder->path . DS . '.hidden.txt');
  449. mkdir($Folder->path . DS . 'visible_folder/.git', 0777, true);
  450. $expected = [
  451. [
  452. $Folder->path,
  453. $Folder->path . DS . 'visible_folder',
  454. ],
  455. [
  456. $Folder->path . DS . 'not_hidden.txt',
  457. ],
  458. ];
  459. $result = $Folder->tree(null, true);
  460. $this->assertEquals($expected, $result);
  461. $result = $Folder->tree(null, ['.']);
  462. $this->assertEquals($expected, $result);
  463. $expected = [
  464. [
  465. $Folder->path,
  466. $Folder->path . DS . 'visible_folder',
  467. $Folder->path . DS . 'visible_folder' . DS . '.git',
  468. $Folder->path . DS . '.svn',
  469. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
  470. ],
  471. [
  472. $Folder->path . DS . 'not_hidden.txt',
  473. $Folder->path . DS . '.hidden.txt',
  474. $Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
  475. $Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
  476. ],
  477. ];
  478. $result = $Folder->tree(null, false);
  479. sort($result[0]);
  480. sort($expected[0]);
  481. sort($result[1]);
  482. sort($expected[1]);
  483. $this->assertEquals($expected, $result);
  484. $Folder->delete();
  485. }
  486. /**
  487. * testWindowsPath method
  488. */
  489. public function testWindowsPath(): void
  490. {
  491. $this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
  492. $this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
  493. $this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
  494. $this->assertTrue(Folder::isWindowsPath('\\\\vmware-host\\Shared Folders\\file'));
  495. }
  496. /**
  497. * testIsAbsolute method
  498. */
  499. public function testIsAbsolute(): void
  500. {
  501. $this->assertFalse(Folder::isAbsolute('path/to/file'));
  502. $this->assertFalse(Folder::isAbsolute('cake/'));
  503. $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
  504. $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
  505. $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
  506. $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
  507. $this->assertFalse(Folder::isAbsolute('notRegisteredStreamWrapper://example'));
  508. $this->assertFalse(Folder::isAbsolute('://example'));
  509. $this->assertTrue(Folder::isAbsolute('/usr/local'));
  510. $this->assertTrue(Folder::isAbsolute('//path/to/file'));
  511. $this->assertTrue(Folder::isAbsolute('C:\\cake'));
  512. $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
  513. $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
  514. $this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
  515. $this->assertTrue(Folder::isAbsolute('http://www.example.com'));
  516. }
  517. /**
  518. * testIsSlashTerm method
  519. */
  520. public function testIsSlashTerm(): void
  521. {
  522. $this->assertFalse(Folder::isSlashTerm('cake'));
  523. $this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
  524. $this->assertTrue(Folder::isSlashTerm('/usr/local/'));
  525. }
  526. /**
  527. * testStatic method
  528. */
  529. public function testSlashTerm(): void
  530. {
  531. $result = Folder::slashTerm('/path/to/file');
  532. $this->assertSame('/path/to/file/', $result);
  533. }
  534. /**
  535. * testNormalizeFullPath method
  536. */
  537. public function testNormalizeFullPath(): void
  538. {
  539. $path = '/path/to\file';
  540. $expected = '/path/to/file';
  541. $result = Folder::normalizeFullPath($path);
  542. $this->assertSame($expected, $result);
  543. $path = '\\path\\to\file';
  544. $expected = '/path/to/file';
  545. $result = Folder::normalizeFullPath($path);
  546. $this->assertSame($expected, $result);
  547. $path = 'C:\\path/to/file';
  548. $expected = 'C:\\path\\to\\file';
  549. $result = Folder::normalizeFullPath($path);
  550. $this->assertSame($expected, $result);
  551. }
  552. /**
  553. * correctSlashFor method
  554. */
  555. public function testCorrectSlashFor(): void
  556. {
  557. $path = '/path/to/file';
  558. $result = Folder::correctSlashFor($path);
  559. $this->assertSame('/', $result);
  560. $path = '\\path\\to\\file';
  561. $result = Folder::correctSlashFor($path);
  562. $this->assertSame('/', $result);
  563. $path = 'C:\\path\to\\file';
  564. $result = Folder::correctSlashFor($path);
  565. $this->assertSame('\\', $result);
  566. }
  567. /**
  568. * testFind method
  569. */
  570. public function testFind(): void
  571. {
  572. $Folder = new Folder();
  573. $Folder->cd(CORE_PATH . 'config');
  574. $result = $Folder->find();
  575. $expected = ['config.php'];
  576. $this->assertSame(array_diff($expected, $result), []);
  577. $this->assertSame(array_diff($expected, $result), []);
  578. $result = $Folder->find('.*', true);
  579. $expected = ['bootstrap.php', 'config.php'];
  580. $this->assertSame($expected, $result);
  581. $result = $Folder->find('.*\.php');
  582. $expected = ['bootstrap.php', 'config.php'];
  583. $this->assertSame(array_diff($expected, $result), []);
  584. $this->assertSame(array_diff($expected, $result), []);
  585. $result = $Folder->find('.*\.php', true);
  586. $expected = ['bootstrap.php', 'config.php'];
  587. $this->assertSame($expected, $result);
  588. $result = $Folder->find('.*ig\.php');
  589. $expected = ['config.php'];
  590. $this->assertSame($expected, $result);
  591. $result = $Folder->find('config\.php');
  592. $expected = ['config.php'];
  593. $this->assertSame($expected, $result);
  594. $Folder = new Folder(TMP . 'tests/', true);
  595. new File($Folder->pwd() . DS . 'paths.php', true);
  596. $Folder->create($Folder->pwd() . DS . 'testme');
  597. $Folder->cd('testme');
  598. $result = $Folder->find('paths\.php');
  599. $expected = [];
  600. $this->assertSame($expected, $result);
  601. $Folder->cd($Folder->pwd() . '/..');
  602. $result = $Folder->find('paths\.php');
  603. $expected = ['paths.php'];
  604. $this->assertSame($expected, $result);
  605. }
  606. /**
  607. * testFindRecursive method
  608. */
  609. public function testFindRecursive(): void
  610. {
  611. $Folder = new Folder(CORE_PATH . 'config');
  612. $result = $Folder->findRecursive('(config|paths)\.php');
  613. $expected = [
  614. CORE_PATH . 'config' . DS . 'config.php',
  615. ];
  616. $this->assertSame([], array_diff($expected, $result));
  617. $result = $Folder->findRecursive('(config|bootstrap)\.php', true);
  618. $expected = [
  619. CORE_PATH . 'config' . DS . 'bootstrap.php',
  620. CORE_PATH . 'config' . DS . 'config.php',
  621. ];
  622. $this->assertSame($expected, $result);
  623. $path = TMP . 'tests' . DS;
  624. $Folder = new Folder($path, true);
  625. $Folder->create($path . 'sessions');
  626. $Folder->create($path . 'testme');
  627. $Folder->cd($path . 'testme');
  628. $File = new File($Folder->pwd() . DS . 'paths.php');
  629. $File->create();
  630. $Folder->cd($path . 'sessions');
  631. $result = $Folder->findRecursive('paths\.php');
  632. $expected = [];
  633. $this->assertSame($expected, $result);
  634. $Folder->cd($path . 'testme');
  635. $File = new File($Folder->pwd() . DS . 'my.php');
  636. $File->create();
  637. $Folder->cd($path);
  638. $result = $Folder->findRecursive('(paths|my)\.php');
  639. $expected = [
  640. $path . 'testme' . DS . 'my.php',
  641. $path . 'testme' . DS . 'paths.php',
  642. ];
  643. $this->assertSame(sort($expected), sort($result));
  644. $result = $Folder->findRecursive('(paths|my)\.php', true);
  645. $expected = [
  646. $path . 'testme' . DS . 'my.php',
  647. $path . 'testme' . DS . 'paths.php',
  648. ];
  649. $this->assertSame($expected, $result);
  650. }
  651. /**
  652. * testConstructWithNonExistentPath method
  653. */
  654. public function testConstructWithNonExistentPath(): void
  655. {
  656. $path = TMP . 'tests' . DS;
  657. $Folder = new Folder($path . 'config_nonexistent', true);
  658. $this->assertDirectoryExists($path . 'config_nonexistent');
  659. $Folder->cd($path);
  660. }
  661. /**
  662. * testDirSize method
  663. */
  664. public function testDirSize(): void
  665. {
  666. $path = TMP . 'tests' . DS;
  667. $Folder = new Folder($path . 'config_nonexistent', true);
  668. $this->assertSame(0, $Folder->dirSize());
  669. $File = new File($Folder->pwd() . DS . 'my.php', true, 0777);
  670. $File->create();
  671. $File->write('something here');
  672. $File->close();
  673. $this->assertSame(14, $Folder->dirSize());
  674. }
  675. /**
  676. * test that errors and messages can be restarted
  677. */
  678. public function testReset(): void
  679. {
  680. $path = TMP . 'tests' . DS . 'folder_delete_test';
  681. mkdir($path, 0777, true);
  682. $folder = $path . DS . 'sub';
  683. mkdir($folder);
  684. $file = $folder . DS . 'file';
  685. touch($file);
  686. chmod($folder, 0555);
  687. chmod($file, 0444);
  688. $Folder = new Folder($folder);
  689. $return = $Folder->delete();
  690. $this->assertFalse($return);
  691. $messages = $Folder->messages();
  692. $errors = $Folder->errors();
  693. $expected = [
  694. $file . ' NOT removed',
  695. $folder . ' NOT removed',
  696. ];
  697. sort($expected);
  698. sort($errors);
  699. $this->assertEmpty($messages);
  700. $this->assertEquals($expected, $errors);
  701. chmod($file, 0644);
  702. chmod($folder, 0755);
  703. $return = $Folder->delete();
  704. $this->assertTrue($return);
  705. $messages = $Folder->messages();
  706. $errors = $Folder->errors();
  707. $expected = [
  708. $file . ' removed',
  709. $folder . ' removed',
  710. ];
  711. sort($expected);
  712. sort($messages);
  713. $this->assertEmpty($errors);
  714. $this->assertEquals($expected, $messages);
  715. }
  716. /**
  717. * testDelete method
  718. */
  719. public function testDelete(): void
  720. {
  721. $path = TMP . 'tests' . DS . 'folder_delete_test';
  722. mkdir($path, 0777, true);
  723. touch($path . DS . 'file_1');
  724. mkdir($path . DS . 'level_1_1');
  725. touch($path . DS . 'level_1_1/file_1_1');
  726. mkdir($path . DS . 'level_1_1/level_2_1');
  727. touch($path . DS . 'level_1_1/level_2_1/file_2_1');
  728. touch($path . DS . 'level_1_1/level_2_1/file_2_2');
  729. mkdir($path . DS . 'level_1_1/level_2_2');
  730. $Folder = new Folder($path, true);
  731. $return = $Folder->delete();
  732. $this->assertTrue($return);
  733. $messages = $Folder->messages();
  734. $errors = $Folder->errors();
  735. $this->assertEquals([], $errors);
  736. $expected = [
  737. $path . DS . 'file_1 removed',
  738. $path . DS . 'level_1_1' . DS . 'file_1_1 removed',
  739. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
  740. $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
  741. $path . DS . 'level_1_1' . DS . 'level_2_1 removed',
  742. $path . DS . 'level_1_1' . DS . 'level_2_2 removed',
  743. $path . DS . 'level_1_1 removed',
  744. $path . ' removed',
  745. ];
  746. sort($expected);
  747. sort($messages);
  748. $this->assertEquals($expected, $messages);
  749. }
  750. /**
  751. * testCopy method
  752. *
  753. * Verify that subdirectories existing in both destination and source directory
  754. * are merged recursively.
  755. */
  756. public function testCopy(): void
  757. {
  758. // phpcs:disable
  759. /**
  760. * @var string $path
  761. * @var string $folderOne
  762. * @var string $folderOneA
  763. * @var string $folderTwo
  764. * @var string $folderTwoB
  765. * @var string $folderThree
  766. * @var string $fileOne
  767. * @var string $fileTwo
  768. * @var string $fileOneA
  769. * @var string $fileTwoB
  770. */
  771. extract($this->_setupFilesystem());
  772. // phpcs:enable
  773. $Folder = new Folder($folderOne);
  774. $result = $Folder->copy($folderThree);
  775. $this->assertTrue($result);
  776. $this->assertFileExists($folderThree . DS . 'file1.php');
  777. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  778. $Folder = new Folder($folderTwo);
  779. $result = $Folder->copy($folderThree);
  780. $this->assertTrue($result);
  781. $this->assertFileExists($folderThree . DS . 'file1.php');
  782. $this->assertFileExists($folderThree . DS . 'file2.php');
  783. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  784. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  785. $Folder = new Folder($path);
  786. $Folder->delete();
  787. }
  788. /**
  789. * testCopyWithMerge method
  790. *
  791. * Verify that subdirectories existing in both destination and source directory
  792. * are merged recursively.
  793. */
  794. public function testCopyWithMerge(): void
  795. {
  796. // phpcs:disable
  797. /**
  798. * @var string $path
  799. * @var string $folderOne
  800. * @var string $folderOneA
  801. * @var string $folderTwo
  802. * @var string $folderTwoB
  803. * @var string $folderThree
  804. * @var string $fileOne
  805. * @var string $fileTwo
  806. * @var string $fileOneA
  807. * @var string $fileTwoB
  808. */
  809. extract($this->_setupFilesystem());
  810. // phpcs:enable
  811. $Folder = new Folder($folderOne);
  812. $result = $Folder->copy($folderThree);
  813. $this->assertTrue($result);
  814. $this->assertFileExists($folderThree . DS . 'file1.php');
  815. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  816. $Folder = new Folder($folderTwo);
  817. $result = $Folder->copy($folderThree, ['scheme' => Folder::MERGE]);
  818. $this->assertTrue($result);
  819. $this->assertFileExists($folderThree . DS . 'file1.php');
  820. $this->assertFileExists($folderThree . DS . 'file2.php');
  821. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  822. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  823. }
  824. /**
  825. * testCopyWithSkip method
  826. *
  827. * Verify that directories and files are copied recursively
  828. * even if the destination directory already exists.
  829. * Subdirectories existing in both destination and source directory
  830. * are skipped and not merged or overwritten.
  831. */
  832. public function testCopyWithSkip(): void
  833. {
  834. // phpcs:disable
  835. /**
  836. * @var string $path
  837. * @var string $folderOne
  838. * @var string $folderOneA
  839. * @var string $folderTwo
  840. * @var string $folderTwoB
  841. * @var string $folderThree
  842. * @var string $fileOne
  843. * @var string $fileTwo
  844. * @var string $fileOneA
  845. * @var string $fileTwoB
  846. */
  847. extract($this->_setupFilesystem());
  848. // phpcs:enable
  849. $Folder = new Folder($folderOne);
  850. $result = $Folder->copy($folderTwo, ['scheme' => Folder::SKIP]);
  851. $this->assertTrue($result);
  852. $this->assertFileExists($folderTwo . DS . 'file1.php');
  853. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  854. $Folder = new Folder($folderTwo);
  855. $Folder->delete();
  856. $Folder = new Folder($folderOne);
  857. $result = $Folder->copy($folderTwo, ['scheme' => Folder::SKIP]);
  858. $this->assertTrue($result);
  859. $this->assertFileExists($folderTwo . DS . 'file1.php');
  860. $this->assertFileExists($folderTwo . DS . 'folderA' . DS . 'fileA.php');
  861. $Folder = new Folder($folderTwo);
  862. $Folder->delete();
  863. new Folder($folderTwo, true);
  864. new Folder($folderTwo . DS . 'folderB', true);
  865. file_put_contents($folderTwo . DS . 'file2.php', 'touched');
  866. file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  867. $Folder = new Folder($folderTwo);
  868. $result = $Folder->copy($folderThree, ['scheme' => Folder::SKIP]);
  869. $this->assertTrue($result);
  870. $this->assertFileExists($folderThree . DS . 'file2.php');
  871. $this->assertStringEqualsFile($folderThree . DS . 'file2.php', 'touched');
  872. $this->assertStringEqualsFile($folderThree . DS . 'folderB' . DS . 'fileB.php', 'untouched');
  873. }
  874. /**
  875. * Test that SKIP mode skips files too.
  876. */
  877. public function testCopyWithSkipFileSkipped(): void
  878. {
  879. $path = TMP . 'folder_test';
  880. $folderOne = $path . DS . 'folder1';
  881. $folderTwo = $path . DS . 'folder2';
  882. new Folder($path, true);
  883. new Folder($folderOne, true);
  884. new Folder($folderTwo, true);
  885. file_put_contents($folderOne . DS . 'fileA.txt', 'Folder One File');
  886. file_put_contents($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  887. $Folder = new Folder($folderOne);
  888. $result = $Folder->copy($folderTwo, ['scheme' => Folder::SKIP]);
  889. $this->assertTrue($result);
  890. $this->assertStringEqualsFile($folderTwo . DS . 'fileA.txt', 'Folder Two File');
  891. }
  892. /**
  893. * testCopyWithOverwrite
  894. *
  895. * Verify that subdirectories existing in both destination and source directory
  896. * are overwritten/replaced recursively.
  897. */
  898. public function testCopyWithOverwrite(): void
  899. {
  900. // phpcs:disable
  901. /**
  902. * @var string $path
  903. * @var string $folderOne
  904. * @var string $folderOneA
  905. * @var string $folderTwo
  906. * @var string $folderTwoB
  907. * @var string $folderThree
  908. * @var string $fileOne
  909. * @var string $fileTwo
  910. * @var string $fileOneA
  911. * @var string $fileTwoB
  912. */
  913. extract($this->_setupFilesystem());
  914. // phpcs:enable
  915. $Folder = new Folder($folderOne);
  916. $Folder->copy($folderThree, ['scheme' => Folder::OVERWRITE]);
  917. $this->assertFileExists($folderThree . DS . 'file1.php');
  918. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  919. $Folder = new Folder($folderTwo);
  920. $result = $Folder->copy($folderThree, ['scheme' => Folder::OVERWRITE]);
  921. $this->assertTrue($result);
  922. $this->assertFileExists($folderThree . DS . 'folderA' . DS . 'fileA.php');
  923. $Folder = new Folder($folderOne);
  924. unlink($fileOneA);
  925. $result = $Folder->copy($folderThree, ['scheme' => Folder::OVERWRITE]);
  926. $this->assertTrue($result);
  927. $this->assertFileExists($folderThree . DS . 'file1.php');
  928. $this->assertFileExists($folderThree . DS . 'file2.php');
  929. $this->assertFileDoesNotExist($folderThree . DS . 'folderA' . DS . 'fileA.php');
  930. $this->assertFileExists($folderThree . DS . 'folderB' . DS . 'fileB.php');
  931. }
  932. /**
  933. * testCopyWithoutRecursive
  934. *
  935. * Verify that only the files exist in the target directory.
  936. */
  937. public function testCopyWithoutRecursive(): void
  938. {
  939. // phpcs:disable
  940. /**
  941. * @var string $path
  942. * @var string $folderOne
  943. * @var string $folderOneA
  944. * @var string $folderTwo
  945. * @var string $folderTwoB
  946. * @var string $folderThree
  947. * @var string $fileOne
  948. * @var string $fileTwo
  949. * @var string $fileOneA
  950. * @var string $fileTwoB
  951. */
  952. extract($this->_setupFilesystem());
  953. // phpcs:enable
  954. $Folder = new Folder($folderOne);
  955. $Folder->copy($folderThree, ['recursive' => false]);
  956. $this->assertFileExists($folderThree . DS . 'file1.php');
  957. $this->assertDirectoryDoesNotExist($folderThree . DS . 'folderA');
  958. $this->assertFileDoesNotExist($folderThree . DS . 'folderA' . DS . 'fileA.php');
  959. }
  960. /**
  961. * Setup filesystem for copy tests
  962. * $path: folder_test/
  963. *
  964. * - folder1/file1.php
  965. * - folder1/folderA/fileA.php
  966. * - folder2/file2.php
  967. * - folder2/folderB/fileB.php
  968. * - folder3/
  969. *
  970. * @return array Filenames to extract in the test methods
  971. */
  972. protected function _setupFilesystem(): array
  973. {
  974. $path = TMP . 'tests';
  975. $folderOne = $path . DS . 'folder1';
  976. $folderOneA = $folderOne . DS . 'folderA';
  977. $folderTwo = $path . DS . 'folder2';
  978. $folderTwoB = $folderTwo . DS . 'folderB';
  979. $folderThree = $path . DS . 'folder3';
  980. $fileOne = $folderOne . DS . 'file1.php';
  981. $fileTwo = $folderTwo . DS . 'file2.php';
  982. $fileOneA = $folderOneA . DS . 'fileA.php';
  983. $fileTwoB = $folderTwoB . DS . 'fileB.php';
  984. new Folder($path, true);
  985. new Folder($folderOne, true);
  986. new Folder($folderOneA, true);
  987. new Folder($folderTwo, true);
  988. new Folder($folderTwoB, true);
  989. new Folder($folderThree, true);
  990. touch($fileOne);
  991. touch($fileTwo);
  992. touch($fileOneA);
  993. touch($fileTwoB);
  994. return compact(
  995. 'path',
  996. 'folderOne',
  997. 'folderOneA',
  998. 'folderTwo',
  999. 'folderTwoB',
  1000. 'folderThree',
  1001. 'fileOne',
  1002. 'fileOneA',
  1003. 'fileTwo',
  1004. 'fileTwoB'
  1005. );
  1006. }
  1007. /**
  1008. * testMove method
  1009. *
  1010. * Verify that directories and files are moved recursively
  1011. * even if the destination directory already exists.
  1012. * Subdirectories existing in both destination and source directory
  1013. * are merged recursively.
  1014. */
  1015. public function testMove(): void
  1016. {
  1017. // phpcs:disable
  1018. /**
  1019. * @var string $path
  1020. * @var string $folderOne
  1021. * @var string $folderOneA
  1022. * @var string $folderTwo
  1023. * @var string $folderTwoB
  1024. * @var string $folderThree
  1025. * @var string $fileOne
  1026. * @var string $fileTwo
  1027. * @var string $fileOneA
  1028. * @var string $fileTwoB
  1029. */
  1030. extract($this->_setupFilesystem());
  1031. // phpcs:enable
  1032. $Folder = new Folder($folderOne);
  1033. $result = $Folder->move($folderTwo);
  1034. $this->assertTrue($result);
  1035. $this->assertFileExists($folderTwo . '/file1.php');
  1036. $this->assertDirectoryExists($folderTwo . '/folderB');
  1037. $this->assertFileExists($folderTwo . '/folderB/fileB.php');
  1038. $this->assertFileDoesNotExist($fileOne);
  1039. $this->assertFileExists($folderTwo . '/folderA');
  1040. $this->assertFileDoesNotExist($folderOneA);
  1041. $this->assertFileDoesNotExist($fileOneA);
  1042. $Folder = new Folder($folderTwo);
  1043. $Folder->delete();
  1044. new Folder($folderOne, true);
  1045. new Folder($folderOneA, true);
  1046. touch($fileOne);
  1047. touch($fileOneA);
  1048. $Folder = new Folder($folderOne);
  1049. $result = $Folder->move($folderTwo);
  1050. $this->assertTrue($result);
  1051. $this->assertFileExists($folderTwo . '/file1.php');
  1052. $this->assertDirectoryExists($folderTwo . '/folderA');
  1053. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1054. $this->assertFileDoesNotExist($fileOne);
  1055. $this->assertFileDoesNotExist($folderOneA);
  1056. $this->assertFileDoesNotExist($fileOneA);
  1057. $Folder = new Folder($folderTwo);
  1058. $Folder->delete();
  1059. new Folder($folderOne, true);
  1060. new Folder($folderOneA, true);
  1061. new Folder($folderTwo, true);
  1062. new Folder($folderTwoB, true);
  1063. touch($fileOne);
  1064. touch($fileOneA);
  1065. new Folder($folderOne . '/folderB', true);
  1066. touch($folderOne . '/folderB/fileB.php');
  1067. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1068. $Folder = new Folder($folderOne);
  1069. $result = $Folder->move($folderTwo);
  1070. $this->assertTrue($result);
  1071. $this->assertFileExists($folderTwo . '/file1.php');
  1072. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', '');
  1073. $this->assertFileDoesNotExist($fileOne);
  1074. $this->assertFileDoesNotExist($folderOneA);
  1075. $this->assertFileDoesNotExist($fileOneA);
  1076. $Folder = new Folder($path);
  1077. $Folder->delete();
  1078. }
  1079. /**
  1080. * testMoveWithSkip method
  1081. *
  1082. * Verify that directories and files are moved recursively
  1083. * even if the destination directory already exists.
  1084. * Subdirectories existing in both destination and source directory
  1085. * are skipped and not merged or overwritten.
  1086. */
  1087. public function testMoveWithSkip(): void
  1088. {
  1089. // phpcs:disable
  1090. /**
  1091. * @var string $path
  1092. * @var string $folderOne
  1093. * @var string $folderOneA
  1094. * @var string $folderTwo
  1095. * @var string $folderTwoB
  1096. * @var string $folderThree
  1097. * @var string $fileOne
  1098. * @var string $fileTwo
  1099. * @var string $fileOneA
  1100. * @var string $fileTwoB
  1101. */
  1102. extract($this->_setupFilesystem());
  1103. // phpcs:enable
  1104. $Folder = new Folder($folderOne);
  1105. $result = $Folder->move($folderTwo, ['scheme' => Folder::SKIP]);
  1106. $this->assertTrue($result);
  1107. $this->assertFileExists($folderTwo . '/file1.php');
  1108. $this->assertDirectoryExists($folderTwo . '/folderB');
  1109. $this->assertFileExists($folderTwoB . '/fileB.php');
  1110. $this->assertFileDoesNotExist($fileOne);
  1111. $this->assertFileDoesNotExist($folderOneA);
  1112. $this->assertFileDoesNotExist($fileOneA);
  1113. $Folder = new Folder($folderTwo);
  1114. $Folder->delete();
  1115. new Folder($folderOne, true);
  1116. new Folder($folderOneA, true);
  1117. new Folder($folderTwo, true);
  1118. touch($fileOne);
  1119. touch($fileOneA);
  1120. $Folder = new Folder($folderOne);
  1121. $result = $Folder->move($folderTwo, ['scheme' => Folder::SKIP]);
  1122. $this->assertTrue($result);
  1123. $this->assertFileExists($folderTwo . '/file1.php');
  1124. $this->assertDirectoryExists($folderTwo . '/folderA');
  1125. $this->assertFileExists($folderTwo . '/folderA/fileA.php');
  1126. $this->assertFileDoesNotExist($fileOne);
  1127. $this->assertFileDoesNotExist($folderOneA);
  1128. $this->assertFileDoesNotExist($fileOneA);
  1129. $Folder = new Folder($folderTwo);
  1130. $Folder->delete();
  1131. new Folder($folderOne, true);
  1132. new Folder($folderOneA, true);
  1133. new Folder($folderTwo, true);
  1134. new Folder($folderTwoB, true);
  1135. touch($fileOne);
  1136. touch($fileOneA);
  1137. file_put_contents($folderTwoB . '/fileB.php', 'untouched');
  1138. $Folder = new Folder($folderOne);
  1139. $result = $Folder->move($folderTwo, ['scheme' => Folder::SKIP]);
  1140. $this->assertTrue($result);
  1141. $this->assertFileExists($folderTwo . '/file1.php');
  1142. $this->assertStringEqualsFile($folderTwoB . '/fileB.php', 'untouched');
  1143. $this->assertFileDoesNotExist($fileOne);
  1144. $this->assertFileDoesNotExist($folderOneA);
  1145. $this->assertFileDoesNotExist($fileOneA);
  1146. $Folder = new Folder($path);
  1147. $Folder->delete();
  1148. }
  1149. public function testMoveWithoutRecursive(): void
  1150. {
  1151. // phpcs:disable
  1152. /**
  1153. * @var string $path
  1154. * @var string $folderOne
  1155. * @var string $folderOneA
  1156. * @var string $folderTwo
  1157. * @var string $folderTwoB
  1158. * @var string $folderThree
  1159. * @var string $fileOne
  1160. * @var string $fileTwo
  1161. * @var string $fileOneA
  1162. * @var string $fileTwoB
  1163. */
  1164. extract($this->_setupFilesystem());
  1165. // phpcs:enable
  1166. $Folder = new Folder($folderOne);
  1167. $result = $Folder->move($folderTwo, ['recursive' => false]);
  1168. $this->assertTrue($result);
  1169. $this->assertFileExists($folderTwo . '/file1.php');
  1170. $this->assertDirectoryDoesNotExist($folderTwo . '/folderA');
  1171. $this->assertFileDoesNotExist($folderTwo . '/folderA/fileA.php');
  1172. }
  1173. /**
  1174. * testSortByTime method
  1175. *
  1176. * Verify that the order using modified time is correct.
  1177. */
  1178. public function testSortByTime(): void
  1179. {
  1180. $Folder = new Folder(TMP . 'tests', true);
  1181. $file2 = new File($Folder->pwd() . DS . 'file_2.tmp');
  1182. $file2->create();
  1183. sleep(1);
  1184. $file1 = new File($Folder->pwd() . DS . 'file_1.tmp');
  1185. $file1->create();
  1186. $results = $Folder->find('.*', Folder::SORT_TIME);
  1187. $this->assertSame(['file_2.tmp', 'file_1.tmp'], $results);
  1188. }
  1189. /**
  1190. * Verify that the order using name is correct.
  1191. */
  1192. public function testSortByName(): void
  1193. {
  1194. $Folder = new Folder(TMP . 'tests', true);
  1195. $fileA = new File($Folder->pwd() . DS . 'a.txt');
  1196. $fileA->create();
  1197. $fileC = new File($Folder->pwd() . DS . 'c.txt');
  1198. $fileC->create();
  1199. sleep(1);
  1200. $fileB = new File($Folder->pwd() . DS . 'b.txt');
  1201. $fileB->create();
  1202. $results = $Folder->find('.*', Folder::SORT_NAME);
  1203. $this->assertSame(['a.txt', 'b.txt', 'c.txt'], $results);
  1204. }
  1205. /**
  1206. * testIsRegisteredStreamWrapper
  1207. */
  1208. public function testIsRegisteredStreamWrapper(): void
  1209. {
  1210. foreach (stream_get_wrappers() as $wrapper) {
  1211. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . '://path/to/file'));
  1212. $this->assertFalse(Folder::isRegisteredStreamWrapper('bad.' . $wrapper . '://path/to/file'));
  1213. }
  1214. $wrapper = 'unit.test1-';
  1215. $this->assertFalse(Folder::isRegisteredStreamWrapper($wrapper . '://path/to/file'));
  1216. stream_wrapper_register($wrapper, self::class);
  1217. $this->assertTrue(Folder::isRegisteredStreamWrapper($wrapper . '://path/to/file'));
  1218. stream_wrapper_unregister($wrapper);
  1219. }
  1220. }