FileSystemTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. namespace SPC\Tests\store;
  4. use PHPUnit\Framework\TestCase;
  5. use SPC\exception\FileSystemException;
  6. use SPC\exception\RuntimeException;
  7. use SPC\store\FileSystem;
  8. /**
  9. * @internal
  10. */
  11. class FileSystemTest extends TestCase
  12. {
  13. private const TEST_FILE_CONTENT = 'Hello! Bye!';
  14. public static function setUpBeforeClass(): void
  15. {
  16. if (file_put_contents(WORKING_DIR . '/.testfile', self::TEST_FILE_CONTENT) === false) {
  17. static::markTestSkipped('Current environment or working dir is not writable!');
  18. }
  19. }
  20. public static function tearDownAfterClass(): void
  21. {
  22. if (file_exists(WORKING_DIR . '/.testfile')) {
  23. unlink(WORKING_DIR . '/.testfile');
  24. }
  25. }
  26. /**
  27. * @throws FileSystemException
  28. */
  29. public function testReplaceFileRegex()
  30. {
  31. $file = WORKING_DIR . '/.txt1';
  32. file_put_contents($file, 'hello');
  33. FileSystem::replaceFileRegex($file, '/ll/', '11');
  34. $this->assertEquals('he11o', file_get_contents($file));
  35. unlink($file);
  36. }
  37. public function testFindCommandPath()
  38. {
  39. $this->assertNull(FileSystem::findCommandPath('randomtestxxxxx'));
  40. if (PHP_OS_FAMILY === 'Windows') {
  41. $this->assertIsString(FileSystem::findCommandPath('explorer'));
  42. } elseif (in_array(PHP_OS_FAMILY, ['Linux', 'Darwin', 'FreeBSD'])) {
  43. $this->assertIsString(FileSystem::findCommandPath('uname'));
  44. }
  45. }
  46. /**
  47. * @throws FileSystemException
  48. */
  49. public function testReadFile()
  50. {
  51. $file = WORKING_DIR . '/.testread';
  52. file_put_contents($file, 'haha');
  53. $content = FileSystem::readFile($file);
  54. $this->assertEquals('haha', $content);
  55. @unlink($file);
  56. }
  57. /**
  58. * @throws FileSystemException
  59. */
  60. public function testReplaceFileUser()
  61. {
  62. $file = WORKING_DIR . '/.txt1';
  63. file_put_contents($file, 'hello');
  64. FileSystem::replaceFileUser($file, function ($file) {
  65. return str_replace('el', '55', $file);
  66. });
  67. $this->assertEquals('h55lo', file_get_contents($file));
  68. unlink($file);
  69. }
  70. public function testExtname()
  71. {
  72. $this->assertEquals('exe', FileSystem::extname('/tmp/asd.exe'));
  73. $this->assertEquals('', FileSystem::extname('/tmp/asd.'));
  74. }
  75. /**
  76. * @throws \ReflectionException
  77. * @throws FileSystemException
  78. */
  79. public function testGetClassesPsr4()
  80. {
  81. $classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/builder/extension', 'SPC\\builder\\extension');
  82. foreach ($classes as $class) {
  83. $this->assertIsString($class);
  84. new \ReflectionClass($class);
  85. }
  86. }
  87. public function testConvertPath()
  88. {
  89. $this->assertEquals('phar://C:/pharfile.phar', FileSystem::convertPath('phar://C:/pharfile.phar'));
  90. if (DIRECTORY_SEPARATOR === '\\') {
  91. $this->assertEquals('C:\Windows\win.ini', FileSystem::convertPath('C:\Windows/win.ini'));
  92. }
  93. }
  94. /**
  95. * @throws FileSystemException
  96. */
  97. public function testCreateDir()
  98. {
  99. FileSystem::createDir(WORKING_DIR . '/.testdir');
  100. $this->assertDirectoryExists(WORKING_DIR . '/.testdir');
  101. rmdir(WORKING_DIR . '/.testdir');
  102. }
  103. /**
  104. * @throws FileSystemException
  105. */
  106. public function testReplaceFileStr()
  107. {
  108. $file = WORKING_DIR . '/.txt1';
  109. file_put_contents($file, 'hello');
  110. FileSystem::replaceFileStr($file, 'el', '55');
  111. $this->assertEquals('h55lo', file_get_contents($file));
  112. unlink($file);
  113. }
  114. /**
  115. * @throws FileSystemException
  116. */
  117. public function testResetDir()
  118. {
  119. // prepare fake git dir to test
  120. FileSystem::createDir(WORKING_DIR . '/.fake_down_test');
  121. FileSystem::writeFile(WORKING_DIR . '/.fake_down_test/a.c', 'int main() { return 0; }');
  122. FileSystem::resetDir(WORKING_DIR . '/.fake_down_test');
  123. $this->assertFileDoesNotExist(WORKING_DIR . '/.fake_down_test/a.c');
  124. FileSystem::removeDir(WORKING_DIR . '/.fake_down_test');
  125. }
  126. /**
  127. * @throws FileSystemException
  128. * @throws RuntimeException
  129. */
  130. public function testCopyDir()
  131. {
  132. // prepare fake git dir to test
  133. FileSystem::createDir(WORKING_DIR . '/.fake_down_test');
  134. FileSystem::writeFile(WORKING_DIR . '/.fake_down_test/a.c', 'int main() { return 0; }');
  135. FileSystem::copyDir(WORKING_DIR . '/.fake_down_test', WORKING_DIR . '/.fake_down_test2');
  136. $this->assertDirectoryExists(WORKING_DIR . '/.fake_down_test2');
  137. $this->assertFileExists(WORKING_DIR . '/.fake_down_test2/a.c');
  138. FileSystem::removeDir(WORKING_DIR . '/.fake_down_test');
  139. FileSystem::removeDir(WORKING_DIR . '/.fake_down_test2');
  140. }
  141. /**
  142. * @throws FileSystemException
  143. */
  144. public function testRemoveDir()
  145. {
  146. FileSystem::createDir(WORKING_DIR . '/.fake_down_test');
  147. $this->assertDirectoryExists(WORKING_DIR . '/.fake_down_test');
  148. FileSystem::removeDir(WORKING_DIR . '/.fake_down_test');
  149. $this->assertDirectoryDoesNotExist(WORKING_DIR . '/.fake_down_test');
  150. }
  151. /**
  152. * @throws FileSystemException
  153. */
  154. public function testLoadConfigArray()
  155. {
  156. $arr = FileSystem::loadConfigArray('lib');
  157. $this->assertArrayHasKey('zlib', $arr);
  158. }
  159. public function testIsRelativePath()
  160. {
  161. $this->assertTrue(FileSystem::isRelativePath('.'));
  162. $this->assertTrue(FileSystem::isRelativePath('.\\sdf'));
  163. if (DIRECTORY_SEPARATOR === '\\') {
  164. $this->assertFalse(FileSystem::isRelativePath('C:\\asdasd/fwe\asd'));
  165. } else {
  166. $this->assertFalse(FileSystem::isRelativePath('/fwefwefewf'));
  167. }
  168. }
  169. public function testScanDirFiles()
  170. {
  171. $this->assertFalse(FileSystem::scanDirFiles('wfwefewfewf'));
  172. $files = FileSystem::scanDirFiles(ROOT_DIR . '/config', true, true);
  173. $this->assertContains('lib.json', $files);
  174. }
  175. /**
  176. * @throws FileSystemException
  177. */
  178. public function testWriteFile()
  179. {
  180. FileSystem::writeFile(WORKING_DIR . '/.txt', 'txt');
  181. $this->assertFileExists(WORKING_DIR . '/.txt');
  182. $this->assertEquals('txt', FileSystem::readFile(WORKING_DIR . '/.txt'));
  183. unlink(WORKING_DIR . '/.txt');
  184. }
  185. }