Passed
Push — test ( d838cf...cef4a5 )
by Tom
03:29
created

ProcessManagerTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 13

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreation() 0 4 1
A testZapContainersByName() 0 13 1
A testZapContainersByNameNoMatches() 0 8 1
A testFindAllContainerIdsByName() 0 8 1
A testRemove() 0 8 1
A testInvalidPrefix() 0 8 1
A testFindContainerIdByName() 0 8 1
A testFindContainerIdByNameSingleMatch() 0 8 1
A testFindContainerIdByNameMultipleMatches() 0 10 1
A testFindRunningContainerIdsByNamePrefix() 0 7 1
A testFindContainerIdByNameNoMatches() 0 8 1
A testKill() 0 8 1
A testFindAllContainerIdsByNamePrefix() 0 7 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Cli\Docker;
6
7
use Ktomk\Pipelines\Cli\Exec;
8
use Ktomk\Pipelines\Cli\ExecTester;
9
use Ktomk\Pipelines\TestCase;
10
11
/**
12
 * Class DockerProcessManagerTest
13
 *
14
 * @covers \Ktomk\Pipelines\Cli\Docker\ProcessManager
15
 */
16
class ProcessManagerTest extends TestCase
17
{
18
    public function testCreation()
19
    {
20
        $ps = new ProcessManager(new Exec());
21
        self::assertInstanceOf('Ktomk\Pipelines\Cli\Docker\ProcessManager', $ps);
22
    }
23
24
    public function testFindAllContainerIdsByNamePrefix()
25
    {
26
        $exec = new ExecTester($this);
27
        $exec->expect('capture', 'docker', 0);
28
29
        $ps = new ProcessManager($exec);
30
        self::assertIsArray($ps->findAllContainerIdsByNamePrefix('pipelines'));
31
    }
32
33
    /**
34
     */
35
    public function testInvalidPrefix()
36
    {
37
        $this->expectException('InvalidArgumentException');
38
        $this->expectExceptionMessage('Invalid container name prefix "**wrong**"');
39
40
        $exec = new ExecTester($this);
41
        $ps = new ProcessManager($exec);
42
        $ps->findAllContainerIdsByNamePrefix('**wrong**');
43
    }
44
45
    public function testFindRunningContainerIdsByNamePrefix()
46
    {
47
        $exec = new ExecTester($this);
48
        $exec->expect('capture', 'docker', 0);
49
50
        $ps = new ProcessManager($exec);
51
        self::assertIsArray($ps->findRunningContainerIdsByNamePrefix('pipelines'));
52
    }
53
54
    public function testKill()
55
    {
56
        $exec = new ExecTester($this);
57
        $exec->expect('capture', 'docker', 0);
58
59
        $ps = new ProcessManager($exec);
60
        self::assertIsInt($ps->kill(
61
            'fc2ed48d903ddba765dd89a6f82baaed4612c0c23aa2558320aad889dd29d74c'
62
        ));
63
    }
64
65
    public function testRemove()
66
    {
67
        $exec = new ExecTester($this);
68
        $exec->expect('capture', 'docker', 0);
69
70
        $ps = new ProcessManager($exec);
71
        self::assertIsInt($ps->remove(
72
            array('fc2ed48d903ddba765dd89a6f82baaed4612c0c23aa2558320aad889dd29d74c')
73
        ));
74
    }
75
76
    public function testFindAllContainerIdsByName()
77
    {
78
        $exec = new ExecTester($this);
79
        $exec->expect('capture', 'docker', 0, 'docke ps');
80
        $pm = new ProcessManager($exec);
81
        self::assertSame(
82
            array(),
83
            $pm->findAllContainerIdsByName('foo')
84
        );
85
    }
86
87
    public function testZapContainersByName()
88
    {
89
        $pm = $this->createPartialMock(
90
            'Ktomk\Pipelines\Cli\Docker\ProcessManager',
91
            array('findAllContainerIdsByName', 'kill', 'remove')
92
        );
93
        $pm->expects(self::once())
94
            ->method('kill');
95
        $pm->expects(self::once())
96
            ->method('remove');
97
        $pm->method('findAllContainerIdsByName')
98
            ->willReturn(array('1234567'));
99
        $pm->zapContainersByName('foo');
100
    }
101
102
    public function testZapContainersByNameNoMatches()
103
    {
104
        $pm = $this->createPartialMock(
105
            'Ktomk\Pipelines\Cli\Docker\ProcessManager',
106
            array('findAllContainerIdsByName')
107
        );
108
        $pm->zapContainersByName('foo');
109
        $this->addToAssertionCount(1);
110
    }
111
112
    public function testFindContainerIdByName()
113
    {
114
        $pm = $this->createPartialMock(
115
            'Ktomk\Pipelines\Cli\Docker\ProcessManager',
116
            array('findAllContainerIdsByName')
117
        );
118
        $pm->method('findAllContainerIdsByName')->willReturn(null);
119
        self::assertNull($pm->findContainerIdByName('foo'));
120
    }
121
122
    public function testFindContainerIdByNameNoMatches()
123
    {
124
        $pm = $this->createPartialMock(
125
            'Ktomk\Pipelines\Cli\Docker\ProcessManager',
126
            array('findAllContainerIdsByName')
127
        );
128
        $pm->method('findAllContainerIdsByName')->willReturn(array());
129
        self::assertNull($pm->findContainerIdByName('foo'));
130
    }
131
132
    public function testFindContainerIdByNameSingleMatch()
133
    {
134
        $pm = $this->createPartialMock(
135
            'Ktomk\Pipelines\Cli\Docker\ProcessManager',
136
            array('findAllContainerIdsByName')
137
        );
138
        $pm->method('findAllContainerIdsByName')->willReturn(array('1234567'));
139
        self::assertSame('1234567', $pm->findContainerIdByName('foo'));
140
    }
141
142
    public function testFindContainerIdByNameMultipleMatches()
143
    {
144
        $pm = $this->createPartialMock(
145
            'Ktomk\Pipelines\Cli\Docker\ProcessManager',
146
            array('findAllContainerIdsByName')
147
        );
148
        $pm->method('findAllContainerIdsByName')->willReturn(
149
            array('1234567', '3456789')
150
        );
151
        self::assertNull($pm->findContainerIdByName('foo'));
152
    }
153
}
154