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