1
|
|
|
<?php |
2
|
|
|
namespace ivol\tests; |
3
|
|
|
|
4
|
|
|
use Composer\EventDispatcher\Event; |
5
|
|
|
use ivol\Config\ConfigurationFactory; |
6
|
|
|
use ivol\EventDispatcher\AfterExecuteEvent; |
7
|
|
|
use ivol\EventDispatcher\BeforeExecuteEvent; |
8
|
|
|
use ivol\ExecutionContext; |
9
|
|
|
use ivol\ExecutionWrapper; |
10
|
|
|
use ivol\ExecutionResult; |
11
|
|
|
|
12
|
|
|
class ExecutionWrapperTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** @var ExecutionWrapper */ |
15
|
|
|
private $sut; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->sut = new ExecutionWrapper(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testExecuteReturnsResult() |
23
|
|
|
{ |
24
|
|
|
$result = $this->sut->exec('echo %s', array('123')); |
25
|
|
|
|
26
|
|
|
$this->assertEquals(0, $result->getReturnCode()); |
27
|
|
|
$this->assertEquals('123', $result->getOutput()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function testExecuteEscapesDataByDefault() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->sut = new ExecutionWrapper(); |
33
|
|
|
$result = $this->sut->exec('echo ? %s', array("'")); |
34
|
|
|
|
35
|
|
|
$this->assertEquals(0, $result->getReturnCode()); |
36
|
|
|
$this->assertEquals("? \'", $result->getOutput()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testExecuteDoesntEscapesCmdWithConfig() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$this->sut = new ExecutionWrapper(['escape_shell_cmd' => false]); |
42
|
|
|
$result = $this->sut->exec('echo ? %s', array("'")); |
43
|
|
|
|
44
|
|
|
$this->assertEquals(0, $result->getReturnCode()); |
45
|
|
|
$this->assertEquals("? '", $result->getOutput()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testExecuteNotifyBeforeAndAfterExecute() |
49
|
|
|
{ |
50
|
|
|
$eventListener = new TestListener(); |
51
|
|
|
$this->sut->getEventDispatcher()->addListener(BeforeExecuteEvent::EVENT_NAME, array($eventListener, 'before')); |
52
|
|
|
$this->sut->getEventDispatcher()->addListener(AfterExecuteEvent::EVENT_NAME, array($eventListener, 'after')); |
53
|
|
|
|
54
|
|
|
$this->sut->exec('echo %s', array('123')); |
55
|
|
|
|
56
|
|
|
$actualEvents = $eventListener->getEvents(); |
57
|
|
|
$this->assertCount(2, $actualEvents); |
58
|
|
|
$this->assertInstanceOf('ivol\EventDispatcher\BeforeExecuteEvent', $actualEvents[0]); |
59
|
|
|
$this->assertEquals(new ExecutionContext('echo %s', array('123')), $actualEvents[0]->getParams()); |
60
|
|
|
$this->assertInstanceOf('ivol\EventDispatcher\AfterExecuteEvent', $actualEvents[1]); |
61
|
|
|
$this->assertEquals(new ExecutionResult(0 , array('123')), $actualEvents[1]->getResult()); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
class TestListener |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
/** |
68
|
|
|
* @var Event[] |
69
|
|
|
*/ |
70
|
|
|
private $events = array(); |
71
|
|
|
|
72
|
|
|
public function before(BeforeExecuteEvent $event) { |
73
|
|
|
$this->events[] = $event; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function after(AfterExecuteEvent $event) { |
77
|
|
|
$this->events[] = $event; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \Composer\EventDispatcher\Event[] |
82
|
|
|
*/ |
83
|
|
|
public function getEvents() |
84
|
|
|
{ |
85
|
|
|
return $this->events; |
86
|
|
|
} |
87
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.