Completed
Push — master ( d48601...03ac5e )
by Jeroen De
13s queued 10s
created

testReporterInvocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
rs 9.408
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Onoi\MessageReporter\Tests\Unit;
4
5
use Onoi\MessageReporter\MessageReporter;
6
use Onoi\MessageReporter\ObservableMessageReporter;
7
8
/**
9
 * @covers \Onoi\MessageReporter\ObservableMessageReporter
10
 * @group onoi-message-reporter
11
 *
12
 * @license GNU GPL v2+
13
 */
14
class ObservableMessageReporterTest extends MessageReporterTestCase {
15
16
	public function testCanConstruct() {
17
18
		$this->assertInstanceOf(
19
			'\Onoi\MessageReporter\ObservableMessageReporter',
20
			new ObservableMessageReporter()
21
		);
22
	}
23
24
	/**
25
	 * @return MessageReporter[]
26
	 */
27
	public function getInstances() {
28
		$instances = [];
29
30
		$instances[] = new ObservableMessageReporter();
31
32
		$reporter = new ObservableMessageReporter();
33
		$reporter->registerMessageReporter( new ObservableMessageReporter() );
34
		$callback0 = function ( $string ) {};
0 ignored issues
show
Unused Code introduced by
The parameter $string is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
		$callback1 = function ( $string ) {};
0 ignored issues
show
Unused Code introduced by
The parameter $string is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
		$instances[] = $reporter;
37
38
		$reporter = clone $reporter;
39
		$reporter->registerReporterCallback( $callback0 );
40
		$reporter->registerReporterCallback( $callback1 );
41
		$instances[] = $reporter;
42
43
		return $instances;
44
	}
45
46
	/**
47
	 * @dataProvider reportMessageProvider
48
	 *
49
	 * @param string $message
50
	 */
51
	public function testCallbackInvocation( $message ) {
52
		$callCount = 0;
53
		$asserter = [ $this, 'assertEquals' ];
54
55
		$callback0 = function ( $actual ) use ( $message, &$callCount, $asserter ) {
56
			$callCount += 1;
57
			call_user_func( $asserter, $message, $actual );
58
		};
59
60
		$callback1 = function ( $actual ) use ( $message, &$callCount, $asserter ) {
61
			$callCount += 1;
62
			call_user_func( $asserter, $message, $actual );
63
		};
64
65
		$reporter = new ObservableMessageReporter();
66
		$reporter->registerReporterCallback( $callback0 );
67
		$reporter->registerReporterCallback( $callback1 );
68
69
		$reporter->reportMessage( $message );
70
71
		$this->assertEquals( 2, $callCount );
72
73
		$reporter->reportMessage( $message );
74
75
		$this->assertEquals( 4, $callCount );
76
	}
77
78
	/**
79
	 * @dataProvider reportMessageProvider
80
	 *
81
	 * @param string $message
82
	 */
83
	public function testReporterInvocation( $message ) {
84
		$callCount = 0;
85
		$asserter = [ $this, 'assertEquals' ];
86
87
		$callback0 = function ( $actual ) use ( $message, &$callCount, $asserter ) {
88
			$callCount += 1;
89
			call_user_func( $asserter, $message, $actual );
90
		};
91
92
		$callback1 = function ( $actual ) use ( $message, &$callCount, $asserter ) {
93
			$callCount += 1;
94
			call_user_func( $asserter, $message, $actual );
95
		};
96
97
		$reporter0 = new ObservableMessageReporter();
98
		$reporter0->registerReporterCallback( $callback0 );
99
100
		$reporter1 = new ObservableMessageReporter();
101
		$reporter1->registerReporterCallback( $callback1 );
102
103
		$reporter = new ObservableMessageReporter();
104
		$reporter->registerMessageReporter( $reporter0 );
105
		$reporter->registerMessageReporter( $reporter1 );
106
107
		$reporter->reportMessage( $message );
108
109
		$this->assertEquals( 2, $callCount );
110
111
		$reporter->reportMessage( $message );
112
113
		$this->assertEquals( 4, $callCount );
114
	}
115
116
	public function testDoNoFailOnNotCallableHandler() {
117
118
		$reporter = new ObservableMessageReporter();
119
120
		$reporter->registerReporterCallback( null );
121
		$reporter->registerReporterCallback( [ $this, 'functionDoesNotExist' ] );
122
123
		$callCount = 0;
124
125
		$reporter->registerReporterCallback(
126
			function ( $actual ) use ( &$callCount ) {
0 ignored issues
show
Unused Code introduced by
The parameter $actual is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
				$callCount += 1;
128
			}
129
		);
130
131
		$reporter->reportMessage( 'Foo' );
132
133
		$this->assertEquals(
134
			1,
135
			$callCount
136
		);
137
	}
138
139
}
140