FilesystemTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. namespace Illuminate\Tests\Filesystem;
  3. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Filesystem\FilesystemManager;
  6. use Illuminate\Foundation\Application;
  7. use Illuminate\Support\LazyCollection;
  8. use Illuminate\Testing\Assert;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. use SplFileInfo;
  12. class FilesystemTest extends TestCase
  13. {
  14. private static $tempDir;
  15. /**
  16. * @beforeClass
  17. */
  18. public static function setUpTempDir()
  19. {
  20. self::$tempDir = sys_get_temp_dir().'/tmp';
  21. mkdir(self::$tempDir);
  22. }
  23. /**
  24. * @afterClass
  25. */
  26. public static function tearDownTempDir()
  27. {
  28. $files = new Filesystem;
  29. $files->deleteDirectory(self::$tempDir);
  30. self::$tempDir = null;
  31. }
  32. protected function tearDown(): void
  33. {
  34. m::close();
  35. $files = new Filesystem;
  36. $files->deleteDirectory(self::$tempDir, $preserve = true);
  37. }
  38. public function testGetRetrievesFiles()
  39. {
  40. file_put_contents(self::$tempDir.'/file.txt', 'Hello World');
  41. $files = new Filesystem;
  42. $this->assertSame('Hello World', $files->get(self::$tempDir.'/file.txt'));
  43. }
  44. public function testPutStoresFiles()
  45. {
  46. $files = new Filesystem;
  47. $files->put(self::$tempDir.'/file.txt', 'Hello World');
  48. $this->assertStringEqualsFile(self::$tempDir.'/file.txt', 'Hello World');
  49. }
  50. public function testLines()
  51. {
  52. $path = self::$tempDir.'/file.txt';
  53. $contents = LazyCollection::times(3)
  54. ->map(function ($number) {
  55. return "line-{$number}";
  56. })
  57. ->join("\n");
  58. file_put_contents($path, $contents);
  59. $files = new Filesystem;
  60. $this->assertInstanceOf(LazyCollection::class, $files->lines($path));
  61. $this->assertSame(
  62. ['line-1', 'line-2', 'line-3'],
  63. $files->lines($path)->all()
  64. );
  65. }
  66. public function testReplaceCreatesFile()
  67. {
  68. $tempFile = self::$tempDir.'/file.txt';
  69. $filesystem = new Filesystem;
  70. $filesystem->replace($tempFile, 'Hello World');
  71. $this->assertStringEqualsFile($tempFile, 'Hello World');
  72. }
  73. public function testReplaceInFileCorrectlyReplaces()
  74. {
  75. $tempFile = self::$tempDir.'/file.txt';
  76. $filesystem = new Filesystem;
  77. $filesystem->put($tempFile, 'Hello World');
  78. $filesystem->replaceInFile('Hello World', 'Hello Taylor', $tempFile);
  79. $this->assertStringEqualsFile($tempFile, 'Hello Taylor');
  80. }
  81. /**
  82. * @requires OS Linux|Darwin
  83. */
  84. public function testReplaceWhenUnixSymlinkExists()
  85. {
  86. $tempFile = self::$tempDir.'/file.txt';
  87. $symlinkDir = self::$tempDir.'/symlink_dir';
  88. $symlink = "{$symlinkDir}/symlink.txt";
  89. mkdir($symlinkDir);
  90. symlink($tempFile, $symlink);
  91. // Prevent changes to symlink_dir
  92. chmod($symlinkDir, 0555);
  93. // Test with a weird non-standard umask.
  94. $umask = 0131;
  95. $originalUmask = umask($umask);
  96. $filesystem = new Filesystem;
  97. // Test replacing non-existent file.
  98. $filesystem->replace($tempFile, 'Hello World');
  99. $this->assertStringEqualsFile($tempFile, 'Hello World');
  100. $this->assertEquals($umask, 0777 - $this->getFilePermissions($tempFile));
  101. // Test replacing existing file.
  102. $filesystem->replace($tempFile, 'Something Else');
  103. $this->assertStringEqualsFile($tempFile, 'Something Else');
  104. $this->assertEquals($umask, 0777 - $this->getFilePermissions($tempFile));
  105. // Test replacing symlinked file.
  106. $filesystem->replace($symlink, 'Yet Something Else Again');
  107. $this->assertStringEqualsFile($tempFile, 'Yet Something Else Again');
  108. $this->assertEquals($umask, 0777 - $this->getFilePermissions($tempFile));
  109. umask($originalUmask);
  110. // Reset changes to symlink_dir
  111. chmod($symlinkDir, 0777 - $originalUmask);
  112. }
  113. public function testSetChmod()
  114. {
  115. file_put_contents(self::$tempDir.'/file.txt', 'Hello World');
  116. $files = new Filesystem;
  117. $files->chmod(self::$tempDir.'/file.txt', 0755);
  118. $filePermission = substr(sprintf('%o', fileperms(self::$tempDir.'/file.txt')), -4);
  119. $expectedPermissions = DIRECTORY_SEPARATOR === '\\' ? '0666' : '0755';
  120. $this->assertEquals($expectedPermissions, $filePermission);
  121. }
  122. public function testGetChmod()
  123. {
  124. file_put_contents(self::$tempDir.'/file.txt', 'Hello World');
  125. chmod(self::$tempDir.'/file.txt', 0755);
  126. $files = new Filesystem;
  127. $filePermission = $files->chmod(self::$tempDir.'/file.txt');
  128. $expectedPermissions = DIRECTORY_SEPARATOR === '\\' ? '0666' : '0755';
  129. $this->assertEquals($expectedPermissions, $filePermission);
  130. }
  131. public function testDeleteRemovesFiles()
  132. {
  133. file_put_contents(self::$tempDir.'/file1.txt', 'Hello World');
  134. file_put_contents(self::$tempDir.'/file2.txt', 'Hello World');
  135. file_put_contents(self::$tempDir.'/file3.txt', 'Hello World');
  136. $files = new Filesystem;
  137. $files->delete(self::$tempDir.'/file1.txt');
  138. Assert::assertFileDoesNotExist(self::$tempDir.'/file1.txt');
  139. $files->delete([self::$tempDir.'/file2.txt', self::$tempDir.'/file3.txt']);
  140. Assert::assertFileDoesNotExist(self::$tempDir.'/file2.txt');
  141. Assert::assertFileDoesNotExist(self::$tempDir.'/file3.txt');
  142. }
  143. public function testPrependExistingFiles()
  144. {
  145. $files = new Filesystem;
  146. $files->put(self::$tempDir.'/file.txt', 'World');
  147. $files->prepend(self::$tempDir.'/file.txt', 'Hello ');
  148. $this->assertStringEqualsFile(self::$tempDir.'/file.txt', 'Hello World');
  149. }
  150. public function testPrependNewFiles()
  151. {
  152. $files = new Filesystem;
  153. $files->prepend(self::$tempDir.'/file.txt', 'Hello World');
  154. $this->assertStringEqualsFile(self::$tempDir.'/file.txt', 'Hello World');
  155. }
  156. public function testMissingFile()
  157. {
  158. $files = new Filesystem;
  159. $this->assertTrue($files->missing(self::$tempDir.'/file.txt'));
  160. }
  161. public function testDeleteDirectory()
  162. {
  163. mkdir(self::$tempDir.'/foo');
  164. file_put_contents(self::$tempDir.'/foo/file.txt', 'Hello World');
  165. $files = new Filesystem;
  166. $files->deleteDirectory(self::$tempDir.'/foo');
  167. Assert::assertDirectoryDoesNotExist(self::$tempDir.'/foo');
  168. Assert::assertFileDoesNotExist(self::$tempDir.'/foo/file.txt');
  169. }
  170. public function testDeleteDirectoryReturnFalseWhenNotADirectory()
  171. {
  172. mkdir(self::$tempDir.'/bar');
  173. file_put_contents(self::$tempDir.'/bar/file.txt', 'Hello World');
  174. $files = new Filesystem;
  175. $this->assertFalse($files->deleteDirectory(self::$tempDir.'/bar/file.txt'));
  176. }
  177. public function testCleanDirectory()
  178. {
  179. mkdir(self::$tempDir.'/baz');
  180. file_put_contents(self::$tempDir.'/baz/file.txt', 'Hello World');
  181. $files = new Filesystem;
  182. $files->cleanDirectory(self::$tempDir.'/baz');
  183. $this->assertDirectoryExists(self::$tempDir.'/baz');
  184. Assert::assertFileDoesNotExist(self::$tempDir.'/baz/file.txt');
  185. }
  186. public function testMacro()
  187. {
  188. file_put_contents(self::$tempDir.'/foo.txt', 'Hello World');
  189. $files = new Filesystem;
  190. $tempDir = self::$tempDir;
  191. $files->macro('getFoo', function () use ($files, $tempDir) {
  192. return $files->get($tempDir.'/foo.txt');
  193. });
  194. $this->assertSame('Hello World', $files->getFoo());
  195. }
  196. public function testFilesMethod()
  197. {
  198. mkdir(self::$tempDir.'/views');
  199. file_put_contents(self::$tempDir.'/views/1.txt', '1');
  200. file_put_contents(self::$tempDir.'/views/2.txt', '2');
  201. mkdir(self::$tempDir.'/views/_layouts');
  202. $files = new Filesystem;
  203. $results = $files->files(self::$tempDir.'/views');
  204. $this->assertInstanceOf(SplFileInfo::class, $results[0]);
  205. $this->assertInstanceOf(SplFileInfo::class, $results[1]);
  206. unset($files);
  207. }
  208. public function testCopyDirectoryReturnsFalseIfSourceIsntDirectory()
  209. {
  210. $files = new Filesystem;
  211. $this->assertFalse($files->copyDirectory(self::$tempDir.'/breeze/boom/foo/bar/baz', self::$tempDir));
  212. }
  213. public function testCopyDirectoryMovesEntireDirectory()
  214. {
  215. mkdir(self::$tempDir.'/tmp', 0777, true);
  216. file_put_contents(self::$tempDir.'/tmp/foo.txt', '');
  217. file_put_contents(self::$tempDir.'/tmp/bar.txt', '');
  218. mkdir(self::$tempDir.'/tmp/nested', 0777, true);
  219. file_put_contents(self::$tempDir.'/tmp/nested/baz.txt', '');
  220. $files = new Filesystem;
  221. $files->copyDirectory(self::$tempDir.'/tmp', self::$tempDir.'/tmp2');
  222. $this->assertDirectoryExists(self::$tempDir.'/tmp2');
  223. $this->assertFileExists(self::$tempDir.'/tmp2/foo.txt');
  224. $this->assertFileExists(self::$tempDir.'/tmp2/bar.txt');
  225. $this->assertDirectoryExists(self::$tempDir.'/tmp2/nested');
  226. $this->assertFileExists(self::$tempDir.'/tmp2/nested/baz.txt');
  227. }
  228. public function testMoveDirectoryMovesEntireDirectory()
  229. {
  230. mkdir(self::$tempDir.'/tmp2', 0777, true);
  231. file_put_contents(self::$tempDir.'/tmp2/foo.txt', '');
  232. file_put_contents(self::$tempDir.'/tmp2/bar.txt', '');
  233. mkdir(self::$tempDir.'/tmp2/nested', 0777, true);
  234. file_put_contents(self::$tempDir.'/tmp2/nested/baz.txt', '');
  235. $files = new Filesystem;
  236. $files->moveDirectory(self::$tempDir.'/tmp2', self::$tempDir.'/tmp3');
  237. $this->assertDirectoryExists(self::$tempDir.'/tmp3');
  238. $this->assertFileExists(self::$tempDir.'/tmp3/foo.txt');
  239. $this->assertFileExists(self::$tempDir.'/tmp3/bar.txt');
  240. $this->assertDirectoryExists(self::$tempDir.'/tmp3/nested');
  241. $this->assertFileExists(self::$tempDir.'/tmp3/nested/baz.txt');
  242. Assert::assertDirectoryDoesNotExist(self::$tempDir.'/tmp2');
  243. }
  244. public function testMoveDirectoryMovesEntireDirectoryAndOverwrites()
  245. {
  246. mkdir(self::$tempDir.'/tmp4', 0777, true);
  247. file_put_contents(self::$tempDir.'/tmp4/foo.txt', '');
  248. file_put_contents(self::$tempDir.'/tmp4/bar.txt', '');
  249. mkdir(self::$tempDir.'/tmp4/nested', 0777, true);
  250. file_put_contents(self::$tempDir.'/tmp4/nested/baz.txt', '');
  251. mkdir(self::$tempDir.'/tmp5', 0777, true);
  252. file_put_contents(self::$tempDir.'/tmp5/foo2.txt', '');
  253. file_put_contents(self::$tempDir.'/tmp5/bar2.txt', '');
  254. $files = new Filesystem;
  255. $files->moveDirectory(self::$tempDir.'/tmp4', self::$tempDir.'/tmp5', true);
  256. $this->assertDirectoryExists(self::$tempDir.'/tmp5');
  257. $this->assertFileExists(self::$tempDir.'/tmp5/foo.txt');
  258. $this->assertFileExists(self::$tempDir.'/tmp5/bar.txt');
  259. $this->assertDirectoryExists(self::$tempDir.'/tmp5/nested');
  260. $this->assertFileExists(self::$tempDir.'/tmp5/nested/baz.txt');
  261. Assert::assertFileDoesNotExist(self::$tempDir.'/tmp5/foo2.txt');
  262. Assert::assertFileDoesNotExist(self::$tempDir.'/tmp5/bar2.txt');
  263. Assert::assertDirectoryDoesNotExist(self::$tempDir.'/tmp4');
  264. }
  265. public function testMoveDirectoryReturnsFalseWhileOverwritingAndUnableToDeleteDestinationDirectory()
  266. {
  267. mkdir(self::$tempDir.'/tmp6', 0777, true);
  268. file_put_contents(self::$tempDir.'/tmp6/foo.txt', '');
  269. mkdir(self::$tempDir.'/tmp7', 0777, true);
  270. $files = m::mock(Filesystem::class)->makePartial();
  271. $files->shouldReceive('deleteDirectory')->once()->andReturn(false);
  272. $this->assertFalse($files->moveDirectory(self::$tempDir.'/tmp6', self::$tempDir.'/tmp7', true));
  273. }
  274. public function testGetThrowsExceptionNonexisitingFile()
  275. {
  276. $this->expectException(FileNotFoundException::class);
  277. $files = new Filesystem;
  278. $files->get(self::$tempDir.'/unknown-file.txt');
  279. }
  280. public function testGetRequireReturnsProperly()
  281. {
  282. file_put_contents(self::$tempDir.'/file.php', '<?php return "Howdy?"; ?>');
  283. $files = new Filesystem;
  284. $this->assertSame('Howdy?', $files->getRequire(self::$tempDir.'/file.php'));
  285. }
  286. public function testGetRequireThrowsExceptionNonExistingFile()
  287. {
  288. $this->expectException(FileNotFoundException::class);
  289. $files = new Filesystem;
  290. $files->getRequire(self::$tempDir.'/file.php');
  291. }
  292. public function testAppendAddsDataToFile()
  293. {
  294. file_put_contents(self::$tempDir.'/file.txt', 'foo');
  295. $files = new Filesystem;
  296. $bytesWritten = $files->append(self::$tempDir.'/file.txt', 'bar');
  297. $this->assertEquals(mb_strlen('bar', '8bit'), $bytesWritten);
  298. $this->assertFileExists(self::$tempDir.'/file.txt');
  299. $this->assertStringEqualsFile(self::$tempDir.'/file.txt', 'foobar');
  300. }
  301. public function testMoveMovesFiles()
  302. {
  303. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  304. $files = new Filesystem;
  305. $files->move(self::$tempDir.'/foo.txt', self::$tempDir.'/bar.txt');
  306. $this->assertFileExists(self::$tempDir.'/bar.txt');
  307. Assert::assertFileDoesNotExist(self::$tempDir.'/foo.txt');
  308. }
  309. public function testNameReturnsName()
  310. {
  311. file_put_contents(self::$tempDir.'/foobar.txt', 'foo');
  312. $filesystem = new Filesystem;
  313. $this->assertSame('foobar', $filesystem->name(self::$tempDir.'/foobar.txt'));
  314. }
  315. public function testExtensionReturnsExtension()
  316. {
  317. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  318. $files = new Filesystem;
  319. $this->assertSame('txt', $files->extension(self::$tempDir.'/foo.txt'));
  320. }
  321. public function testBasenameReturnsBasename()
  322. {
  323. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  324. $files = new Filesystem;
  325. $this->assertSame('foo.txt', $files->basename(self::$tempDir.'/foo.txt'));
  326. }
  327. public function testDirnameReturnsDirectory()
  328. {
  329. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  330. $files = new Filesystem;
  331. $this->assertEquals(self::$tempDir, $files->dirname(self::$tempDir.'/foo.txt'));
  332. }
  333. public function testTypeIdentifiesFile()
  334. {
  335. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  336. $files = new Filesystem;
  337. $this->assertSame('file', $files->type(self::$tempDir.'/foo.txt'));
  338. }
  339. public function testTypeIdentifiesDirectory()
  340. {
  341. mkdir(self::$tempDir.'/foo-dir');
  342. $files = new Filesystem;
  343. $this->assertSame('dir', $files->type(self::$tempDir.'/foo-dir'));
  344. }
  345. public function testSizeOutputsSize()
  346. {
  347. $size = file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  348. $files = new Filesystem;
  349. $this->assertEquals($size, $files->size(self::$tempDir.'/foo.txt'));
  350. }
  351. /**
  352. * @requires extension fileinfo
  353. */
  354. public function testMimeTypeOutputsMimeType()
  355. {
  356. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  357. $files = new Filesystem;
  358. $this->assertSame('text/plain', $files->mimeType(self::$tempDir.'/foo.txt'));
  359. }
  360. public function testIsWritable()
  361. {
  362. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  363. $files = new Filesystem;
  364. @chmod(self::$tempDir.'/foo.txt', 0444);
  365. $this->assertFalse($files->isWritable(self::$tempDir.'/foo.txt'));
  366. @chmod(self::$tempDir.'/foo.txt', 0777);
  367. $this->assertTrue($files->isWritable(self::$tempDir.'/foo.txt'));
  368. }
  369. public function testIsReadable()
  370. {
  371. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  372. $files = new Filesystem;
  373. // chmod is noneffective on Windows
  374. if (DIRECTORY_SEPARATOR === '\\') {
  375. $this->assertTrue($files->isReadable(self::$tempDir.'/foo.txt'));
  376. } else {
  377. @chmod(self::$tempDir.'/foo.txt', 0000);
  378. $this->assertFalse($files->isReadable(self::$tempDir.'/foo.txt'));
  379. @chmod(self::$tempDir.'/foo.txt', 0777);
  380. $this->assertTrue($files->isReadable(self::$tempDir.'/foo.txt'));
  381. }
  382. $this->assertFalse($files->isReadable(self::$tempDir.'/doesnotexist.txt'));
  383. }
  384. public function testGlobFindsFiles()
  385. {
  386. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  387. file_put_contents(self::$tempDir.'/bar.txt', 'bar');
  388. $files = new Filesystem;
  389. $glob = $files->glob(self::$tempDir.'/*.txt');
  390. $this->assertContains(self::$tempDir.'/foo.txt', $glob);
  391. $this->assertContains(self::$tempDir.'/bar.txt', $glob);
  392. }
  393. public function testAllFilesFindsFiles()
  394. {
  395. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  396. file_put_contents(self::$tempDir.'/bar.txt', 'bar');
  397. $files = new Filesystem;
  398. $allFiles = [];
  399. foreach ($files->allFiles(self::$tempDir) as $file) {
  400. $allFiles[] = $file->getFilename();
  401. }
  402. $this->assertContains('foo.txt', $allFiles);
  403. $this->assertContains('bar.txt', $allFiles);
  404. }
  405. public function testDirectoriesFindsDirectories()
  406. {
  407. mkdir(self::$tempDir.'/film');
  408. mkdir(self::$tempDir.'/music');
  409. $files = new Filesystem;
  410. $directories = $files->directories(self::$tempDir);
  411. $this->assertContains(self::$tempDir.DIRECTORY_SEPARATOR.'film', $directories);
  412. $this->assertContains(self::$tempDir.DIRECTORY_SEPARATOR.'music', $directories);
  413. }
  414. public function testMakeDirectory()
  415. {
  416. $files = new Filesystem;
  417. $this->assertTrue($files->makeDirectory(self::$tempDir.'/created'));
  418. $this->assertFileExists(self::$tempDir.'/created');
  419. }
  420. /**
  421. * @requires extension pcntl
  422. * @requires OS Linux|Darwin
  423. */
  424. public function testSharedGet()
  425. {
  426. $content = str_repeat('123456', 1000000);
  427. $result = 1;
  428. posix_setpgid(0, 0);
  429. for ($i = 1; $i <= 20; $i++) {
  430. $pid = pcntl_fork();
  431. if (! $pid) {
  432. $files = new Filesystem;
  433. $files->put(self::$tempDir.'/file.txt', $content, true);
  434. $read = $files->get(self::$tempDir.'/file.txt', true);
  435. exit(strlen($read) === strlen($content) ? 1 : 0);
  436. }
  437. }
  438. while (pcntl_waitpid(0, $status) != -1) {
  439. $status = pcntl_wexitstatus($status);
  440. $result *= $status;
  441. }
  442. $this->assertSame(1, $result);
  443. }
  444. public function testRequireOnceRequiresFileProperly()
  445. {
  446. $filesystem = new Filesystem;
  447. mkdir(self::$tempDir.'/scripts');
  448. file_put_contents(self::$tempDir.'/scripts/foo.php', '<?php function random_function_xyz(){};');
  449. $filesystem->requireOnce(self::$tempDir.'/scripts/foo.php');
  450. file_put_contents(self::$tempDir.'/scripts/foo.php', '<?php function random_function_xyz_changed(){};');
  451. $filesystem->requireOnce(self::$tempDir.'/scripts/foo.php');
  452. $this->assertTrue(function_exists('random_function_xyz'));
  453. $this->assertFalse(function_exists('random_function_xyz_changed'));
  454. }
  455. public function testCopyCopiesFileProperly()
  456. {
  457. $filesystem = new Filesystem;
  458. $data = 'contents';
  459. mkdir(self::$tempDir.'/text');
  460. file_put_contents(self::$tempDir.'/text/foo.txt', $data);
  461. $filesystem->copy(self::$tempDir.'/text/foo.txt', self::$tempDir.'/text/foo2.txt');
  462. $this->assertFileExists(self::$tempDir.'/text/foo2.txt');
  463. $this->assertEquals($data, file_get_contents(self::$tempDir.'/text/foo2.txt'));
  464. }
  465. public function testIsFileChecksFilesProperly()
  466. {
  467. $filesystem = new Filesystem;
  468. mkdir(self::$tempDir.'/help');
  469. file_put_contents(self::$tempDir.'/help/foo.txt', 'contents');
  470. $this->assertTrue($filesystem->isFile(self::$tempDir.'/help/foo.txt'));
  471. $this->assertFalse($filesystem->isFile(self::$tempDir.'./help'));
  472. }
  473. public function testFilesMethodReturnsFileInfoObjects()
  474. {
  475. mkdir(self::$tempDir.'/objects');
  476. file_put_contents(self::$tempDir.'/objects/1.txt', '1');
  477. file_put_contents(self::$tempDir.'/objects/2.txt', '2');
  478. mkdir(self::$tempDir.'/objects/bar');
  479. $files = new Filesystem;
  480. $this->assertContainsOnlyInstancesOf(SplFileInfo::class, $files->files(self::$tempDir.'/objects'));
  481. unset($files);
  482. }
  483. public function testAllFilesReturnsFileInfoObjects()
  484. {
  485. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  486. file_put_contents(self::$tempDir.'/bar.txt', 'bar');
  487. $files = new Filesystem;
  488. $this->assertContainsOnlyInstancesOf(SplFileInfo::class, $files->allFiles(self::$tempDir));
  489. }
  490. /**
  491. * @requires extension ftp
  492. */
  493. public function testCreateFtpDriver()
  494. {
  495. $filesystem = new FilesystemManager(new Application);
  496. $driver = $filesystem->createFtpDriver([
  497. 'host' => 'ftp.example.com',
  498. 'username' => 'admin',
  499. 'permPublic' => 0700,
  500. 'unsupportedParam' => true,
  501. ]);
  502. /** @var \League\Flysystem\Adapter\Ftp $adapter */
  503. $adapter = $driver->getAdapter();
  504. $this->assertEquals(0700, $adapter->getPermPublic());
  505. $this->assertSame('ftp.example.com', $adapter->getHost());
  506. $this->assertSame('admin', $adapter->getUsername());
  507. }
  508. public function testHash()
  509. {
  510. file_put_contents(self::$tempDir.'/foo.txt', 'foo');
  511. $filesystem = new Filesystem;
  512. $this->assertSame('acbd18db4cc2f85cedef654fccc4a4d8', $filesystem->hash(self::$tempDir.'/foo.txt'));
  513. }
  514. /**
  515. * @param string $file
  516. * @return int
  517. */
  518. private function getFilePermissions($file)
  519. {
  520. $filePerms = fileperms($file);
  521. $filePerms = substr(sprintf('%o', $filePerms), -3);
  522. return (int) base_convert($filePerms, 8, 10);
  523. }
  524. }