From e3f60f663eb60a8beb3d15f1d2be776cbca702b9 Mon Sep 17 00:00:00 2001 From: adlawson Date: Sat, 6 Sep 2014 14:43:43 +0100 Subject: [PATCH] Cover use of `file_get_contents` with a directory --- src/Stream/FileHandle.php | 2 ++ .../StreamWrapper/FileGetContentsAcceptanceTest.php | 5 +++++ test/unit/Stream/FileHandleTest.php | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/Stream/FileHandle.php b/src/Stream/FileHandle.php index a74dc2d..11b33a3 100644 --- a/src/Stream/FileHandle.php +++ b/src/Stream/FileHandle.php @@ -78,6 +78,8 @@ class FileHandle extends AbstractHandle { if (!$this->node) { throw new UnopenedHandleException($this, $this->url); + } elseif (!$this->node instanceof FileInterface) { + return ''; } if (null !== $length) { diff --git a/test/acceptance/Stream/StreamWrapper/FileGetContentsAcceptanceTest.php b/test/acceptance/Stream/StreamWrapper/FileGetContentsAcceptanceTest.php index d4c565d..72d55a7 100644 --- a/test/acceptance/Stream/StreamWrapper/FileGetContentsAcceptanceTest.php +++ b/test/acceptance/Stream/StreamWrapper/FileGetContentsAcceptanceTest.php @@ -11,6 +11,11 @@ class FileGetContentsAcceptanceTest extends AcceptanceTestCase ] ]; + public function testGetDirectory() + { + $this->assertEquals('', file_get_contents("$this->scheme://foo")); + } + public function testGetFile() { $this->assertEquals($this->tree['foo']['bar'], file_get_contents("$this->scheme://foo/bar")); diff --git a/test/unit/Stream/FileHandleTest.php b/test/unit/Stream/FileHandleTest.php index 33e79f5..fe8cf6d 100644 --- a/test/unit/Stream/FileHandleTest.php +++ b/test/unit/Stream/FileHandleTest.php @@ -219,6 +219,18 @@ class FileHandleTest extends UnitTestCase $this->assertEquals($expected, $handle->read($offset, $length)); } + public function testReadNonFile() + { + $handle = new FileHandle($this->fs, 'foo://bar'); + + $file = Mockery::mock('Vfs\Node\NodeInterface'); + + $this->fs->shouldReceive('get')->once()->with('/bar')->andReturn($file); + + $handle->open(); + $this->assertEquals('', $handle->read(0, PHP_INT_MAX)); + } + public function testReadThrowsWithoutOpening() { $handle = new FileHandle($this->fs, 'foo://bar');