Passed
Pull Request — master (#20)
by lee
05:02
created

testWhenFilesAreNotStringType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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