1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace FileFetcher\Tests\Unit; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use FileFetcher\FileFetchingException; |
9
|
|
|
use FileFetcher\InMemoryFileFetcher; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers \FileFetcher\InMemoryFileFetcher |
14
|
|
|
* |
15
|
|
|
* @licence BSD-3-Clause |
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
17
|
|
|
*/ |
18
|
|
|
class InMemoryFileFetcherTest extends TestCase { |
19
|
|
|
|
20
|
|
|
public function testWhenEmptyHash_requestsCauseException() { |
21
|
|
|
$fetcher = new InMemoryFileFetcher( [] ); |
22
|
|
|
$invalidFileUrl = 'http://foo.bar/baz'; |
23
|
|
|
|
24
|
|
|
$this->expectException( FileFetchingException::class ); |
25
|
|
|
$this->expectExceptionMessage( 'Could not fetch file: ' . $invalidFileUrl ); |
26
|
|
|
$fetcher->fetchFile( $invalidFileUrl ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testWhenUrlNotKnown_requestsCauseException() { |
30
|
|
|
$fetcher = new InMemoryFileFetcher( [ |
31
|
|
|
'http://something.else/entirely' => 'kittens' |
32
|
|
|
] ); |
33
|
|
|
$invalidFileUrl = 'http://foo.bar/baz'; |
34
|
|
|
|
35
|
|
|
$this->expectException( FileFetchingException::class ); |
36
|
|
|
$this->expectExceptionMessage( 'Could not fetch file: ' . $invalidFileUrl ); |
37
|
|
|
$fetcher->fetchFile( $invalidFileUrl ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testWhenUrlKnown_requestsReturnsValue() { |
41
|
|
|
$fetcher = new InMemoryFileFetcher( [ |
42
|
|
|
'http://something.else/entirely' => 'kittens', |
43
|
|
|
'http://foo.bar/baz' => 'cats' |
44
|
|
|
] ); |
45
|
|
|
|
46
|
|
|
$this->assertSame( 'cats', $fetcher->fetchFile( 'http://foo.bar/baz' ) ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testWhenThereIsADefault_itIsUsedForUnknownUrls() { |
50
|
|
|
$fetcher = new InMemoryFileFetcher( [], 'default kittens' ); |
51
|
|
|
|
52
|
|
|
$this->assertSame( 'default kittens', $fetcher->fetchFile( 'http://foo.bar' ) ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testWhenThereIsADefault_itIsNotUsedForKnownUrls() { |
56
|
|
|
$fetcher = new InMemoryFileFetcher( |
57
|
|
|
[ |
58
|
|
|
'http://foo.bar' => 'cats' |
59
|
|
|
], |
60
|
|
|
'default kittens' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->assertSame( 'cats', $fetcher->fetchFile( 'http://foo.bar' ) ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testWhenFilesAreNotStringType() { |
67
|
|
|
$this->expectException( InvalidArgumentException::class ); |
68
|
|
|
$this->expectExceptionMessage( 'Both file url and file contents need to be of type string' ); |
69
|
|
|
|
70
|
|
|
new InMemoryFileFetcher( |
71
|
|
|
[ |
72
|
|
|
'http://foo.bar' => 1000, |
73
|
|
|
], |
74
|
|
|
'default kittens' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|