Passed
Push — master ( 915f9b...db478f )
by Jeroen De
05:06 queued 03:24
created

PsrCacheFileFetcherTest::newCacheThatThrowsOnSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
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\PsrCacheFileFetcher;
10
use FileFetcher\ThrowingFileFetcher;
11
use PHPUnit\Framework\TestCase;
12
use Psr\SimpleCache\CacheException;
13
use Psr\SimpleCache\CacheInterface;
14
15
/**
16
 * @covers \FileFetcher\PsrCacheFileFetcher
17
 *
18
 * @licence BSD-3-Clause
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class PsrCacheFileFetcherTest extends TestCase {
22
23
	private const FILE_URL = 'foo://bar';
24
	private const FILE_CONTENT = 'NyanData across the sky!';
25
26
	public function testWhenFileIsNotCached_itGetsRetrieved() {
27
		$cachingFetcher = new PsrCacheFileFetcher(
28
			new InMemoryFileFetcher( [
29
				self::FILE_URL => self::FILE_CONTENT
30
			] ),
31
			$this->newNullCache()
32
		);
33
34
		$this->assertSame(
35
			self::FILE_CONTENT,
36
			$cachingFetcher->fetchFile( self::FILE_URL )
37
		);
38
	}
39
40
	private function newNullCache(): CacheInterface {
41
		$cache = $this->createMock( CacheInterface::class );
42
43
		$cache->expects( $this->any() )
44
			->method( 'get' )
45
			->with( self::FILE_URL )
46
			->will( $this->returnValue( null ) );
47
48
		return $cache;
49
	}
50
51
	public function testWhenFileIsCached_cachedContentsGetsReturned() {
52
		$cachingFetcher = new PsrCacheFileFetcher(
53
			new ThrowingFileFetcher(),
54
			$this->newCacheWithFile()
55
		);
56
57
		$this->assertSame(
58
			self::FILE_CONTENT,
59
			$cachingFetcher->fetchFile( self::FILE_URL )
60
		);
61
	}
62
63
	private function newCacheWithFile(): CacheInterface {
64
		$cache = $this->createMock( CacheInterface::class );
65
66
		$cache->expects( $this->once() )
67
			->method( 'get' )
68
			->with( self::FILE_URL )
69
			->will( $this->returnValue( self::FILE_CONTENT ) );
70
71
		return $cache;
72
	}
73
74
	public function testWhenFileIsNotCached_fileContentsGetsCached() {
75
		$cache = $this->newNullCache();
76
77
		$cache->expects( $this->once() )
78
			->method( 'set' )
79
			->with(
80
				$this->equalTo( self::FILE_URL ),
81
				$this->equalTo( self::FILE_CONTENT )
82
			);
83
84
		$cachingFetcher = new PsrCacheFileFetcher(
85
			new InMemoryFileFetcher( [
86
				self::FILE_URL => self::FILE_CONTENT
87
			] ),
88
			$cache
89
		);
90
91
		$cachingFetcher->fetchFile( self::FILE_URL );
92
	}
93
94
	public function testWhenFetcherThrowsException_itIsNotCaught() {
95
		$fetcher = new PsrCacheFileFetcher( new ThrowingFileFetcher(), $this->newNullCache() );
96
97
		$this->expectException( FileFetchingException::class );
98
		$fetcher->fetchFile( self::FILE_URL );
99
	}
100
101
	public function testWhenCacheReadThrowsException_fileContentIsFetched() {
102
		$cachingFetcher = new PsrCacheFileFetcher(
103
			new InMemoryFileFetcher( [
104
				self::FILE_URL => self::FILE_CONTENT
105
			] ),
106
			$this->newCacheThatThrowsOnGet()
107
		);
108
109
		$this->assertSame(
110
			self::FILE_CONTENT,
111
			$cachingFetcher->fetchFile( self::FILE_URL )
112
		);
113
	}
114
115
	private function newCacheThatThrowsOnGet(): CacheInterface {
116
		$cache = $this->createMock( CacheInterface::class );
117
118
		$cache->expects( $this->any() )
119
			->method( 'get' )
120
			->willThrowException( $this->newCacheException() );
121
122
		return $cache;
123
	}
124
125
	private function newCacheException(): \Exception {
126
		return new class() extends \Exception implements CacheException {
127
		};
128
	}
129
130
	public function testWhenCacheWriteThrowsException_fileContentIsReturned() {
131
		$cachingFetcher = new PsrCacheFileFetcher(
132
			new InMemoryFileFetcher( [
133
				self::FILE_URL => self::FILE_CONTENT
134
			] ),
135
			$this->newCacheThatThrowsOnSet()
136
		);
137
138
		$this->assertSame(
139
			self::FILE_CONTENT,
140
			$cachingFetcher->fetchFile( self::FILE_URL )
141
		);
142
	}
143
144
	private function newCacheThatThrowsOnSet(): CacheInterface {
145
		$cache = $this->createMock( CacheInterface::class );
146
147
		$cache->expects( $this->any() )
148
			->method( 'set' )
149
			->willThrowException( $this->newCacheException() );
150
151
		return $cache;
152
	}
153
154
}
155