Skip to content

Commit 742e750

Browse files
func0dericanhazstring
authored andcommitted
Add new commands squashed (#5)
* Add new methods for status fetching of serivces. * Fix coding standard issues with too long lines. * Fix another issue with the coding style.
1 parent 0970385 commit 742e750

File tree

4 files changed

+235
-11
lines changed

4 files changed

+235
-11
lines changed

src/Unit/AbstractUnit.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace SystemCtl\Unit;
44

5+
use Symfony\Component\Process\Process;
56
use Symfony\Component\Process\ProcessBuilder;
7+
use SystemCtl\Exception\CommandFailedException;
68

79
abstract class AbstractUnit implements UnitInterface
810
{
@@ -64,33 +66,96 @@ public function getInstanceName(): ?string
6466
*/
6567
abstract protected function execute(string $command): bool;
6668

69+
/**
70+
* @param string $command
71+
*
72+
* @return Process
73+
*/
74+
protected function runCommandAgainstService(string $command): Process
75+
{
76+
$process = $this->processBuilder
77+
->setArguments([$command, $this->getName()])
78+
->getProcess();
79+
80+
81+
$process->run();
82+
83+
return $process;
84+
}
85+
86+
/**
87+
* @return bool
88+
*/
6789
public function start(): bool
6890
{
6991
return $this->execute(__FUNCTION__);
7092
}
7193

94+
/**
95+
* @return bool
96+
*/
7297
public function stop(): bool
7398
{
7499
return $this->execute(__FUNCTION__);
75100
}
76101

102+
/**
103+
* @return bool
104+
*/
77105
public function disable(): bool
78106
{
79107
return $this->execute(__FUNCTION__);
80108
}
81109

110+
/**
111+
* @return bool
112+
*/
82113
public function reload(): bool
83114
{
84115
return $this->execute(__FUNCTION__);
85116
}
86117

118+
/**
119+
* @return bool
120+
*/
87121
public function restart(): bool
88122
{
89123
return $this->execute(__FUNCTION__);
90124
}
91125

126+
/**
127+
* @return bool
128+
*/
92129
public function enable(): bool
93130
{
94131
return $this->execute(__FUNCTION__);
95132
}
133+
134+
/**
135+
* @return bool
136+
*/
137+
public function isEnabled(): bool
138+
{
139+
$process = $this->runCommandAgainstService('is-enabled');
140+
141+
return $process->isSuccessful() && trim($process->getOutput()) === 'enabled';
142+
}
143+
144+
/**
145+
* @return bool
146+
*/
147+
public function isActive(): bool
148+
{
149+
$process = $this->runCommandAgainstService('is-active');
150+
151+
return $process->isSuccessful() && trim($process->getOutput()) === 'active';
152+
}
153+
154+
/**
155+
* @return bool
156+
*/
157+
public function isRunning(): bool
158+
{
159+
return $this->isActive();
160+
}
96161
}

src/Unit/Service.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<?php
22

3-
43
namespace SystemCtl\Unit;
54

65
use SystemCtl\Exception\CommandFailedException;
76

87
class Service extends AbstractUnit
98
{
9+
/**
10+
* @var string
11+
*/
1012
public const UNIT = 'service';
1113

14+
/**
15+
* @inheritdoc
16+
*
17+
* @throws \SystemCtl\Exception\CommandFailedException
18+
*/
1219
protected function execute(string $command): bool
1320
{
14-
$process = $this->processBuilder
15-
->setArguments([$command, $this->getName()])
16-
->getProcess();
17-
18-
$process->run();
21+
$process = $this->runCommandAgainstService($command);
1922

2023
if (!$process->isSuccessful()) {
2124
throw CommandFailedException::fromService($this->getName(), $command);

src/Unit/Timer.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66

77
class Timer extends AbstractUnit
88
{
9+
/**
10+
* @var string
11+
*/
912
public const UNIT = 'timer';
1013

14+
/**
15+
* @inheritdoc
16+
*
17+
* @throws \SystemCtl\Exception\CommandFailedException
18+
*/
1119
protected function execute(string $command): bool
1220
{
13-
$process = $this->processBuilder
14-
->setArguments([$command, $this->getName()])
15-
->getProcess();
16-
17-
$process->run();
21+
$process = $this->runCommandAgainstService($command);
1822

1923
if (!$process->isSuccessful()) {
2024
throw CommandFailedException::fromTimer($this->getName(), $command);

tests/Unit/Unit/AbstractUnitTest.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,156 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider()
145145
],
146146
];
147147
}
148+
149+
/**
150+
* @test
151+
*/
152+
public function itShouldReturnTrueIfServiceEnabledCommandRanSuccessfully()
153+
{
154+
$processBuilderStub = $this->buildProcessBuilderMock(true, 'enabled');
155+
$processBuilderStub->setArguments(['is-enabled', static::SERVICE_NAME,])->willReturn($processBuilderStub);
156+
157+
$unit = new UnitStub(static::SERVICE_NAME, $processBuilderStub->reveal());
158+
159+
$this->assertTrue($unit->isEnabled());
160+
}
161+
162+
/**
163+
* @test
164+
*/
165+
public function itShouldReturnFalseIfServiceEnabledCommandFailed()
166+
{
167+
$processBuilderStub = $this->buildProcessBuilderMock(false, 'enabled');
168+
$processBuilderStub->setArguments(['is-enabled', static::SERVICE_NAME,])->willReturn($processBuilderStub);
169+
170+
$unit = new UnitStub(static::SERVICE_NAME, $processBuilderStub->reveal());
171+
172+
$this->assertFalse($unit->isEnabled());
173+
}
174+
175+
/**
176+
* @param bool $commandSuccessful
177+
* @param string $commandOutput
178+
*
179+
* @test
180+
* @dataProvider itShouldReturnFalseIfServiceEnabledCommandOutputDoesNotEqualEnabledDataProvider
181+
*/
182+
public function itShouldReturnFalseIfServiceEnabledCommandOutputDoesNotEqualEnabled(
183+
$commandSuccessful,
184+
$commandOutput
185+
) {
186+
$processBuilderStub = $this->buildProcessBuilderMock($commandSuccessful, $commandOutput);
187+
$processBuilderStub->setArguments(['is-enabled', static::SERVICE_NAME,])->willReturn($processBuilderStub);
188+
189+
$unit = new UnitStub(static::SERVICE_NAME, $processBuilderStub->reveal());
190+
191+
$this->assertFalse($unit->isEnabled());
192+
}
193+
194+
/**
195+
* @return array
196+
*/
197+
public function itShouldReturnFalseIfServiceEnabledCommandOutputDoesNotEqualEnabledDataProvider(): array
198+
{
199+
return [
200+
[
201+
'commandSuccessful' => true,
202+
'commandOutput' => 'static',
203+
],
204+
[
205+
'commandSuccessful' => false,
206+
'commandOutput' => 'static',
207+
],
208+
[
209+
'commandSuccessful' => true,
210+
'commandOutput' => 'enable',
211+
],
212+
];
213+
}
214+
215+
/**
216+
* @test
217+
*/
218+
public function itShouldReturnTrueIfServiceActiveCommandRanSuccessfully()
219+
{
220+
$processBuilderStub = $this->buildProcessBuilderMock(true, 'active');
221+
$processBuilderStub->setArguments(['is-active', static::SERVICE_NAME,])->willReturn($processBuilderStub);
222+
223+
$unit = new UnitStub(static::SERVICE_NAME, $processBuilderStub->reveal());
224+
225+
$this->assertTrue($unit->isRunning());
226+
}
227+
228+
/**
229+
* @test
230+
*/
231+
public function itShouldReturnFalseIfServiceActiveCommandFailed()
232+
{
233+
$processBuilderStub = $this->buildProcessBuilderMock(false, 'active');
234+
$processBuilderStub->setArguments(['is-active', static::SERVICE_NAME,])->willReturn($processBuilderStub);
235+
236+
$unit = new UnitStub(static::SERVICE_NAME, $processBuilderStub->reveal());
237+
238+
$this->assertFalse($unit->isRunning());
239+
}
240+
241+
/**
242+
* @param bool $commandSuccessful
243+
* @param string $commandOutput
244+
*
245+
* @test
246+
* @dataProvider itShouldReturnFalseIfServiceActiveCommandOutputDoesNotEqualActiveDataProvider
247+
*/
248+
public function itShouldReturnFalseIfServiceActiveCommandOutputDoesNotEqualActive(
249+
$commandSuccessful,
250+
$commandOutput
251+
) {
252+
$processBuilderStub = $this->buildProcessBuilderMock($commandSuccessful, $commandOutput);
253+
$processBuilderStub->setArguments(['is-active', static::SERVICE_NAME,])->willReturn($processBuilderStub);
254+
255+
$unit = new UnitStub(static::SERVICE_NAME, $processBuilderStub->reveal());
256+
257+
$this->assertFalse($unit->isRunning());
258+
}
259+
260+
/**
261+
* @return array
262+
*/
263+
public function itShouldReturnFalseIfServiceActiveCommandOutputDoesNotEqualActiveDataProvider(): array
264+
{
265+
return [
266+
[
267+
'commandSuccessful' => true,
268+
'commandOutput' => 'static',
269+
],
270+
[
271+
'commandSuccessful' => false,
272+
'commandOutput' => 'static',
273+
],
274+
[
275+
'commandSuccessful' => true,
276+
'commandOutput' => 'enable',
277+
],
278+
];
279+
}
280+
281+
/**
282+
* @param bool $processRanSuccessful
283+
* @param string $processOutput
284+
*
285+
* @return \Prophecy\Prophecy\ObjectProphecy
286+
*/
287+
private function buildProcessBuilderMock($processRanSuccessful = true, $processOutput = ''): ObjectProphecy
288+
{
289+
$processBuilderStub = $this->prophesize(ProcessBuilder::class);
290+
291+
$processStub = $this->prophesize(Process::class);
292+
$processStub->run()->willReturn(!$processRanSuccessful);
293+
$processStub->isSuccessful()->willReturn($processRanSuccessful);
294+
$processStub->getOutput()->willReturn($processOutput);
295+
296+
$processBuilderStub->getProcess()->willReturn($processStub);
297+
298+
return $processBuilderStub;
299+
}
148300
}

0 commit comments

Comments
 (0)