Skip to content

Commit

Permalink
Merge pull request #8 from JeroenDeDauw/SpyingFileFetcherr
Browse files Browse the repository at this point in the history
Added SpyingFileFetcher
  • Loading branch information
JeroenDeDauw committed May 9, 2017
2 parents 792f58c + 047267b commit 5e1240b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -55,6 +55,7 @@ For a full CI run
* Dropped support for PHP 5.x
* Added scalar type hints to the `FileFetcher` interface and its implementations
* Added scalar type hints to `FileFetchingException`
* Added `SpyingFileFetcher`

### 3.1.0 (2016-01-07)

Expand Down
43 changes: 43 additions & 0 deletions src/SpyingFileFetcher.php
@@ -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;
}

}
79 changes: 79 additions & 0 deletions tests/unit/SpyingFileFetcherTest.php
@@ -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() );
}

}

0 comments on commit 5e1240b

Please sign in to comment.