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 ) { |
|
|
|
|
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 ) { |
|
|
|
|
77
|
|
|
} |
78
|
|
|
// @codingStandardsIgnoreEnd |
79
|
|
|
|
80
|
|
|
$this->assertSame( LogLevel::CRITICAL, $logger->getLogCalls()->getFirstCall()->getLevel() ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|