FilesystemTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Utility;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Utility\Filesystem;
  19. use org\bovigo\vfs\vfsStream;
  20. /**
  21. * Filesystem class
  22. */
  23. class FilesystemTest extends TestCase
  24. {
  25. protected $vfs;
  26. protected $fs;
  27. protected $vfsPath;
  28. public function setUp(): void
  29. {
  30. parent::setUp();
  31. $this->vfs = vfsStream::setup('root');
  32. $this->vfsPath = vfsStream::url('root');
  33. $this->fs = new Filesystem();
  34. clearstatcache();
  35. }
  36. public function testMkdir(): void
  37. {
  38. $path = $this->vfsPath . DS . 'tests' . DS . 'first' . DS . 'second' . DS . 'third';
  39. $this->fs->mkdir($path);
  40. $this->assertTrue(is_dir($path));
  41. }
  42. public function testDumpFile(): void
  43. {
  44. $path = $this->vfsPath . DS . 'foo.txt';
  45. $this->fs->dumpFile($path, 'bar');
  46. $this->assertEquals(file_get_contents($path), 'bar');
  47. $path = $this->vfsPath . DS . 'empty.txt';
  48. $this->fs->dumpFile($path, '');
  49. $this->assertSame(file_get_contents($path), '');
  50. }
  51. public function testCopyDir(): void
  52. {
  53. $return = $this->fs->copyDir(WWW_ROOT, $this->vfsPath . DS . 'dest');
  54. $this->assertTrue($return);
  55. }
  56. public function testDeleteDir(): void
  57. {
  58. $structure = [
  59. 'Core' => [
  60. 'AbstractFactory' => [
  61. 'test.php' => 'some text content',
  62. 'other.php' => 'Some more text content',
  63. 'Invalid.csv' => 'Something else',
  64. ],
  65. 'AnEmptyFolder' => [],
  66. 'badlocation.php' => 'some bad content',
  67. ],
  68. ];
  69. vfsStream::create($structure);
  70. $return = $this->fs->deleteDir($this->vfsPath . DS . 'Core');
  71. $this->assertTrue($return);
  72. }
  73. /**
  74. * Tests deleteDir() on directory that contains symlinks
  75. */
  76. public function testDeleteDirWithLinks(): void
  77. {
  78. $path = TMP . 'fs_links_test';
  79. // phpcs:ignore
  80. @mkdir($path);
  81. $target = $path . DS . 'target';
  82. // phpcs:ignore
  83. @mkdir($target);
  84. $link = $path . DS . 'link';
  85. // phpcs:ignore
  86. @symlink($target, $link);
  87. $this->assertTrue($this->fs->deleteDir($path));
  88. $this->assertFalse(file_exists($link));
  89. }
  90. }