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