1
|
|
|
<?php |
2
|
|
|
namespace ivol\tests; |
3
|
|
|
|
4
|
|
|
use ivol\EventDispatcher\AfterExecuteEvent; |
5
|
|
|
use ivol\EventDispatcher\BeforeExecuteEvent; |
6
|
|
|
use ivol\ExecutionContext; |
7
|
|
|
use ivol\ExecutionWrapper; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
|
10
|
|
|
class ExecutionWrapperIntegrationTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
public function testExecuteSubscriber() |
13
|
|
|
{ |
14
|
|
|
$sut = new ExecutionWrapper(); |
15
|
|
|
$profiler = new TimeProfilerSubscriber(); |
16
|
|
|
$sut->getEventDispatcher()->addSubscriber($profiler); |
17
|
|
|
|
18
|
|
|
$result = $sut->exec('echo %s', array("'123'")); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$this->assertTrue($profiler->getProfiledTimestamp() > 0); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
class TimeProfilerSubscriber implements EventSubscriberInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** @var float */ |
28
|
|
|
private $startTime = -1; |
29
|
|
|
/** @var float */ |
30
|
|
|
private $profiledTime = -1; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
34
|
|
|
* |
35
|
|
|
* The array keys are event names and the value can be: |
36
|
|
|
* |
37
|
|
|
* * The method name to call (priority defaults to 0) |
38
|
|
|
* * An array composed of the method name to call and the priority |
39
|
|
|
* * An array of arrays composed of the method names to call and respective |
40
|
|
|
* priorities, or 0 if unset |
41
|
|
|
* |
42
|
|
|
* For instance: |
43
|
|
|
* |
44
|
|
|
* * array('eventName' => 'methodName') |
45
|
|
|
* * array('eventName' => array('methodName', $priority)) |
46
|
|
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) |
47
|
|
|
* |
48
|
|
|
* @return array The event names to listen to |
49
|
|
|
*/ |
50
|
|
|
public static function getSubscribedEvents() |
51
|
|
|
{ |
52
|
|
|
return array( |
53
|
|
|
BeforeExecuteEvent::EVENT_NAME => 'start', |
54
|
|
|
AfterExecuteEvent::EVENT_NAME => 'stop' |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function start(BeforeExecuteEvent $event) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->startTime = microtime(true); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function stop(AfterExecuteEvent $event) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->profiledTime = microtime(true) - $this->startTime; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getProfiledTimestamp() |
69
|
|
|
{ |
70
|
|
|
if ($this->profiledTime < 0) { |
71
|
|
|
throw new RuntimeException('Profiling not started yet'); |
72
|
|
|
} |
73
|
|
|
return $this->profiledTime; |
74
|
|
|
} |
75
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.