LoggerSpyTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 70
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testWhenNothingIsLogged_getLogCallsReturnsEmptyArray() 0 5 1
A testWhenLogIsCalled_getLogCallsReturnsAllCalls() 0 14 1
A testWhenShotcutMethodsAreCalled_getLogCallsReturnsAllCalls() 0 14 1
A testWhenLoggerWasCalled_assertNoCallsThrowsException() 0 7 1
A testWhenLoggerWasNotCalled_assertNoCallsDoesNotThrowException() 0 7 1
A testWhenThereAreNoLogCalls_getFirstLogCallReturnsNull() 0 3 1
A testWhenMultipleThingsAreLogged_getFirstLogCallReturnsTheFirst() 0 10 1
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