1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FileFetcher\Tests\Phpunit; |
4
|
|
|
|
5
|
|
|
use FileFetcher\FileFetchingException; |
6
|
|
|
use FileFetcher\InMemoryFileFetcher; |
7
|
|
|
use FileFetcher\SpyingFileFetcher; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers FileFetcher\SpyingFileFetcher |
11
|
|
|
* |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class SpyingFileFetcherTest extends \PHPUnit_Framework_TestCase { |
16
|
|
|
|
17
|
|
|
public function testReturnsResultOfDecoratedFetcher() { |
18
|
|
|
$innerFetcher = new InMemoryFileFetcher( [ |
19
|
|
|
'url' => 'content' |
20
|
|
|
] ); |
21
|
|
|
|
22
|
|
|
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); |
23
|
|
|
|
24
|
|
|
$this->assertSame( 'content', $spyingFetcher->fetchFile( 'url' ) ); |
25
|
|
|
|
26
|
|
|
$this->expectException( FileFetchingException::class ); |
27
|
|
|
$spyingFetcher->fetchFile( 'foo' ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testWhenNoCalls_getFetchedUrlsReturnsEmptyArray() { |
31
|
|
|
$innerFetcher = new InMemoryFileFetcher( [ |
32
|
|
|
'url' => 'content' |
33
|
|
|
] ); |
34
|
|
|
|
35
|
|
|
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); |
36
|
|
|
|
37
|
|
|
$this->assertSame( [], $spyingFetcher->getFetchedUrls() ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testWhenSomeCalls_getFetchedUrlsReturnsTheArguments() { |
41
|
|
|
$innerFetcher = new InMemoryFileFetcher( [ |
42
|
|
|
'url' => 'content', |
43
|
|
|
'foo' => 'bar' |
44
|
|
|
] ); |
45
|
|
|
|
46
|
|
|
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); |
47
|
|
|
$spyingFetcher->fetchFile( 'url' ); |
48
|
|
|
$spyingFetcher->fetchFile( 'foo' ); |
49
|
|
|
$spyingFetcher->fetchFile( 'url' ); |
50
|
|
|
|
51
|
|
|
$this->assertSame( [ 'url', 'foo', 'url' ], $spyingFetcher->getFetchedUrls() ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testCallsCausingExceptionsGetRecorded() { |
55
|
|
|
$innerFetcher = new InMemoryFileFetcher( [] ); |
56
|
|
|
|
57
|
|
|
$spyingFetcher = new SpyingFileFetcher( $innerFetcher ); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$spyingFetcher->fetchFile( 'url' ); |
61
|
|
|
} |
62
|
|
|
catch ( FileFetchingException $ex ) { |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$spyingFetcher->fetchFile( 'foo' ); |
67
|
|
|
} |
68
|
|
|
catch ( FileFetchingException $ex ) { |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->assertSame( [ 'url', 'foo' ], $spyingFetcher->getFetchedUrls() ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|