Completed
Branch master (bae7d3)
by Dzmitry
05:41 queued 02:34
created

ExecutionWrapperTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testExecuteReturnsResult() 0 7 1
A testExecuteEscapesDataByDefault() 0 8 1
A testExecuteDoesntEscapesCmdWithConfig() 0 8 1
A testExecuteNotifyBeforeAndAfterExecute() 0 15 1
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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());
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
61
    }
62
}
63
64
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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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