testExecuteEscapesDataByDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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
use ivol\tests\Helper\TestListener;
12
13
class ExecutionWrapperTest extends \PHPUnit_Framework_TestCase
14
{
15
    /** @var ExecutionWrapper */
16
    private $sut;
17
18
    protected function setUp()
19
    {
20
        $this->sut = new ExecutionWrapper();
21
    }
22
23
    public function testExecuteReturnsResult()
24
    {
25
        $result = $this->sut->exec('echo %s', array('123'));
26
27
        $this->assertEquals(0, $result->getReturnCode());
28
        $this->assertEquals('123', $result->getOutput());
29
    }
30
31 View Code Duplication
    public function testExecuteEscapesDataByDefault()
32
    {
33
        $this->sut = new ExecutionWrapper();
34
        $result = $this->sut->exec('echo ? %s', array("'"));
35
36
        $this->assertEquals(0, $result->getReturnCode());
37
        $this->assertEquals("? \'", $result->getOutput());
38
    }
39
40 View Code Duplication
    public function testExecuteDoesntEscapesCmdWithConfig()
41
    {
42
        $this->sut = new ExecutionWrapper(['escape_shell_cmd' => false]);
43
        $result = $this->sut->exec('echo ? %s', array("'"));
44
45
        $this->assertEquals(0, $result->getReturnCode());
46
        $this->assertEquals("? '", $result->getOutput());
47
    }
48
49
    public function testExecuteNotifyBeforeAndAfterExecute()
50
    {
51
        $eventListener = new TestListener();
52
        $this->sut->getEventDispatcher()->addListener(BeforeExecuteEvent::EVENT_NAME, array($eventListener, 'before'));
53
        $this->sut->getEventDispatcher()->addListener(AfterExecuteEvent::EVENT_NAME, array($eventListener, 'after'));
54
55
        $this->sut->exec('echo %s', array('123'));
56
57
        $actualEvents = $eventListener->getEvents();
58
        $this->assertCount(2, $actualEvents);
59
        $this->assertInstanceOf('ivol\EventDispatcher\BeforeExecuteEvent', $actualEvents[0]);
60
        $this->assertEquals(new ExecutionContext('echo %s', array('123')), $actualEvents[0]->getParams());
61
        $this->assertInstanceOf('ivol\EventDispatcher\AfterExecuteEvent', $actualEvents[1]);
62
        $this->assertEquals(new ExecutionResult(0, array('123')), $actualEvents[1]->getResult());
63
    }
64
}