Passed
Pull Request — master (#8)
by Jeroen De
07:56
created

testWhenThereAreNoLogCalls_getFirstLogCallReturnsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\PsrLogTestDoubles\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Log\LogLevel;
9
use WMDE\PsrLogTestDoubles\AssertionException;
10
use WMDE\PsrLogTestDoubles\LogCall;
11
use WMDE\PsrLogTestDoubles\LogCalls;
12
use WMDE\PsrLogTestDoubles\LoggerSpy;
13
14
/**
15
 * @covers \WMDE\PsrLogTestDoubles\LoggerSpy
16
 *
17
 * @license GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class LoggerSpyTest extends TestCase {
21
22
	public function testWhenNothingIsLogged_getLogCallsReturnsEmptyArray() {
23
		$loggerSpy = new LoggerSpy();
24
25
		$this->assertEquals( new LogCalls(), $loggerSpy->getLogCalls() );
26
	}
27
28
	public function testWhenLogIsCalled_getLogCallsReturnsAllCalls() {
29
		$loggerSpy = new LoggerSpy();
30
31
		$loggerSpy->log( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] );
32
		$loggerSpy->log( LogLevel::ALERT, "There's a hole in your mind" );
33
34
		$this->assertEquals(
35
			new LogCalls(
36
				new LogCall( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] ),
37
				new LogCall( LogLevel::ALERT, "There's a hole in your mind" )
38
			),
39
			$loggerSpy->getLogCalls()
40
		);
41
	}
42
43
	public function testWhenShotcutMethodsAreCalled_getLogCallsReturnsAllCalls() {
44
		$loggerSpy = new LoggerSpy();
45
46
		$loggerSpy->info( 'And so it begins', [ 'year' => 2258 ] );
47
		$loggerSpy->alert( "There's a hole in your mind" );
48
49
		$this->assertEquals(
50
			new LogCalls(
51
				new LogCall( LogLevel::INFO, 'And so it begins', [ 'year' => 2258 ] ),
52
				new LogCall( LogLevel::ALERT, "There's a hole in your mind" )
53
			),
54
			$loggerSpy->getLogCalls()
55
		);
56
	}
57
58
	public function testWhenLoggerWasCalled_assertNoCallsThrowsException() {
59
		$loggerSpy = new LoggerSpy();
60
		$loggerSpy->alert( "There's a hole in your mind" );
61
62
		$this->expectException( AssertionException::class );
63
		$loggerSpy->assertNoLoggingCallsWhereMade();
64
	}
65
66
	public function testWhenLoggerWasNotCalled_assertNoCallsDoesNotThrowException() {
67
		$loggerSpy = new LoggerSpy();
68
69
		$loggerSpy->assertNoLoggingCallsWhereMade();
70
71
		$this->assertTrue( true );
72
	}
73
74
	public function testWhenThereAreNoLogCalls_getFirstLogCallReturnsNull() {
75
		$this->assertNull( ( new LoggerSpy() )->getFirstLogCall() );
76
	}
77
78
	public function testWhenMultipleThingsAreLogged_getFirstLogCallReturnsTheFirst() {
79
		$loggerSpy = new LoggerSpy();
80
81
		$loggerSpy->info( 'And so it begins', [ 'year' => 2258 ] );
82
		$loggerSpy->alert( "There's a hole in your mind" );
83
84
		$this->assertSame( LogLevel::INFO, $loggerSpy->getFirstLogCall()->getLevel() );
0 ignored issues
show
Bug introduced by
The method getLevel cannot be called on $loggerSpy->getFirstLogCall() (of type array<integer,object<WMD...ogTestDoubles\LogCall>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
85
		$this->assertSame( 'And so it begins', $loggerSpy->getFirstLogCall()->getMessage() );
0 ignored issues
show
Bug introduced by
The method getMessage cannot be called on $loggerSpy->getFirstLogCall() (of type array<integer,object<WMD...ogTestDoubles\LogCall>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
86
		$this->assertSame( [ 'year' => 2258 ], $loggerSpy->getFirstLogCall()->getContext() );
0 ignored issues
show
Bug introduced by
The method getContext cannot be called on $loggerSpy->getFirstLogCall() (of type array<integer,object<WMD...ogTestDoubles\LogCall>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
87
	}
88
89
}
90