Completed
Push — master ( 106c7e...3c0ece )
by Dzmitry
06:55
created

testExecuteDoesntEscapesCmdWithConfig()   A

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
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()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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());
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
Documentation introduced by
array('123') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
}
64
65
class TestListener
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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
}