Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from JeroenDeDauw/SpyingFileFetcherr
Added SpyingFileFetcher
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 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,43 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace FileFetcher; | ||
|
||
/** | ||
* Decorator for FileFetcher objects that records file fetching calls. | ||
* | ||
* @since 3.2 | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
class SpyingFileFetcher implements FileFetcher { | ||
|
||
private $fileFetcher; | ||
|
||
private $fetchedUrls = []; | ||
|
||
public function __construct( FileFetcher $fileFetcher ) { | ||
$this->fileFetcher = $fileFetcher; | ||
} | ||
|
||
/** | ||
* @see FileFetcher::fetchFile | ||
* @throws FileFetchingException | ||
*/ | ||
public function fetchFile( string $fileUrl ): string { | ||
$this->fetchedUrls[] = $fileUrl; | ||
return $this->fileFetcher->fetchFile( $fileUrl ); | ||
} | ||
|
||
/** | ||
* Returns an ordered list of fetched URLs. Duplicates are preserved. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getFetchedUrls(): array { | ||
return $this->fetchedUrls; | ||
} | ||
|
||
} |
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,79 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace FileFetcher\Tests\Phpunit; | ||
|
||
use FileFetcher\FileFetchingException; | ||
use FileFetcher\InMemoryFileFetcher; | ||
use FileFetcher\SpyingFileFetcher; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers FileFetcher\SpyingFileFetcher | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
class SpyingFileFetcherTest extends TestCase { | ||
|
||
public function testReturnsResultOfDecoratedFetcher() { | ||
$innerFetcher = new InMemoryFileFetcher( [ | ||
'url' => 'content' | ||
] ); | ||
|
||
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | ||
|
||
$this->assertSame( 'content', $spyingFetcher->fetchFile( 'url' ) ); | ||
|
||
$this->expectException( FileFetchingException::class ); | ||
$spyingFetcher->fetchFile( 'foo' ); | ||
} | ||
|
||
public function testWhenNoCalls_getFetchedUrlsReturnsEmptyArray() { | ||
$innerFetcher = new InMemoryFileFetcher( [ | ||
'url' => 'content' | ||
] ); | ||
|
||
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | ||
|
||
$this->assertSame( [], $spyingFetcher->getFetchedUrls() ); | ||
} | ||
|
||
public function testWhenSomeCalls_getFetchedUrlsReturnsTheArguments() { | ||
$innerFetcher = new InMemoryFileFetcher( [ | ||
'url' => 'content', | ||
'foo' => 'bar' | ||
] ); | ||
|
||
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | ||
$spyingFetcher->fetchFile( 'url' ); | ||
$spyingFetcher->fetchFile( 'foo' ); | ||
$spyingFetcher->fetchFile( 'url' ); | ||
|
||
$this->assertSame( [ 'url', 'foo', 'url' ], $spyingFetcher->getFetchedUrls() ); | ||
} | ||
|
||
public function testCallsCausingExceptionsGetRecorded() { | ||
$innerFetcher = new InMemoryFileFetcher( [] ); | ||
|
||
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); | ||
|
||
// @codingStandardsIgnoreStart | ||
try { | ||
$spyingFetcher->fetchFile( 'url' ); | ||
} | ||
catch ( FileFetchingException $ex ) { | ||
} | ||
|
||
try { | ||
$spyingFetcher->fetchFile( 'foo' ); | ||
} | ||
catch ( FileFetchingException $ex ) { | ||
} | ||
// @codingStandardsIgnoreEnd | ||
|
||
$this->assertSame( [ 'url', 'foo' ], $spyingFetcher->getFetchedUrls() ); | ||
} | ||
|
||
} |