FileTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Test for \Magento\Framework\Filesystem\Driver\File
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Filesystem\Driver;
  9. use Magento\Framework\Exception\FileSystemException;
  10. class FileTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var File
  14. */
  15. private $driver;
  16. /**
  17. * @var String
  18. */
  19. private $absolutePath;
  20. /**
  21. * @var String
  22. */
  23. private $generatedPath;
  24. /**
  25. * Returns relative path for the test.
  26. *
  27. * @param $relativePath
  28. * @return string
  29. */
  30. protected function getTestPath($relativePath)
  31. {
  32. return $this->absolutePath . $relativePath;
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function setUp()
  38. {
  39. $this->driver = new File();
  40. $this->absolutePath = dirname(__DIR__) . '/_files/';
  41. $this->generatedPath = $this->getTestPath('generated');
  42. $this->removeGeneratedDirectory();
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. protected function tearDown()
  48. {
  49. $this->removeGeneratedDirectory();
  50. }
  51. /**
  52. * Tests directory recursive read.
  53. */
  54. public function testReadDirectoryRecursively()
  55. {
  56. $paths = [
  57. 'foo/bar',
  58. 'foo/bar/baz',
  59. 'foo/bar/baz/file_one.txt',
  60. 'foo/bar/file_two.txt',
  61. 'foo/file_three.txt',
  62. ];
  63. $expected = array_map(['self', 'getTestPath'], $paths);
  64. $actual = $this->driver->readDirectoryRecursively($this->getTestPath('foo'));
  65. sort($actual);
  66. $this->assertEquals($expected, $actual);
  67. }
  68. /**
  69. * Tests directory reading exception.
  70. *
  71. * @expectedException \Magento\Framework\Exception\FileSystemException
  72. */
  73. public function testReadDirectoryRecursivelyFailure()
  74. {
  75. $this->driver->readDirectoryRecursively($this->getTestPath('not-existing-directory'));
  76. }
  77. /**
  78. * Tests of directory creating.
  79. *
  80. * @throws FileSystemException
  81. */
  82. public function testCreateDirectory()
  83. {
  84. $generatedPath = $this->getTestPath('generated/roo/bar/baz/foo');
  85. $generatedPathBase = $this->getTestPath('generated');
  86. // Delete the generated directory if it already exists
  87. if (is_dir($generatedPath)) {
  88. $this->assertTrue($this->driver->deleteDirectory($generatedPathBase));
  89. }
  90. $this->assertTrue($this->driver->createDirectory($generatedPath));
  91. $this->assertTrue(is_dir($generatedPath));
  92. }
  93. /**
  94. * Tests creation and removing of symlinks.
  95. *
  96. * @throws FileSystemException
  97. * @return void
  98. */
  99. public function testSymlinks(): void
  100. {
  101. $sourceDirectory = $this->generatedPath . '/source';
  102. $destinationDirectory = $this->generatedPath . '/destination';
  103. $this->driver->createDirectory($sourceDirectory);
  104. $this->driver->createDirectory($destinationDirectory);
  105. $linkName = $destinationDirectory . '/link';
  106. self::assertTrue($this->driver->isWritable($destinationDirectory));
  107. self::assertTrue($this->driver->symlink($sourceDirectory, $linkName));
  108. self::assertTrue($this->driver->isExists($linkName));
  109. self::assertTrue($this->driver->deleteDirectory($linkName));
  110. }
  111. /**
  112. * Remove generated directories.
  113. *
  114. * @throws FileSystemException
  115. * @return void
  116. */
  117. private function removeGeneratedDirectory(): void
  118. {
  119. if (is_dir($this->generatedPath)) {
  120. $this->driver->deleteDirectory($this->generatedPath);
  121. }
  122. }
  123. }