GenericEventListenerCollectionTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 86
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 12 1
A testRegisterListener() 0 18 1
A testTryRegisterListenerUsingInvalidEventIdentifierThrowsException() 0 11 1
A testRegisterCallback() 0 16 1
A testTryRegisterCallbackUsingInvalidEventIdentifierThrowsException() 0 9 1
A testTryRegisterCallbackUsingInvalidCallbackThrowsException() 0 11 1
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 );
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 );
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
	}
90
91
	public function testTryRegisterCallbackUsingInvalidCallbackThrowsException() {
92
93
		$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' )
0 ignored issues
show
Unused Code introduced by
$eventListener is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
94
			->disableOriginalConstructor()
95
			->getMock();
96
97
		$instance = new GenericEventListenerCollection();
98
99
		$this->setExpectedException( 'RuntimeException' );
100
		$instance->registerCallback( 'foo', new \stdClass );
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a callable.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
	}
102
103
}
104