1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Composer\EventDispatcher\Event; |
4
|
|
|
use ivol\Config\ConfigurationFactory; |
5
|
|
|
use ivol\EventDispatcher\AfterExecuteEvent; |
6
|
|
|
use ivol\EventDispatcher\BeforeExecuteEvent; |
7
|
|
|
use ivol\ExecParams; |
8
|
|
|
use ivol\ExecutionWrapper; |
9
|
|
|
use ivol\Result; |
10
|
|
|
|
11
|
|
|
class ExecutionWrapperTest extends PHPUnit_Framework_TestCase |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** @var ExecutionWrapper */ |
14
|
|
|
private $sut; |
15
|
|
|
|
16
|
|
|
protected function setUp() |
17
|
|
|
{ |
18
|
|
|
$this->sut = new ExecutionWrapper(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testExecuteReturnsResult() |
22
|
|
|
{ |
23
|
|
|
$result = $this->sut->exec('echo %s', array('123')); |
24
|
|
|
|
25
|
|
|
$this->assertEquals(0, $result->getReturnCode()); |
26
|
|
|
$this->assertEquals('123', $result->getOutput()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testExecuteEscapesDataByDefault() |
30
|
|
|
{ |
31
|
|
|
$this->sut = new ExecutionWrapper(); |
32
|
|
|
$result = $this->sut->exec('echo ? %s', array("'")); |
33
|
|
|
|
34
|
|
|
$this->assertEquals(0, $result->getReturnCode()); |
35
|
|
|
$this->assertEquals("? \'", $result->getOutput()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testExecuteDoesntEscapesCmdWithConfig() |
39
|
|
|
{ |
40
|
|
|
$this->sut = new ExecutionWrapper(['escape_shell_cmd' => false]); |
41
|
|
|
$result = $this->sut->exec('echo ? %s', array("'")); |
42
|
|
|
|
43
|
|
|
$this->assertEquals(0, $result->getReturnCode()); |
44
|
|
|
$this->assertEquals("? '", $result->getOutput()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testExecuteNotifyBeforeAndAfterExecute() |
48
|
|
|
{ |
49
|
|
|
$eventListener = new TestListener(); |
50
|
|
|
$this->sut->getEventDispatcher()->addListener(BeforeExecuteEvent::EVENT_NAME, array($eventListener, 'before')); |
51
|
|
|
$this->sut->getEventDispatcher()->addListener(AfterExecuteEvent::EVENT_NAME, array($eventListener, 'after')); |
52
|
|
|
|
53
|
|
|
$this->sut->exec('echo %s', array('123')); |
54
|
|
|
|
55
|
|
|
$actualEvents = $eventListener->getEvents(); |
56
|
|
|
$this->assertCount(2, $actualEvents); |
57
|
|
|
$this->assertInstanceOf('ivol\EventDispatcher\BeforeExecuteEvent', $actualEvents[0]); |
58
|
|
|
$this->assertEquals(new ExecParams('echo %s', array('123')), $actualEvents[0]->getParams()); |
59
|
|
|
$this->assertInstanceOf('ivol\EventDispatcher\AfterExecuteEvent', $actualEvents[1]); |
60
|
|
|
$this->assertEquals(new Result(0 , array('123')), $actualEvents[1]->getResult()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
class TestListener |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* @var Event[] |
68
|
|
|
*/ |
69
|
|
|
private $events = array(); |
70
|
|
|
|
71
|
|
|
public function before(BeforeExecuteEvent $event) { |
72
|
|
|
$this->events[] = $event; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function after(AfterExecuteEvent $event) { |
76
|
|
|
$this->events[] = $event; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Composer\EventDispatcher\Event[] |
81
|
|
|
*/ |
82
|
|
|
public function getEvents() |
83
|
|
|
{ |
84
|
|
|
return $this->events; |
85
|
|
|
} |
86
|
|
|
} |
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.