Skip to content

Commit

Permalink
Merge b34db47 into 1d180d0
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Aug 17, 2016
2 parents 1d180d0 + b34db47 commit 0d2d017
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
@@ -1,10 +1,9 @@
language: php

php:
- 5.3
- 5.4
- 5.6
- 7
- 7.1
- hhvm

before_script: travis_retry composer install
Expand Down
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -36,6 +36,11 @@ FileFetcher.php.

## Release notes

### 3.2.0 (2016-08-18)

* Dropped support for PHP 5.3 and 5.4
* Added `SpyingFileFetcher`

### 3.1.0 (2016-01-07)

* Added `InMemoryFileFetcher`
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -20,7 +20,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=5.5.0",
"jeroen/simple-cache": "~2.0"
},
"require-dev": {
Expand All @@ -36,7 +36,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.1.x-dev"
"dev-master": "3.2.x-dev"
}
},
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion phpcs.xml
Expand Up @@ -5,7 +5,9 @@
-->
<ruleset name="MediaWiki">
<rule ref="Generic.Classes" />
<rule ref="Generic.CodeAnalysis" />
<rule ref="Generic.CodeAnalysis">
<exclude name="Generic.CodeAnalysis.EmptyStatement" />
</rule>
<rule ref="Generic.ControlStructures" />

<rule ref="Generic.Files.ByteOrderMark" />
Expand Down
2 changes: 2 additions & 0 deletions src/InMemoryFileFetcher.php
Expand Up @@ -12,6 +12,8 @@
*/
class InMemoryFileFetcher implements FileFetcher {

private $files;

/**
* @param string[] $files
* @throws InvalidArgumentException
Expand Down
45 changes: 45 additions & 0 deletions src/SpyingFileFetcher.php
@@ -0,0 +1,45 @@
<?php

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
*
* @param string $fileUrl
*
* @return string
* @throws FileFetchingException
*/
public function fetchFile( $fileUrl ) {
$this->fetchedUrls[] = $fileUrl;
return $this->fileFetcher->fetchFile( $fileUrl );
}

/**
* Returns an ordered list of fetched URLs. Duplicates are preserved.
*
* @return string[]
*/
public function getFetchedUrls() {
return $this->fetchedUrls;
}

}
14 changes: 8 additions & 6 deletions tests/unit/CachingFileFetcherTest.php
Expand Up @@ -3,6 +3,8 @@
namespace FileFetcher\Tests\Phpunit;

use FileFetcher\CachingFileFetcher;
use FileFetcher\FileFetcher;
use SimpleCache\Cache\Cache;

/**
* @covers FileFetcher\CachingFileFetcher
Expand All @@ -13,8 +15,8 @@
class CachingFileFetcherTest extends \PHPUnit_Framework_TestCase {

public function testCanConstruct() {
$fileFetcher = $this->createMock( 'FileFetcher\FileFetcher' );
$cache = $this->createMock( 'SimpleCache\Cache\Cache' );
$fileFetcher = $this->createMock( FileFetcher::class );
$cache = $this->createMock( Cache::class );

new CachingFileFetcher( $fileFetcher, $cache );

Expand All @@ -25,14 +27,14 @@ public function testGetFileWhenNotCached() {
$fileUrl = 'foo://bar';
$fileContents = 'NyanData across the sky!';

$fileFetcher = $this->createMock( 'FileFetcher\FileFetcher' );
$fileFetcher = $this->createMock( FileFetcher::class );

$fileFetcher->expects( $this->once() )
->method( 'fetchFile' )
->with( $fileUrl )
->will( $this->returnValue( $fileContents ) );

$cache = $this->createMock( 'SimpleCache\Cache\Cache' );
$cache = $this->createMock( Cache::class );

$cache->expects( $this->once() )
->method( 'get' )
Expand All @@ -51,12 +53,12 @@ public function testGetFileWhenCached() {
$fileUrl = 'foo://bar';
$fileContents = 'NyanData across the sky!';

$fileFetcher = $this->createMock( 'FileFetcher\FileFetcher' );
$fileFetcher = $this->createMock( FileFetcher::class );

$fileFetcher->expects( $this->never() )
->method( 'fetchFile' );

$cache = $this->createMock( 'SimpleCache\Cache\Cache' );
$cache = $this->createMock( Cache::class );

$cache->expects( $this->once() )
->method( 'get' )
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/InMemoryFileFetcherTest.php
Expand Up @@ -2,6 +2,7 @@

namespace FileFetcher\Tests\Phpunit;

use FileFetcher\FileFetchingException;
use FileFetcher\InMemoryFileFetcher;

/**
Expand All @@ -15,7 +16,7 @@ class InMemoryFileFetcherTest extends \PHPUnit_Framework_TestCase {
public function testWhenEmptyHash_requestsCauseException() {
$fetcher = new InMemoryFileFetcher( array() );

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

Expand All @@ -24,7 +25,7 @@ public function testWhenUrlNotKnown_requestsCauseException() {
'http://something.else/entirely' => 'kittens'
) );

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

Expand Down
74 changes: 74 additions & 0 deletions tests/unit/SpyingFileFetcherTest.php
@@ -0,0 +1,74 @@
<?php

namespace FileFetcher\Tests\Phpunit;

use FileFetcher\FileFetchingException;
use FileFetcher\InMemoryFileFetcher;
use FileFetcher\SpyingFileFetcher;

/**
* @covers FileFetcher\SpyingFileFetcher
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class SpyingFileFetcherTest extends \PHPUnit_Framework_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 );

try {
$spyingFetcher->fetchFile( 'url' );
}
catch ( FileFetchingException $ex ) {
}

try {
$spyingFetcher->fetchFile( 'foo' );
}
catch ( FileFetchingException $ex ) {
}

$this->assertSame( [ 'url', 'foo' ], $spyingFetcher->getFetchedUrls() );
}

}

0 comments on commit 0d2d017

Please sign in to comment.