testWhenThereAreSomeCalls_getFirstFetchedUrlReturnsTheFirstOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
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 FileFetcher\FileFetchingException;
8
use FileFetcher\InMemoryFileFetcher;
9
use FileFetcher\NullFileFetcher;
10
use FileFetcher\SpyingFileFetcher;
11
use FileFetcher\ThrowingFileFetcher;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * @covers \FileFetcher\SpyingFileFetcher
16
 *
17
 * @licence BSD-3-Clause
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class SpyingFileFetcherTest extends TestCase {
21
22
	public function testReturnsResultOfDecoratedFetcher() {
23
		$innerFetcher = new InMemoryFileFetcher( [
24
			'url' => 'content'
25
		] );
26
27
		$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
28
29
		$this->assertSame( 'content', $spyingFetcher->fetchFile( 'url' ) );
30
31
		$invalidFilePath = 'foo';
32
33
		$this->expectException( FileFetchingException::class );
34
		$this->expectExceptionMessage( 'Could not fetch file: ' . $invalidFilePath );
35
		$spyingFetcher->fetchFile( $invalidFilePath );
36
	}
37
38
	public function testWhenNoCalls_getFetchedUrlsReturnsEmptyArray() {
39
		$innerFetcher = new InMemoryFileFetcher( [
40
			'url' => 'content'
41
		] );
42
43
		$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
44
		$result = $spyingFetcher->getFetchedUrls();
45
46
		$this->assertIsArray( $result );
47
		$this->assertSame( [], $result );
48
	}
49
50
	public function testWhenSomeCalls_getFetchedUrlsReturnsTheArguments() {
51
		$innerFetcher = new InMemoryFileFetcher( [
52
			'url' => 'content',
53
			'foo' => 'bar'
54
		] );
55
56
		$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
57
		$spyingFetcher->fetchFile( 'url' );
58
		$spyingFetcher->fetchFile( 'foo' );
59
		$spyingFetcher->fetchFile( 'url' );
60
61
		$this->assertSame( [ 'url', 'foo', 'url' ], $spyingFetcher->getFetchedUrls() );
62
	}
63
64
	public function testCallsCausingExceptionsGetRecorded() {
65
		$spyingFetcher = new SpyingFileFetcher( new ThrowingFileFetcher() );
66
67
		// @codingStandardsIgnoreStart
68
		try {
69
			$spyingFetcher->fetchFile( 'url' );
70
		}
71
		catch ( FileFetchingException $ex ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
72
		}
73
74
		try {
75
			$spyingFetcher->fetchFile( 'foo' );
76
		}
77
		catch ( FileFetchingException $ex ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
78
		}
79
		// @codingStandardsIgnoreEnd
80
81
		$this->assertSame( [ 'url', 'foo' ], $spyingFetcher->getFetchedUrls() );
82
	}
83
84
	public function testWhenThereAreSomeCalls_getFirstFetchedUrlReturnsTheFirstOne() {
85
		$innerFetcher = new InMemoryFileFetcher( [
86
			'url' => 'content',
87
			'foo' => 'bar'
88
		] );
89
90
		$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
91
		$spyingFetcher->fetchFile( 'url' );
92
		$spyingFetcher->fetchFile( 'foo' );
93
94
		$this->assertSame( 'url', $spyingFetcher->getFirstFetchedUrl() );
95
	}
96
97
	public function testWhenThereAreNoCalls_getFirstFetchedUrlReturnsNull() {
98
		$spyingFetcher = new SpyingFileFetcher( new NullFileFetcher() );
99
100
		$this->assertNull( $spyingFetcher->getFirstFetchedUrl() );
101
	}
102
103
}
104