Skip to content

Commit

Permalink
Added CallbackFileFetcher and LazyStubFileFetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed May 30, 2018
1 parent 01448ef commit 593cd33
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -22,6 +22,8 @@ The library provides some trivial implementations of the `FileFetcher` interface
* `ThrowingFileFetcher`: Throws a `FileFetchingException` for all calls
* `NullFileFetcher`: Returns an empty string for all calls
* `StubFileFetcher`: Returns a stub value for all calls
* `CallbackFileFetcher`: Adapter around a callback
* `LazyStubFileFetcher`: Return a lazily retrieved stub value for all calls

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

Expand Down Expand Up @@ -61,6 +63,11 @@ For a full CI run

## Release notes

### 4.4.0 (2018-05-31)

* Added `CallbackFileFetcher`
* Added `LazyStubFileFetcher`

### 4.3.0 (2017-06-10)

* Added `getFirstFetchedUrl` to `SpyingFileFetcher`
Expand Down
33 changes: 33 additions & 0 deletions src/CallbackFileFetcher.php
@@ -0,0 +1,33 @@
<?php

declare( strict_types=1 );

namespace FileFetcher;

/**
* Callback adapter. Calls to fetchFile are routed to the callback.
*
* @since 4.4
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class CallbackFileFetcher implements FileFetcher {

private $callback;

/**
* The callback should have the same signature and contract as @see FileFetcher::fetchFile()
* Note that this contract include not throwing exceptions other than FileFetchingException.
*
* @param callable $callback
*/
public function __construct( callable $callback ) {
$this->callback = $callback;
}

public function fetchFile( string $fileUrl ): string {
return ($this->callback)( $fileUrl );
}

}
26 changes: 26 additions & 0 deletions src/LazyStubFileFetcher.php
@@ -0,0 +1,26 @@
<?php

declare( strict_types=1 );

namespace FileFetcher;

/**
* @since 4.4
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class LazyStubFileFetcher {

private function __construct() {
}

public static function newFromFileUrl( string $fileUrl ): FileFetcher {
$fetcher = new SimpleFileFetcher();

return new CallbackFileFetcher( function() use ( $fetcher, $fileUrl ) {
return $fetcher->fetchFile( $fileUrl );
} );
}

}
25 changes: 25 additions & 0 deletions tests/integration/LazyStubFileFetcherTest.php
@@ -0,0 +1,25 @@
<?php

declare( strict_types=1 );

namespace FileFetcher\Tests\Integration;

use FileFetcher\LazyStubFileFetcher;
use PHPUnit\Framework\TestCase;

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

public function testCallbackGetsFileUrlAndReturnValueIsReturned() {
$this->assertSame(
file_get_contents( __FILE__ ),
LazyStubFileFetcher::newFromFileUrl( __FILE__ )->fetchFile( 'Whatever' )
);
}

}
4 changes: 2 additions & 2 deletions tests/integration/SimpleFileFetcherTest.php
Expand Up @@ -9,7 +9,7 @@
use PHPUnit\Framework\TestCase;

/**
* @covers FileFetcher\SimpleFileFetcher
* @covers \FileFetcher\SimpleFileFetcher
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
Expand All @@ -21,7 +21,7 @@ public function testGetThisFileFromDisk() {

$contents = $fetcher->fetchFile( __FILE__ );

$this->assertEquals( file_get_contents( __FILE__ ), $contents );
$this->assertSame( file_get_contents( __FILE__ ), $contents );
}

public function testGetThisFileFromGitHub() {
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/CallbackFileFetcherTest.php
@@ -0,0 +1,36 @@
<?php

declare( strict_types=1 );

namespace FileFetcher\Tests\Phpunit;

use FileFetcher\CallbackFileFetcher;
use FileFetcher\FileFetchingException;
use PHPUnit\Framework\TestCase;

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

public function testCallbackGetsFileUrlAndReturnValueIsReturned() {
$fetcher = new CallbackFileFetcher( function( string $fileUrl ) {
return $fileUrl . $fileUrl . $fileUrl;
} );

$this->assertSame( 'SuchSuchSuch', $fetcher->fetchFile( 'Such' ) );
}

public function testCallbackExceptionBubblesUp() {
$fetcher = new CallbackFileFetcher( function( string $fileUrl ) {
throw new FileFetchingException( $fileUrl );
} );

$this->expectException( FileFetchingException::class );
$fetcher->fetchFile( 'Such' );
}

}
2 changes: 1 addition & 1 deletion tests/unit/StubFileFetcherTest.php
Expand Up @@ -8,7 +8,7 @@
use PHPUnit\Framework\TestCase;

/**
* @covers FileFetcher\StubFileFetcher
* @covers \FileFetcher\StubFileFetcher
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
Expand Down

0 comments on commit 593cd33

Please sign in to comment.