Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace FileFetcher; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* @since 3.1 | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
class InMemoryFileFetcher implements FileFetcher { | ||
|
||
/** | ||
* @param string[] $files | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct( array $files ) { | ||
foreach ( $files as $url => $fileContents ) { | ||
if ( !is_string( $url ) || !is_string( $fileContents ) ) { | ||
throw new InvalidArgumentException( 'Both file url and file contents need to be of type string' ); | ||
} | ||
} | ||
|
||
$this->files = $files; | ||
} | ||
|
||
/** | ||
* @see FileFetcher::fetchFile | ||
* | ||
* @param string $fileUrl | ||
* | ||
* @return string | ||
* @throws FileFetchingException | ||
*/ | ||
public function fetchFile( $fileUrl ) { | ||
if ( array_key_exists( $fileUrl, $this->files ) ) { | ||
return $this->files[$fileUrl]; | ||
} | ||
|
||
throw new FileFetchingException( $fileUrl ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace FileFetcher\Tests\Phpunit; | ||
|
||
use FileFetcher\InMemoryFileFetcher; | ||
|
||
/** | ||
* @covers FileFetcher\InMemoryFileFetcher | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
class InMemoryFileFetcherTest extends \PHPUnit_Framework_TestCase { | ||
|
||
public function testWhenEmptyHash_requestsCauseException() { | ||
$fetcher = new InMemoryFileFetcher( array() ); | ||
|
||
$this->setExpectedException( 'FileFetcher\FileFetchingException' ); | ||
$fetcher->fetchFile( 'http://foo.bar/baz' ); | ||
} | ||
|
||
public function testWhenUrlNotKnown_requestsCauseException() { | ||
$fetcher = new InMemoryFileFetcher( array( | ||
'http://something.else/entirely' => 'kittens' | ||
) ); | ||
|
||
$this->setExpectedException( 'FileFetcher\FileFetchingException' ); | ||
$fetcher->fetchFile( 'http://foo.bar/baz' ); | ||
} | ||
|
||
public function testWhenUrlKnown_requestsReturnsValue() { | ||
$fetcher = new InMemoryFileFetcher( array( | ||
'http://something.else/entirely' => 'kittens', | ||
'http://foo.bar/baz' => 'cats' | ||
) ); | ||
|
||
$this->assertSame( 'cats', $fetcher->fetchFile( 'http://foo.bar/baz' ) ); | ||
} | ||
|
||
} |