Completed
Branch master (fee52a)
by Andrey
03:56 queued 01:55
created

EventTest::testSetAndGetEventTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Tests;
4
5
use Psr\EventManager\EventInterface;
6
use Event\Event;
7
use Tests\EventTestSuite;
8
9
final class EventTest extends EventTestSuite
10
{
11
    public function testCanEventObjectCreated()
12
    {
13
        $this->assertNotEmpty($this->event);
14
        $this->assertInstanceOf(EventInterface::class, $this->event);
15
        $this->assertObjectHasAttribute('name', $this->event);
16
        $this->assertObjectHasAttribute('params', $this->event);
17
        $this->assertObjectHasAttribute('target', $this->event);
18
        $this->assertObjectHasAttribute('isPropagationStopped', $this->event);
19
    }
20
21
    public function testSetAndGetEventParam()
22
    {
23
        $oneParam = 10;
24
        $twoParam = 'Hello';
25
        $twoParamEdited = 'World';
0 ignored issues
show
Unused Code introduced by
$twoParamEdited 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...
26
27
        $params = [
28
            'oneParam' => $oneParam,
29
            'twoParam' => $twoParam
30
        ];
31
32
        $this->event->setParams($params);
33
        
34
        $this->assertSame($this->event->getParams(), $params);
35
        $this->assertSame($this->event->getParam('oneParam'), $oneParam);
36
    }
37
38
    public function testSetAndGetEventTarget()
39
    {
40
        $target = (object) function () {
41
        };
42
43
        $this->event->setTarget($target);
44
        
45
        $this->assertSame($this->event->getTarget(), $target);
46
    }
47
48
    public function testSetAndGetEventPropagation()
49
    {
50
        $flag = true;
51
52
        $this->event->stopPropagation($flag);
53
        
54
        $this->assertTrue($this->event->isPropagationStopped());
55
    }
56
57
    public function testSetAndGetEventName()
58
    {
59
        $newName = "new.event.name";
60
61
        $this->event->setName($newName);
62
        
63
        $this->assertSame($this->event->getName(), $newName);
64
    }
65
66
    public function testSetEventInvalidNameBehavior()
67
    {
68
        $invalidName = "new%name";
69
70
        $this->assertRegExp('/[^(A-z0-9_.)]/', $invalidName);
71
72
        $this->expectException(\InvalidArgumentException::class);
73
        $this->event->setName($invalidName);
74
    }
75
}
76