Skip to content

Commit

Permalink
Finder: add read() & write() to FileInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 2, 2023
1 parent 58eb1e8 commit 39ce6e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Utils/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,24 @@ public function getRelativePathname(): string
return ($this->relativePath === '' ? '' : $this->relativePath . DIRECTORY_SEPARATOR)
. $this->getBasename();
}


/**
* Returns the contents of the file.
* @throws Nette\IOException
*/
public function read(): string
{
return FileSystem::read($this->getPathname());
}


/**
* Writes the contents to the file.
* @throws Nette\IOException
*/
public function write(string $content): void
{
FileSystem::write($this->getPathname(), $content);
}
}
13 changes: 13 additions & 0 deletions tests/Utils/Finder.fileInfo.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ test('relative path', function () {
Assert::same('subdir', $files[0]->getRelativePath());
Assert::same("subdir{$ds}readme", $files[0]->getRelativePathname());
});


test('read', function () {
$file = new FileInfo('fixtures.finder/file.txt');
Assert::true(str_starts_with($file->read(), 'File for testing purposes'));
});


test('write', function () {
$file = new FileInfo(getTempDir() . '/foo');
$file->write('foo');
Assert::same('foo', $file->read());
});

0 comments on commit 39ce6e5

Please sign in to comment.