1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\EventDispatcher\Tests\Listener; |
4
|
|
|
|
5
|
|
|
use Onoi\EventDispatcher\Listener\GenericCallbackEventListener; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\EventDispatcher\Listener\GenericCallbackEventListener |
9
|
|
|
* |
10
|
|
|
* @group onoi-event-dispatcher |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 1.0 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class GenericCallbackEventListenerTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testCanConstruct() { |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf( |
22
|
|
|
'\Onoi\EventDispatcher\EventListener', |
23
|
|
|
new GenericCallbackEventListener() |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->assertInstanceOf( |
27
|
|
|
'\Onoi\EventDispatcher\Listener\GenericCallbackEventListener', |
28
|
|
|
new GenericCallbackEventListener() |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testTryRegisterNonCallbackThrowsException() { |
33
|
|
|
|
34
|
|
|
$instance = new GenericCallbackEventListener(); |
35
|
|
|
|
36
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
37
|
|
|
$instance->registerCallback( 'foo' ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testRegisterClosure() { |
41
|
|
|
|
42
|
|
|
$instance = new GenericCallbackEventListener(); |
43
|
|
|
|
44
|
|
|
$testClass = $this->getMockBuilder( '\stdClass' ) |
45
|
|
|
->disableOriginalConstructor() |
46
|
|
|
->setMethods( array( 'runTest' ) ) |
47
|
|
|
->getMock(); |
48
|
|
|
|
49
|
|
|
$testClass->expects( $this->once() ) |
50
|
|
|
->method( 'runTest' ); |
51
|
|
|
|
52
|
|
|
$callback = function() use( $testClass ) { |
53
|
|
|
$testClass->runTest(); |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
$instance->registerCallback( $callback ); |
57
|
|
|
$instance->execute(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testRegisterClosureViaConstrutor() { |
61
|
|
|
|
62
|
|
|
$testClass = $this->getMockBuilder( '\stdClass' ) |
63
|
|
|
->disableOriginalConstructor() |
64
|
|
|
->setMethods( array( 'runTest' ) ) |
65
|
|
|
->getMock(); |
66
|
|
|
|
67
|
|
|
$testClass->expects( $this->once() ) |
68
|
|
|
->method( 'runTest' ); |
69
|
|
|
|
70
|
|
|
$callback = function() use( $testClass ) { |
71
|
|
|
$testClass->runTest(); |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
$instance = new GenericCallbackEventListener( $callback ); |
75
|
|
|
$instance->execute(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testRegisterExecutableCallbackViaConstrutor() { |
79
|
|
|
|
80
|
|
|
$mockTester = $this->getMockBuilder( '\stdClass' ) |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->setMethods( array( 'runTest' ) ) |
83
|
|
|
->getMock(); |
84
|
|
|
|
85
|
|
|
$mockTester->expects( $this->once() ) |
86
|
|
|
->method( 'runTest' ); |
87
|
|
|
|
88
|
|
|
$instance = new GenericCallbackEventListener( array( |
89
|
|
|
new FooMockTester( $mockTester ), 'invokedCallback' ) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$instance->execute(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testPropagationState() { |
96
|
|
|
|
97
|
|
|
$instance = new GenericCallbackEventListener(); |
98
|
|
|
|
99
|
|
|
$this->assertFalse( |
100
|
|
|
$instance->isPropagationStopped() |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$instance->setPropagationStopState( true ); |
104
|
|
|
|
105
|
|
|
$this->assertTrue( |
106
|
|
|
$instance->isPropagationStopped() |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
class FooMockTester { |
113
|
|
|
|
114
|
|
|
private $mockTester; |
115
|
|
|
|
116
|
|
|
public function __construct( $mockTester ) { |
117
|
|
|
$this->mockTester = $mockTester; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function invokedCallback() { |
121
|
|
|
$this->mockTester->runTest(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|