1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\MessageReporter\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Onoi\MessageReporter\ObservableMessageReporter; |
6
|
|
|
use Onoi\MessageReporter\MessageReporter; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \Onoi\MessageReporter\ObservableMessageReporter |
11
|
|
|
* @group onoi-message-reporter |
12
|
|
|
* |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
*/ |
15
|
|
|
class ObservableMessageReporterTest extends MessageReporterTestCase { |
16
|
|
|
|
17
|
|
|
public function testCanConstruct() { |
18
|
|
|
|
19
|
|
|
$this->assertInstanceOf( |
20
|
|
|
'\Onoi\MessageReporter\ObservableMessageReporter', |
21
|
|
|
new ObservableMessageReporter() |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return MessageReporter[] |
27
|
|
|
*/ |
28
|
|
|
public function getInstances() { |
29
|
|
|
$instances = []; |
30
|
|
|
|
31
|
|
|
$instances[] = new ObservableMessageReporter(); |
32
|
|
|
|
33
|
|
|
$reporter = new ObservableMessageReporter(); |
34
|
|
|
$reporter->registerMessageReporter( new ObservableMessageReporter() ); |
35
|
|
|
$callback0 = function ( $string ) {}; |
|
|
|
|
36
|
|
|
$callback1 = function ( $string ) {}; |
|
|
|
|
37
|
|
|
$instances[] = $reporter; |
38
|
|
|
|
39
|
|
|
$reporter = clone $reporter; |
40
|
|
|
$reporter->registerReporterCallback( $callback0 ); |
41
|
|
|
$reporter->registerReporterCallback( $callback1 ); |
42
|
|
|
$instances[] = $reporter; |
43
|
|
|
|
44
|
|
|
return $instances; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider reportMessageProvider |
49
|
|
|
* |
50
|
|
|
* @param string $message |
51
|
|
|
*/ |
52
|
|
|
public function testCallbackInvocation( $message ) { |
53
|
|
|
$callCount = 0; |
54
|
|
|
$asserter = [ $this, 'assertEquals' ]; |
55
|
|
|
|
56
|
|
|
$callback0 = function ( $actual ) use ( $message, &$callCount, $asserter ) { |
57
|
|
|
$callCount += 1; |
58
|
|
|
call_user_func( $asserter, $message, $actual ); |
59
|
|
|
}; |
60
|
|
|
|
61
|
|
|
$callback1 = function ( $actual ) use ( $message, &$callCount, $asserter ) { |
62
|
|
|
$callCount += 1; |
63
|
|
|
call_user_func( $asserter, $message, $actual ); |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
$reporter = new ObservableMessageReporter(); |
67
|
|
|
$reporter->registerReporterCallback( $callback0 ); |
68
|
|
|
$reporter->registerReporterCallback( $callback1 ); |
69
|
|
|
|
70
|
|
|
$reporter->reportMessage( $message ); |
71
|
|
|
|
72
|
|
|
$this->assertEquals( 2, $callCount ); |
73
|
|
|
|
74
|
|
|
$reporter->reportMessage( $message ); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( 4, $callCount ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @dataProvider reportMessageProvider |
81
|
|
|
* |
82
|
|
|
* @param string $message |
83
|
|
|
*/ |
84
|
|
|
public function testReporterInvocation( $message ) { |
85
|
|
|
$callCount = 0; |
86
|
|
|
$asserter = [ $this, 'assertEquals' ]; |
87
|
|
|
|
88
|
|
|
$callback0 = function ( $actual ) use ( $message, &$callCount, $asserter ) { |
89
|
|
|
$callCount += 1; |
90
|
|
|
call_user_func( $asserter, $message, $actual ); |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
$callback1 = function ( $actual ) use ( $message, &$callCount, $asserter ) { |
94
|
|
|
$callCount += 1; |
95
|
|
|
call_user_func( $asserter, $message, $actual ); |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
$reporter0 = new ObservableMessageReporter(); |
99
|
|
|
$reporter0->registerReporterCallback( $callback0 ); |
100
|
|
|
|
101
|
|
|
$reporter1 = new ObservableMessageReporter(); |
102
|
|
|
$reporter1->registerReporterCallback( $callback1 ); |
103
|
|
|
|
104
|
|
|
$reporter = new ObservableMessageReporter(); |
105
|
|
|
$reporter->registerMessageReporter( $reporter0 ); |
106
|
|
|
$reporter->registerMessageReporter( $reporter1 ); |
107
|
|
|
|
108
|
|
|
$reporter->reportMessage( $message ); |
109
|
|
|
|
110
|
|
|
$this->assertEquals( 2, $callCount ); |
111
|
|
|
|
112
|
|
|
$reporter->reportMessage( $message ); |
113
|
|
|
|
114
|
|
|
$this->assertEquals( 4, $callCount ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testDoNoFailOnNotCallableHandler() { |
118
|
|
|
|
119
|
|
|
$reporter = new ObservableMessageReporter(); |
120
|
|
|
|
121
|
|
|
$reporter->registerReporterCallback( null ); |
122
|
|
|
$reporter->registerReporterCallback( [ $this, 'functionDoesNotExist' ] ); |
123
|
|
|
|
124
|
|
|
$callCount = 0; |
125
|
|
|
|
126
|
|
|
$reporter->registerReporterCallback( |
127
|
|
|
function ( $actual ) use ( &$callCount ) { |
|
|
|
|
128
|
|
|
$callCount += 1; |
129
|
|
|
} |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$reporter->reportMessage( 'Foo' ); |
133
|
|
|
|
134
|
|
|
$this->assertEquals( |
135
|
|
|
1, |
136
|
|
|
$callCount |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.