Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CallbackFileFetcher and LazyStubFileFetcher
- Loading branch information
1 parent
01448ef
commit 593cd33
Showing
7 changed files
with
130 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} | ||
|
||
} |
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,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 ); | ||
} ); | ||
} | ||
|
||
} |
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,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' ) | ||
); | ||
} | ||
|
||
} |
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,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' ); | ||
} | ||
|
||
} |
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