Passed
Push — master ( 954b94...8d8751 )
by Jeroen De
36s
created

testWhenWrappedFetcherThrowsAnException_theExceptionIsLogged()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 3
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace FileFetcher\Tests\Phpunit;
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 GNU GPL v2+
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
		$this->expectException( FileFetchingException::class );
39
		$errorLoggingFileFetcher->fetchFile( 'song.txt' );
40
	}
41
42
	public function testWhenWrappedFetcherThrowsAnException_theExceptionIsLogged() {
43
		$logger = new LoggerSpy();
44
		$fileFetcher = new ErrorLoggingFileFetcher(
45
			new ThrowingFileFetcher(),
46
			$logger
47
		);
48
49
		// @codingStandardsIgnoreStart
50
		try {
51
			$fileFetcher->fetchFile( 'song.txt' );
52
			$this->fail( 'Should have thrown a FileFetchingException' );
53
		} 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...
54
		}
55
		// @codingStandardsIgnoreEnd
56
57
		$calls = $logger->getLogCalls();
58
		$this->assertCount( 1, $calls );
59
		$this->assertArrayHasKey( 'exception', $calls->getFirstCall()->getContext() );
60
		$this->assertSame( LogLevel::ERROR, $calls->getFirstCall()->getLevel() );
61
	}
62
63
	public function testGivenLogLevel_exceptionsAreLoggedAtThisLevel() {
64
		$logger = new LoggerSpy();
65
		$fileFetcher = new ErrorLoggingFileFetcher(
66
			new ThrowingFileFetcher(),
67
			$logger,
68
			LogLevel::CRITICAL
69
		);
70
71
		// @codingStandardsIgnoreStart
72
		try {
73
			$fileFetcher->fetchFile( 'song.txt' );
74
		} 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...
75
		}
76
		// @codingStandardsIgnoreEnd
77
78
		$this->assertSame( LogLevel::CRITICAL, $logger->getLogCalls()->getFirstCall()->getLevel() );
79
	}
80
81
}
82