FilesystemTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Filesystem;
  17. use Cake\Filesystem\Filesystem;
  18. use Cake\TestSuite\TestCase;
  19. use org\bovigo\vfs\vfsStream;
  20. /**
  21. * Filesystem class
  22. *
  23. * @coversDefaultClass \Cake\Filesystem\Filesystem
  24. */
  25. class FilesystemTest extends TestCase
  26. {
  27. protected $vfs;
  28. protected $fs;
  29. protected $vfsPath;
  30. public function setUp(): void
  31. {
  32. parent::setUp();
  33. $this->skipIf(version_compare(PHP_VERSION, '8.1.0-dev', '>='));
  34. $this->vfs = vfsStream::setup('root');
  35. $this->vfsPath = vfsStream::url('root');
  36. $this->fs = new Filesystem();
  37. clearstatcache();
  38. }
  39. /**
  40. * @covers ::mkdir
  41. */
  42. public function testMkdir(): void
  43. {
  44. $path = $this->vfsPath . DS . 'tests' . DS . 'first' . DS . 'second' . DS . 'third';
  45. $this->fs->mkdir($path);
  46. $this->assertTrue(is_dir($path));
  47. }
  48. /**
  49. * @covers ::dumpFile
  50. */
  51. public function testDumpFile(): void
  52. {
  53. $path = $this->vfsPath . DS . 'foo.txt';
  54. $this->fs->dumpFile($path, 'bar');
  55. $this->assertEquals(file_get_contents($path), 'bar');
  56. $path = $this->vfsPath . DS . 'empty.txt';
  57. $this->fs->dumpFile($path, '');
  58. $this->assertSame(file_get_contents($path), '');
  59. }
  60. /**
  61. * @covers ::copyDir
  62. */
  63. public function testCopyDir(): void
  64. {
  65. $return = $this->fs->copyDir(WWW_ROOT, $this->vfsPath . DS . 'dest');
  66. $this->assertTrue($return);
  67. }
  68. /**
  69. * @covers ::deleteDir
  70. */
  71. public function testDeleteDir(): void
  72. {
  73. $structure = [
  74. 'Core' => [
  75. 'AbstractFactory' => [
  76. 'test.php' => 'some text content',
  77. 'other.php' => 'Some more text content',
  78. 'Invalid.csv' => 'Something else',
  79. ],
  80. 'AnEmptyFolder' => [],
  81. 'badlocation.php' => 'some bad content',
  82. ],
  83. ];
  84. vfsStream::create($structure);
  85. $return = $this->fs->deleteDir($this->vfsPath . DS . 'Core');
  86. $this->assertTrue($return);
  87. }
  88. /**
  89. * Tests deleteDir() on directory that contains symlinks
  90. */
  91. public function testDeleteDirWithLinks(): void
  92. {
  93. $path = TMP . 'fs_links_test';
  94. // phpcs:ignore
  95. @mkdir($path);
  96. $target = $path . DS . 'target';
  97. // phpcs:ignore
  98. @mkdir($target);
  99. $link = $path . DS . 'link';
  100. // phpcs:ignore
  101. @symlink($target, $link);
  102. $this->assertTrue($this->fs->deleteDir($path));
  103. $this->assertFalse(file_exists($link));
  104. }
  105. }