Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ThrowingFileFetcher for readability #10

Merged
merged 1 commit into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ presumably an adapter to a library that focuses on this task such as [Guzzle](ht

## Usage

The library provides two trivial implementations of the `FileFetcher` interface at its heart:
The library provides some trivial implementations of the `FileFetcher` interface at its heart:

* `SimpleFileFetcher`: [Adapter](https://en.wikipedia.org/wiki/Adapter_pattern) around `file_get_contents`
* `InMemoryFileFetcher`: Adapter around an array provided to its constructor (construct with [] for a "throwing fetcher")
* `InMemoryFileFetcher`: Adapter around an array provided to its constructor
* `ThrowingFileFetcher`: Throws a `FileFetchingException` for all calls
* `NullFileFetcher`: Returns an empty string for all calls

It also provides a number of generic [decorators](https://en.wikipedia.org/wiki/Decorator_pattern):

Expand Down Expand Up @@ -58,6 +60,11 @@ For a full CI run

## Release notes

### 4.1.0 (2017-05-11)

* Added `ThrowingFileFetcher`
* Added `NullFileFetcher`

### 4.0.0 (2017-05-09)

Breaking changes:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.0.x-dev"
"dev-master": "4.1.x-dev"
}
},
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/ErrorLoggingFileFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/**
* @license GNU GPL v2+
* @author Gabriel Birke < gabriel.birke@wikimedia.de >
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ErrorLoggingFileFetcher implements FileFetcher {

Expand Down
19 changes: 19 additions & 0 deletions src/NullFileFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare( strict_types=1 );

namespace FileFetcher;

/**
* @since 4.1
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class NullFileFetcher implements FileFetcher {

public function fetchFile( string $fileUrl ): string {
return '';
}

}
19 changes: 19 additions & 0 deletions src/ThrowingFileFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare( strict_types=1 );

namespace FileFetcher;

/**
* @since 4.1
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ThrowingFileFetcher extends InMemoryFileFetcher {

public function __construct() {
parent::__construct( [] );
}

}
14 changes: 8 additions & 6 deletions tests/unit/ErrorLoggingFileFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FileFetcher\ErrorLoggingFileFetcher;
use FileFetcher\FileFetchingException;
use FileFetcher\InMemoryFileFetcher;
use FileFetcher\ThrowingFileFetcher;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
Expand All @@ -15,10 +16,11 @@
/**
* @license GNU GPL v2+
* @author Gabriel Birke < gabriel.birke@wikimedia.de >
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ErrorLoggingFileFetcherTest extends TestCase {

public function testGivenSucceedingFileFetcher_itsContentIsReturned() {
public function testWhenWrappedFetcherReturnsValue_itIsReturned() {
$logger = new LoggerSpy();
$fileFetcher = new ErrorLoggingFileFetcher(
new InMemoryFileFetcher( [ 'song.txt' => 'I\'m a little teapot' ] ),
Expand All @@ -28,19 +30,19 @@ public function testGivenSucceedingFileFetcher_itsContentIsReturned() {
$logger->assertNoLoggingCallsWhereMade();
}

public function testGivenFailingFileFetcher_anExceptionIsThrown() {
public function testWhenWrappedFetcherThrowsAnException_itIsRethrown() {
$errorLoggingFileFetcher = new ErrorLoggingFileFetcher(
new InMemoryFileFetcher( [] ),
new ThrowingFileFetcher(),
new NullLogger()
);
$this->expectException( FileFetchingException::class );
$errorLoggingFileFetcher->fetchFile( 'song.txt' );
}

public function testGivenFailingFileFetcher_theExceptionIsLogged() {
public function testWhenWrappedFetcherThrowsAnException_theExceptionIsLogged() {
$logger = new LoggerSpy();
$fileFetcher = new ErrorLoggingFileFetcher(
new InMemoryFileFetcher( [] ),
new ThrowingFileFetcher(),
$logger
);

Expand All @@ -61,7 +63,7 @@ public function testGivenFailingFileFetcher_theExceptionIsLogged() {
public function testGivenLogLevel_exceptionsAreLoggedAtThisLevel() {
$logger = new LoggerSpy();
$fileFetcher = new ErrorLoggingFileFetcher(
new InMemoryFileFetcher( [] ),
new ThrowingFileFetcher(),
$logger,
LogLevel::CRITICAL
);
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/InMemoryFileFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@
class InMemoryFileFetcherTest extends TestCase {

public function testWhenEmptyHash_requestsCauseException() {
$fetcher = new InMemoryFileFetcher( array() );
$fetcher = new InMemoryFileFetcher( [] );

$this->expectException( FileFetchingException::class );
$fetcher->fetchFile( 'http://foo.bar/baz' );
}

public function testWhenUrlNotKnown_requestsCauseException() {
$fetcher = new InMemoryFileFetcher( array(
$fetcher = new InMemoryFileFetcher( [
'http://something.else/entirely' => 'kittens'
) );
] );

$this->expectException( FileFetchingException::class );
$fetcher->fetchFile( 'http://foo.bar/baz' );
}

public function testWhenUrlKnown_requestsReturnsValue() {
$fetcher = new InMemoryFileFetcher( array(
$fetcher = new InMemoryFileFetcher( [
'http://something.else/entirely' => 'kittens',
'http://foo.bar/baz' => 'cats'
) );
] );

$this->assertSame( 'cats', $fetcher->fetchFile( 'http://foo.bar/baz' ) );
}
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/NullFileFetcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare( strict_types=1 );

namespace FileFetcher\Tests\Phpunit;

use FileFetcher\NullFileFetcher;
use PHPUnit\Framework\TestCase;

/**
* @covers FileFetcher\NullFileFetcher
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class NullFileFetcherTest extends TestCase {

public function testReturnsEmptyString() {
$this->assertSame( '', ( new NullFileFetcher() )->fetchFile( 'foo.txt' ) );
}

}
5 changes: 2 additions & 3 deletions tests/unit/SpyingFileFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FileFetcher\FileFetchingException;
use FileFetcher\InMemoryFileFetcher;
use FileFetcher\SpyingFileFetcher;
use FileFetcher\ThrowingFileFetcher;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -55,9 +56,7 @@ public function testWhenSomeCalls_getFetchedUrlsReturnsTheArguments() {
}

public function testCallsCausingExceptionsGetRecorded() {
$innerFetcher = new InMemoryFileFetcher( [] );

$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
$spyingFetcher = new SpyingFileFetcher( new ThrowingFileFetcher() );

// @codingStandardsIgnoreStart
try {
Expand Down