Skip to content

Commit

Permalink
Prevent passing invalid resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Aug 23, 2020
1 parent 88cb954 commit f66f0e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function write($path, $contents, array $config = [])
*/
public function writeStream($path, $resource, array $config = [])
{
if ( ! is_resource($resource)) {
if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') {
throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public function put($path, $contents, array $config = [])
*/
public function putStream($path, $resource, array $config = [])
{
if ( ! is_resource($resource)) {
if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') {
throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
}

Expand Down Expand Up @@ -158,7 +158,7 @@ public function update($path, $contents, array $config = [])
*/
public function updateStream($path, $resource, array $config = [])
{
if ( ! is_resource($resource)) {
if ( ! is_resource($resource) || get_resource_type($resource) !== 'stream') {
throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
}

Expand Down
20 changes: 20 additions & 0 deletions tests/FilesystemTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ public function testPutStreamInvalid()
$this->filesystem->putStream('path.txt', '__INVALID__');
}

/**
* @dataProvider methodsThatGuardAgainstClosedResources
*/
public function testSupplyingClosedStreams($method)
{
$handle = tmpfile();
fclose($handle);
$this->expectException('InvalidArgumentException');
$this->filesystem->{$method}('path.txt', $handle);
}

public function methodsThatGuardAgainstClosedResources()
{
return [
['putStream'],
['writeStream'],
['updateStream'],
];
}

public function testWriteStreamInvalid()
{
$this->expectException('InvalidArgumentException');
Expand Down

0 comments on commit f66f0e5

Please sign in to comment.