Skip to content

Commit

Permalink
Test edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 13, 2024
1 parent bdc7036 commit 34bd942
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions packages/framework/tests/Unit/FilesystemFindFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hyde\Testing\UnitTestCase;
use Hyde\Testing\CreatesTemporaryFiles;
use Hyde\Foundation\Kernel\Filesystem;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;

/**
* @covers \Hyde\Foundation\Kernel\Filesystem::findFiles
Expand Down Expand Up @@ -74,6 +75,72 @@ public function testFindFilesReturnsCorrectFilesWhenUsingNestedSubdirectoriesOfD
$this->assertSameArray(['apple.md', 'nested/banana.md'], 'directory', 'md', true);
}

public function testFindFilesWithFilesHavingNoExtensions()
{
$this->files(['directory/file', 'directory/another_file']);
$this->assertSameArray(['file', 'another_file'], 'directory');
}

public function testFindFilesWithSpecialCharactersInNames()
{
$this->files(['directory/file-with-dash.md', 'directory/another_file.txt', 'directory/special@char!.blade.php']);
$this->assertSameArray(['file-with-dash.md', 'another_file.txt', 'special@char!.blade.php'], 'directory');
}

public function testFindFilesWithSpecialPrefixes()
{
$this->files(['directory/_file.md', 'directory/-another_file.txt', 'directory/~special_file.blade.php']);
$this->assertSameArray(['_file.md', '-another_file.txt', '~special_file.blade.php'], 'directory');
}

public function testFindFilesWithHiddenFiles()
{
$this->files(['directory/.hidden_file', 'directory/.another_hidden.md', 'directory/visible_file.md']);
$this->assertSameArray(['visible_file.md'], 'directory');
}

public function testFindFilesWithRecursiveAndHiddenFiles()
{
$this->files(['directory/.hidden_file', 'directory/nested/.another_hidden.md', 'directory/nested/visible_file.md']);
$this->assertSameArray(['nested/visible_file.md'], 'directory', false, true);
}

public function testFindFilesWithEmptyExtensionFilter()
{
$this->files(['directory/file.md', 'directory/another_file.txt']);
$this->assertSameArray([], 'directory', '');
}

public function testFindFilesWithCaseInsensitiveExtensions()
{
$this->files(['directory/file.MD', 'directory/another_file.md', 'directory/ignored.TXT']);
$this->assertSameArray(['another_file.md'], 'directory', 'md');
}

public function testFindFilesWithCaseInsensitiveFilenames()
{
$this->files(['directory/file.md', 'directory/anotherFile.md', 'directory/ANOTHER_FILE.md']);
$this->assertSameArray(['file.md', 'anotherFile.md', 'ANOTHER_FILE.md'], 'directory');
}

public function testFindFilesHandlesLargeNumberOfFiles()
{
$this->files(array_map(fn($i) => "directory/file$i.md", range(1, 100)));
$this->assertSameArray(array_map(fn($i) => "file$i.md", range(1, 100)), 'directory');
}

public function testFindFilesWithEmptyDirectory()
{
$this->directory('directory');
$this->assertSameArray([], 'directory');
}

public function testFindFilesWithNonExistentDirectory()
{
$this->expectException(DirectoryNotFoundException::class);
$this->assertSameArray([], 'nonexistent-directory');
}

protected function assertSameArray(array $expected, string $directory, string|false $matchExtension = false, bool $recursive = false): void
{
$files = (new Filesystem(Hyde::getInstance()))->findFiles($directory, $matchExtension, $recursive);
Expand Down

0 comments on commit 34bd942

Please sign in to comment.