These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | $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
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
|
|||
75 | } |
||
76 | // @codingStandardsIgnoreEnd |
||
77 | |||
78 | $this->assertSame( LogLevel::CRITICAL, $logger->getLogCalls()->getFirstCall()->getLevel() ); |
||
79 | } |
||
80 | |||
81 | } |
||
82 |