|
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'; |
|
|
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.