Skip to content

Commit

Permalink
Merge a8846bf into 1a30e21
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Jan 7, 2016
2 parents 1a30e21 + a8846bf commit 7846cf2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -36,6 +36,10 @@ FileFetcher.php.

## Release notes

### 3.1.0 (2016-01-07)

* Added `InMemoryFileFetcher`

### 3.0.0 (2015-08-21)

* Added `FileFetchingException`, which should now be thrown by implementations of `FileFetcher` on error
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -24,7 +24,7 @@
"jeroen/simple-cache": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "~5.0",
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~2.1",
"phpmd/phpmd": "~2.1",
"ockcyp/covers-validator": "~0.4"
Expand All @@ -36,7 +36,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
"dev-master": "3.1.x-dev"
}
},
"scripts": {
Expand Down
45 changes: 45 additions & 0 deletions src/InMemoryFileFetcher.php
@@ -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 );
}

}
40 changes: 40 additions & 0 deletions tests/unit/InMemoryFileFetcherTest.php
@@ -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' ) );
}

}

0 comments on commit 7846cf2

Please sign in to comment.