Completed
Push — master ( e714f3...157665 )
by Andrey
02:39
created

EventTest::testCanBadEventObjectCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\EventManager\EventInterface;
7
use Event\Event;
8
use Tests\EventTestSuite;
9
10
final class EventTest extends EventTestSuite
11
{
12
    public function testCanEventObjectCreated()
13
    {
14
        $this->assertNotEmpty($this->event);
15
        $this->assertInstanceOf(EventInterface::class, $this->event);
16
        $this->assertObjectHasAttribute('name', $this->event);
17
        $this->assertObjectHasAttribute('params', $this->event);
18
        $this->assertObjectHasAttribute('target', $this->event);
19
        $this->assertObjectHasAttribute('isPropagationStopped', $this->event);
20
    }
21
22
    public function testCanBadEventObjectCreate()
23
    {
24
        $this->expectException(\Error::class);
25
        $event = new Event(); // missing required argument
0 ignored issues
show
Bug introduced by
The call to Event::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
Unused Code introduced by
$event 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
28
    public function testSetAndGetEventParam()
29
    {
30
        $oneParam = 10;
31
        $twoParam = 'Hello';
32
        $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...
33
34
        $params = [
35
            'oneParam' => $oneParam,
36
            'twoParam' => $twoParam
37
        ];
38
39
        $this->event->setParams($params);
40
        
41
        $this->assertSame($this->event->getParams(), $params);
42
        $this->assertSame($this->event->getParam('oneParam'), $oneParam);
43
    }
44
45
    public function testSetAndGetEventTarget()
46
    {
47
        $target = (object) function() {};
48
49
        $this->event->setTarget($target);
50
        
51
        $this->assertSame($this->event->getTarget(), $target);
52
    }
53
54
    public function testSetAndGetEventPropagation()
55
    {
56
        $flag = true;
57
58
        $this->event->stopPropagation($flag);
59
        
60
        $this->assertTrue($this->event->isPropagationStopped());
61
    }
62
63
    public function testSetAndGetEventName()
64
    {
65
        $newName = "new.event.name";
66
67
        $this->event->setName($newName);
68
        
69
        $this->assertSame($this->event->getName(), $newName);
70
    }
71
72
    public function testSetEventInvalidNameBehavior()
73
    {
74
        $invalidName = "new%name";
75
76
        $this->assertRegExp('/[^(A-z0-9_.)]/', $invalidName);
77
78
        $this->expectException(\InvalidArgumentException::class);
79
        $this->event->setName($invalidName);
80
    }
81
82
83
}
84