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 PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers FileFetcher\InMemoryFileFetcher |
13
|
|
|
* |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class InMemoryFileFetcherTest extends TestCase { |
18
|
|
|
|
19
|
|
|
public function testWhenEmptyHash_requestsCauseException() { |
20
|
|
|
$fetcher = new InMemoryFileFetcher( [] ); |
21
|
|
|
|
22
|
|
|
$this->expectException( FileFetchingException::class ); |
23
|
|
|
$fetcher->fetchFile( 'http://foo.bar/baz' ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testWhenUrlNotKnown_requestsCauseException() { |
27
|
|
|
$fetcher = new InMemoryFileFetcher( [ |
28
|
|
|
'http://something.else/entirely' => 'kittens' |
29
|
|
|
] ); |
30
|
|
|
|
31
|
|
|
$this->expectException( FileFetchingException::class ); |
32
|
|
|
$fetcher->fetchFile( 'http://foo.bar/baz' ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testWhenUrlKnown_requestsReturnsValue() { |
36
|
|
|
$fetcher = new InMemoryFileFetcher( [ |
37
|
|
|
'http://something.else/entirely' => 'kittens', |
38
|
|
|
'http://foo.bar/baz' => 'cats' |
39
|
|
|
] ); |
40
|
|
|
|
41
|
|
|
$this->assertSame( 'cats', $fetcher->fetchFile( 'http://foo.bar/baz' ) ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testWhenThereIsADefault_itIsUsedForUnknownUrls() { |
45
|
|
|
$fetcher = new InMemoryFileFetcher( [], 'default kittens' ); |
46
|
|
|
|
47
|
|
|
$this->assertSame( 'default kittens', $fetcher->fetchFile( 'http://foo.bar' ) ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testWhenThereIsADefault_itIsNotUsedForKnownUrls() { |
51
|
|
|
$fetcher = new InMemoryFileFetcher( |
52
|
|
|
[ |
53
|
|
|
'http://foo.bar' => 'cats' |
54
|
|
|
], |
55
|
|
|
'default kittens' |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->assertSame( 'cats', $fetcher->fetchFile( 'http://foo.bar' ) ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|