diff --git a/Tests/FileTest.php b/Tests/FileTest.php index 46efa0cd..34bd567e 100644 --- a/Tests/FileTest.php +++ b/Tests/FileTest.php @@ -564,4 +564,35 @@ public function testUploadToNestedDirectory() File::upload($this->testPath . '/' . $name . '.txt', $this->testPath . '/' . $name . '/' . $uploadedFileName) ); } + + /** + * Test exists method. + */ + public function testExistsForExistingFile() + { + $name = 'tempFile'; + $data = 'Lorem ipsum dolor sit amet'; + + if (!File::write($this->testPath . '/' . $name, $data)) { + $this->markTestSkipped('The test file could not be created.'); + } + + $this->assertTrue( + File::exists($this->testPath . '/' . $name), + 'The file exists.' + ); + } + + /** + * Test exists method. + */ + public function testExistsForNonexistingFile() + { + $name = 'nonExistingTempFile'; + + $this->assertFalse( + File::exists($this->testPath . '/' . $name), + 'The file does not exists.' + ); + } } diff --git a/Tests/FolderTest.php b/Tests/FolderTest.php index fb87c1f8..86ff91d2 100644 --- a/Tests/FolderTest.php +++ b/Tests/FolderTest.php @@ -921,4 +921,35 @@ public function testMakeSafe() Folder::makeSafe('test1/testdirectory') ); } + + + /** + * Test exists method. + */ + public function testExistsForExistingFolder() + { + $name = 'tempFolder'; + + if (!Folder::create($this->testPath . '/' . $name)) { + $this->markTestSkipped('The test directory could not be created.'); + } + + $this->assertTrue( + Folder::exists($this->testPath . '/' . $name), + 'The folder exists.' + ); + } + + /** + * Test exists method. + */ + public function testExistsForNonexistingFolder() + { + $name = 'nonExistingTempFolder'; + + $this->assertFalse( + Folder::exists($this->testPath . '/' . $name), + 'The folder does not exists.' + ); + } } diff --git a/src/File.php b/src/File.php index ca69baf4..796508bb 100644 --- a/src/File.php +++ b/src/File.php @@ -372,4 +372,17 @@ public static function invalidateFileCache($file) opcache_invalidate($file, true); } } + + /** + * Wrapper for the standard file_exists function + * + * @param string $file File path + * + * @return boolean True if path is a file + * + */ + public static function exists(string $file): bool + { + return is_file(Path::clean($file)); + } } diff --git a/src/Folder.php b/src/Folder.php index 1d943b1d..a147b025 100644 --- a/src/Folder.php +++ b/src/Folder.php @@ -547,4 +547,16 @@ public static function makeSafe($path) return preg_replace($regex, '', $path); } + + /** + * Wrapper for the standard is_dir function + * + * @param string $path Folder path + * + * @return boolean True if path is a folder + */ + public static function exists(string $path): bool + { + return is_dir(Path::clean($path)); + } }