Passed
Push — master ( b58e9b...f2401b )
by Jeroen De
35s
created

testGivenFailingFileFetcher_anExceptionIsThrown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
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 PHPUnit\Framework\TestCase;
11
use Psr\Log\LogLevel;
12
use Psr\Log\NullLogger;
13
use WMDE\PsrLogTestDoubles\LoggerSpy;
14
15
/**
16
 * @license GNU GPL v2+
17
 * @author Gabriel Birke < [email protected] >
18
 */
19
class ErrorLoggingFileFetcherTest extends TestCase {
20
21
	public function testGivenSucceedingFileFetcher_itsContentIsReturned() {
22
		$logger = new LoggerSpy();
23
		$fileFetcher = new ErrorLoggingFileFetcher(
24
			new InMemoryFileFetcher( [ 'song.txt' => 'I\'m a little teapot' ] ),
25
			$logger
26
		);
27
		$this->assertSame( 'I\'m a little teapot', $fileFetcher->fetchFile( 'song.txt' ) );
28
		$logger->assertNoLoggingCallsWhereMade();
29
	}
30
31
	public function testGivenFailingFileFetcher_anExceptionIsThrown() {
32
		$errorLoggingFileFetcher = new ErrorLoggingFileFetcher(
33
			new InMemoryFileFetcher( [] ),
34
			new NullLogger()
35
		);
36
		$this->expectException( FileFetchingException::class );
37
		$errorLoggingFileFetcher->fetchFile( 'song.txt' );
38
	}
39
40
	public function testGivenFailingFileFetcher_theExceptionIsLogged() {
41
		$logger = new LoggerSpy();
42
		$fileFetcher = new ErrorLoggingFileFetcher(
43
			new InMemoryFileFetcher( [] ),
44
			$logger
45
		);
46
47
		// @codingStandardsIgnoreStart
48
		try {
49
			$fileFetcher->fetchFile( 'song.txt' );
50
			$this->fail( 'Should have thrown a FileFetchingException' );
51
		} catch ( FileFetchingException $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
52
		}
53
		// @codingStandardsIgnoreEnd
54
55
		$calls = $logger->getLogCalls();
56
		$this->assertCount( 1, $calls );
57
		$this->assertArrayHasKey( 'exception', $calls->getFirstCall()->getContext() );
58
		$this->assertSame( LogLevel::ERROR, $calls->getFirstCall()->getLevel() );
59
	}
60
61
	public function testGivenLogLevel_exceptionsAreLoggedAtThisLevel() {
62
		$logger = new LoggerSpy();
63
		$fileFetcher = new ErrorLoggingFileFetcher(
64
			new InMemoryFileFetcher( [] ),
65
			$logger,
66
			LogLevel::CRITICAL
67
		);
68
69
		// @codingStandardsIgnoreStart
70
		try {
71
			$fileFetcher->fetchFile( 'song.txt' );
72
		} catch ( FileFetchingException $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
73
		}
74
		// @codingStandardsIgnoreEnd
75
76
		$this->assertSame( LogLevel::CRITICAL, $logger->getLogCalls()->getFirstCall()->getLevel() );
77
	}
78
79
}
80