Skip to content

Commit be6917c

Browse files
authored
Merge pull request #79 from joomla-framework/3.x/feature/exists
Backport File:exists and Folder:exists from CMS
2 parents 556e740 + d515c8b commit be6917c

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

Tests/FileTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,35 @@ public function testUploadToNestedDirectory()
564564
File::upload($this->testPath . '/' . $name . '.txt', $this->testPath . '/' . $name . '/' . $uploadedFileName)
565565
);
566566
}
567+
568+
/**
569+
* Test exists method.
570+
*/
571+
public function testExistsForExistingFile()
572+
{
573+
$name = 'tempFile';
574+
$data = 'Lorem ipsum dolor sit amet';
575+
576+
if (!File::write($this->testPath . '/' . $name, $data)) {
577+
$this->markTestSkipped('The test file could not be created.');
578+
}
579+
580+
$this->assertTrue(
581+
File::exists($this->testPath . '/' . $name),
582+
'The file exists.'
583+
);
584+
}
585+
586+
/**
587+
* Test exists method.
588+
*/
589+
public function testExistsForNonexistingFile()
590+
{
591+
$name = 'nonExistingTempFile';
592+
593+
$this->assertFalse(
594+
File::exists($this->testPath . '/' . $name),
595+
'The file does not exists.'
596+
);
597+
}
567598
}

Tests/FolderTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,4 +921,35 @@ public function testMakeSafe()
921921
Folder::makeSafe('test1/testdirectory')
922922
);
923923
}
924+
925+
926+
/**
927+
* Test exists method.
928+
*/
929+
public function testExistsForExistingFolder()
930+
{
931+
$name = 'tempFolder';
932+
933+
if (!Folder::create($this->testPath . '/' . $name)) {
934+
$this->markTestSkipped('The test directory could not be created.');
935+
}
936+
937+
$this->assertTrue(
938+
Folder::exists($this->testPath . '/' . $name),
939+
'The folder exists.'
940+
);
941+
}
942+
943+
/**
944+
* Test exists method.
945+
*/
946+
public function testExistsForNonexistingFolder()
947+
{
948+
$name = 'nonExistingTempFolder';
949+
950+
$this->assertFalse(
951+
Folder::exists($this->testPath . '/' . $name),
952+
'The folder does not exists.'
953+
);
954+
}
924955
}

src/File.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,17 @@ public static function invalidateFileCache($file)
372372
opcache_invalidate($file, true);
373373
}
374374
}
375+
376+
/**
377+
* Wrapper for the standard file_exists function
378+
*
379+
* @param string $file File path
380+
*
381+
* @return boolean True if path is a file
382+
*
383+
*/
384+
public static function exists(string $file): bool
385+
{
386+
return is_file(Path::clean($file));
387+
}
375388
}

src/Folder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,16 @@ public static function makeSafe($path)
547547

548548
return preg_replace($regex, '', $path);
549549
}
550+
551+
/**
552+
* Wrapper for the standard is_dir function
553+
*
554+
* @param string $path Folder path
555+
*
556+
* @return boolean True if path is a folder
557+
*/
558+
public static function exists(string $path): bool
559+
{
560+
return is_dir(Path::clean($path));
561+
}
550562
}

0 commit comments

Comments
 (0)