ExecutionWrapperTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 30.77 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 5
c 2
b 2
f 0
lcom 1
cbo 6
dl 16
loc 52
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testExecuteReturnsResult() 0 7 1
A testExecuteEscapesDataByDefault() 8 8 1
A testExecuteDoesntEscapesCmdWithConfig() 8 8 1
A testExecuteNotifyBeforeAndAfterExecute() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}