ErrorLoggingFileFetcherTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testWhenWrappedFetcherReturnsValue_itIsReturned() 0 9 1
A testWhenWrappedFetcherThrowsAnException_itIsRethrown() 0 10 1
A testWhenWrappedFetcherThrowsAnException_theExceptionIsLogged() 0 20 2
A testGivenLogLevel_exceptionsAreLoggedAtThisLevel() 0 17 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace FileFetcher\Tests\Unit;
6
7
use FileFetcher\ErrorLoggingFileFetcher;
8
use FileFetcher\FileFetchingException;
9
use FileFetcher\InMemoryFileFetcher;
10
use FileFetcher\ThrowingFileFetcher;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Log\LogLevel;
13
use Psr\Log\NullLogger;
14
use WMDE\PsrLogTestDoubles\LoggerSpy;
15
16
/**
17
 * @license BSD-3-Clause
18
 * @author Gabriel Birke < [email protected] >
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class ErrorLoggingFileFetcherTest extends TestCase {
22
23
	public function testWhenWrappedFetcherReturnsValue_itIsReturned() {
24
		$logger = new LoggerSpy();
25
		$fileFetcher = new ErrorLoggingFileFetcher(
26
			new InMemoryFileFetcher( [ 'song.txt' => 'I\'m a little teapot' ] ),
27
			$logger
28
		);
29
		$this->assertSame( 'I\'m a little teapot', $fileFetcher->fetchFile( 'song.txt' ) );
30
		$logger->assertNoLoggingCallsWhereMade();
31
	}
32
33
	public function testWhenWrappedFetcherThrowsAnException_itIsRethrown() {
34
		$errorLoggingFileFetcher = new ErrorLoggingFileFetcher(
35
			new ThrowingFileFetcher(),
36
			new NullLogger()
37
		);
38
		$invalidFilePath = 'song.txt';
39
		$this->expectException( FileFetchingException::class );
40
		$this->expectExceptionMessage( 'Could not fetch file: ' . $invalidFilePath );
41
		$errorLoggingFileFetcher->fetchFile( $invalidFilePath );
42
	}
43
44
	public function testWhenWrappedFetcherThrowsAnException_theExceptionIsLogged() {
45
		$logger = new LoggerSpy();
46
		$fileFetcher = new ErrorLoggingFileFetcher(
47
			new ThrowingFileFetcher(),
48
			$logger
49
		);
50
51
		// @codingStandardsIgnoreStart
52
		try {
53
			$fileFetcher->fetchFile( 'song.txt' );
54
			$this->fail( 'Should have thrown a FileFetchingException' );
55
		} catch ( FileFetchingException $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by jeroendedauw
Consider adding a comment why this CATCH block is empty.
Loading history...
56
		}
57
		// @codingStandardsIgnoreEnd
58
59
		$calls = $logger->getLogCalls();
60
		$this->assertCount( 1, $calls );
61
		$this->assertArrayHasKey( 'exception', $calls->getFirstCall()->getContext() );
62
		$this->assertSame( LogLevel::ERROR, $calls->getFirstCall()->getLevel() );
63
	}
64
65
	public function testGivenLogLevel_exceptionsAreLoggedAtThisLevel() {
66
		$logger = new LoggerSpy();
67
		$fileFetcher = new ErrorLoggingFileFetcher(
68
			new ThrowingFileFetcher(),
69
			$logger,
70
			LogLevel::CRITICAL
71
		);
72
73
		// @codingStandardsIgnoreStart
74
		try {
75
			$fileFetcher->fetchFile( 'song.txt' );
76
		} catch ( FileFetchingException $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by jeroendedauw
Consider adding a comment why this CATCH block is empty.
Loading history...
77
		}
78
		// @codingStandardsIgnoreEnd
79
80
		$this->assertSame( LogLevel::CRITICAL, $logger->getLogCalls()->getFirstCall()->getLevel() );
81
	}
82
83
}
84