Passed
Push — master ( 202ca4...c1a033 )
by
unknown
37s
created

testWhenThereAreSomeCalls_getFirstFetchedUrlReturnsTheFirstOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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\NullFileFetcher;
10
use FileFetcher\SpyingFileFetcher;
11
use FileFetcher\ThrowingFileFetcher;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * @covers FileFetcher\SpyingFileFetcher
16
 *
17
 * @licence GNU GPL v2+
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
		$this->expectException( FileFetchingException::class );
32
		$spyingFetcher->fetchFile( 'foo' );
33
	}
34
35
	public function testWhenNoCalls_getFetchedUrlsReturnsEmptyArray() {
36
		$innerFetcher = new InMemoryFileFetcher( [
37
			'url' => 'content'
38
		] );
39
40
		$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
41
42
		$this->assertSame( [], $spyingFetcher->getFetchedUrls() );
43
	}
44
45
	public function testWhenSomeCalls_getFetchedUrlsReturnsTheArguments() {
46
		$innerFetcher = new InMemoryFileFetcher( [
47
			'url' => 'content',
48
			'foo' => 'bar'
49
		] );
50
51
		$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
52
		$spyingFetcher->fetchFile( 'url' );
53
		$spyingFetcher->fetchFile( 'foo' );
54
		$spyingFetcher->fetchFile( 'url' );
55
56
		$this->assertSame( [ 'url', 'foo', 'url' ], $spyingFetcher->getFetchedUrls() );
57
	}
58
59
	public function testCallsCausingExceptionsGetRecorded() {
60
		$spyingFetcher = new SpyingFileFetcher( new ThrowingFileFetcher() );
61
62
		// @codingStandardsIgnoreStart
63
		try {
64
			$spyingFetcher->fetchFile( 'url' );
65
		}
66
		catch ( FileFetchingException $ex ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by Jeroen De Dauw
Consider adding a comment why this CATCH block is empty.
Loading history...
67
		}
68
69
		try {
70
			$spyingFetcher->fetchFile( 'foo' );
71
		}
72
		catch ( FileFetchingException $ex ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by Jeroen De Dauw
Consider adding a comment why this CATCH block is empty.
Loading history...
73
		}
74
		// @codingStandardsIgnoreEnd
75
76
		$this->assertSame( [ 'url', 'foo' ], $spyingFetcher->getFetchedUrls() );
77
	}
78
79
	public function testWhenThereAreSomeCalls_getFirstFetchedUrlReturnsTheFirstOne() {
80
		$innerFetcher = new InMemoryFileFetcher( [
81
			'url' => 'content',
82
			'foo' => 'bar'
83
		] );
84
85
		$spyingFetcher = new SpyingFileFetcher( $innerFetcher );
86
		$spyingFetcher->fetchFile( 'url' );
87
		$spyingFetcher->fetchFile( 'foo' );
88
89
		$this->assertSame( 'url', $spyingFetcher->getFirstFetchedUrl() );
90
	}
91
92
	public function testWhenThereAreNoCalls_getFirstFetchedUrlReturnsNull() {
93
		$spyingFetcher = new SpyingFileFetcher( new NullFileFetcher() );
94
95
		$this->assertNull( $spyingFetcher->getFirstFetchedUrl() );
96
	}
97
98
}
99