1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\EventDispatcher\Tests\Listener; |
4
|
|
|
|
5
|
|
|
use Onoi\EventDispatcher\Listener\GenericEventListenerCollection; |
6
|
|
|
use Onoi\EventDispatcher\Listener\GenericCallbackEventListener; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Onoi\EventDispatcher\Listener\GenericEventListenerCollection |
10
|
|
|
* |
11
|
|
|
* @group onoi-event-dispatcher |
12
|
|
|
* |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
* @since 1.0 |
15
|
|
|
* |
16
|
|
|
* @author mwjames |
17
|
|
|
*/ |
18
|
|
|
class GenericEventListenerCollectionTest extends \PHPUnit_Framework_TestCase { |
19
|
|
|
|
20
|
|
|
public function testCanConstruct() { |
21
|
|
|
|
22
|
|
|
$this->assertInstanceOf( |
23
|
|
|
'\Onoi\EventDispatcher\EventListenerCollection', |
24
|
|
|
new GenericEventListenerCollection() |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
$this->assertInstanceOf( |
28
|
|
|
'\Onoi\EventDispatcher\Listener\GenericEventListenerCollection', |
29
|
|
|
new GenericEventListenerCollection() |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testRegisterListener() { |
34
|
|
|
|
35
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
36
|
|
|
->disableOriginalConstructor() |
37
|
|
|
->getMock(); |
38
|
|
|
|
39
|
|
|
$instance = new GenericEventListenerCollection(); |
40
|
|
|
$instance->registerListener( 'FOO', $eventListener ); |
41
|
|
|
|
42
|
|
|
$expected = array( |
43
|
|
|
'foo' => array( $eventListener ) |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->assertEquals( |
47
|
|
|
$expected, |
48
|
|
|
$instance->getCollection() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testTryRegisterListenerUsingInvalidEventIdentifierThrowsException() { |
53
|
|
|
|
54
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
55
|
|
|
->disableOriginalConstructor() |
56
|
|
|
->getMock(); |
57
|
|
|
|
58
|
|
|
$instance = new GenericEventListenerCollection(); |
59
|
|
|
|
60
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
61
|
|
|
$instance->registerListener( new \stdClass, $eventListener ); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testRegisterCallback() { |
65
|
|
|
|
66
|
|
|
$callback = function() { return 'doSomething'; }; |
67
|
|
|
|
68
|
|
|
$instance = new GenericEventListenerCollection(); |
69
|
|
|
$instance->registerCallback( 'fOo', $callback ); |
70
|
|
|
|
71
|
|
|
$expected = array( |
72
|
|
|
'foo' => array( new GenericCallbackEventListener( $callback ) ) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$this->assertEquals( |
76
|
|
|
$expected, |
77
|
|
|
$instance->getCollection() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testTryRegisterCallbackUsingInvalidEventIdentifierThrowsException() { |
82
|
|
|
|
83
|
|
|
$callback = function() { return 'doSomething'; }; |
84
|
|
|
|
85
|
|
|
$instance = new GenericEventListenerCollection(); |
86
|
|
|
|
87
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
88
|
|
|
$instance->registerCallback( new \stdClass, $callback ); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testTryRegisterCallbackUsingInvalidCallbackThrowsException() { |
92
|
|
|
|
93
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
|
|
|
|
94
|
|
|
->disableOriginalConstructor() |
95
|
|
|
->getMock(); |
96
|
|
|
|
97
|
|
|
$instance = new GenericEventListenerCollection(); |
98
|
|
|
|
99
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
100
|
|
|
$instance->registerCallback( 'foo', new \stdClass ); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: