FilesystemTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  31. * @return void
  32. */
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. $this->vfs = vfsStream::setup('root');
  37. $this->vfsPath = vfsStream::url('root');
  38. $this->fs = new Filesystem();
  39. clearstatcache();
  40. }
  41. /**
  42. * @return void
  43. * @covers ::mkdir
  44. */
  45. public function testMkdir()
  46. {
  47. $path = $this->vfsPath . DS . 'tests' . DS . 'first' . DS . 'second' . DS . 'third';
  48. $this->fs->mkdir($path);
  49. $this->assertTrue(is_dir($path));
  50. }
  51. /**
  52. * @return void
  53. * @covers ::dumpFile
  54. */
  55. public function testDumpFile()
  56. {
  57. $path = $this->vfsPath . DS . 'foo.txt';
  58. $this->fs->dumpFile($path, 'bar');
  59. $this->assertEquals(file_get_contents($path), 'bar');
  60. $path = $this->vfsPath . DS . 'empty.txt';
  61. $this->fs->dumpFile($path, '');
  62. $this->assertSame(file_get_contents($path), '');
  63. }
  64. /**
  65. * @return void
  66. * @covers ::copyDir
  67. */
  68. public function testCopyDir()
  69. {
  70. $return = $this->fs->copyDir(WWW_ROOT, $this->vfsPath . DS . 'dest');
  71. $this->assertTrue($return);
  72. }
  73. /**
  74. * @return void
  75. * @covers ::deleteDir
  76. */
  77. public function testDeleteDir()
  78. {
  79. $structure = [
  80. 'Core' => [
  81. 'AbstractFactory' => [
  82. 'test.php' => 'some text content',
  83. 'other.php' => 'Some more text content',
  84. 'Invalid.csv' => 'Something else',
  85. ],
  86. 'AnEmptyFolder' => [],
  87. 'badlocation.php' => 'some bad content',
  88. ],
  89. ];
  90. vfsStream::create($structure);
  91. $return = $this->fs->deleteDir($this->vfsPath . DS . 'Core');
  92. $this->assertTrue($return);
  93. }
  94. }