Skip to content

Commit ffd4f62

Browse files
RageZBlaOlivier Lechevalier
andauthored
Add Unit device (#33)
* Add Unit device * Fix coding standard errors Co-authored-by: Olivier Lechevalier <[email protected]>
1 parent 6adc819 commit ffd4f62

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

src/SystemCtl.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use icanhazstring\SystemCtl\Command\SymfonyCommandDispatcher;
77
use icanhazstring\SystemCtl\Exception\UnitNotFoundException;
88
use icanhazstring\SystemCtl\Exception\UnitTypeNotSupportedException;
9+
use icanhazstring\SystemCtl\Unit\Device;
910
use icanhazstring\SystemCtl\Unit\Service;
1011
use icanhazstring\SystemCtl\Unit\Timer;
1112
use icanhazstring\SystemCtl\Unit\Socket;
@@ -311,4 +312,36 @@ public function setCommandDispatcher(CommandDispatcherInterface $dispatcher)
311312

312313
return $this;
313314
}
315+
316+
/**
317+
* @param string $name
318+
*
319+
* @return Device
320+
*/
321+
public function getDevice(string $name): Device
322+
{
323+
$units = $this->listUnits($name, [Device::UNIT]);
324+
325+
$unitName = $this->searchForUnitInUnits($name, $units);
326+
327+
if (is_null($unitName)) {
328+
throw UnitNotFoundException::create(Device::UNIT, $name);
329+
}
330+
331+
return new Device($unitName, $this->getCommandDispatcher());
332+
}
333+
334+
/**
335+
* @param string|null $unitPrefix
336+
*
337+
* @return Device[]
338+
*/
339+
public function getDevices(?string $unitPrefix = null): array
340+
{
341+
$units = $this->listUnits($unitPrefix, [Device::UNIT]);
342+
343+
return array_map(function ($unitName) {
344+
return new Device($unitName, $this->getCommandDispatcher());
345+
}, $units);
346+
}
314347
}

src/Unit/Device.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace icanhazstring\SystemCtl\Unit;
4+
5+
/**
6+
* Class Device
7+
*
8+
* @package icanhazstring\SystemCtl\Unit
9+
*/
10+
class Device extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'device';
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected function getUnitSuffix(): string
21+
{
22+
return static::UNIT;
23+
}
24+
}

test/Integration/Unit/UnitTest.php

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

33
namespace icanhazstring\SystemCtl\Test\Integration\Unit;
44

5+
use icanhazstring\SystemCtl\Unit\Device;
56
use PHPUnit\Framework\TestCase;
67
use Prophecy\Argument;
78
use Prophecy\Prophecy\ObjectProphecy;
@@ -144,4 +145,33 @@ public function testScopeCommandsIfProcessIsUnsuccessFulShouldRaiseException():
144145
$this->expectException(CommandFailedException::class);
145146
$scope->start();
146147
}
148+
149+
public function testDeviceCommandsIfProcessIsSuccessfulShouldReturnTrue()
150+
{
151+
$command = $this->prophesize(CommandInterface::class);
152+
$command->isSuccessful()->willReturn(true);
153+
154+
$commandDispatcher = $this->createCommandDispatcherStub();
155+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
156+
157+
$device = new Device('AwesomeDevice', $commandDispatcher->reveal());
158+
159+
$this->assertTrue($device->start());
160+
$this->assertTrue($device->stop());
161+
$this->assertTrue($device->enable());
162+
$this->assertTrue($device->disable());
163+
$this->assertTrue($device->reload());
164+
$this->assertTrue($device->restart());
165+
}
166+
167+
public function testDeviceCommandsIfProcessIsUnsuccessFulShouldRaiseException()
168+
{
169+
$commandDispatcher = $this->createCommandDispatcherStub();
170+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
171+
172+
$device = new Device('AwesomeDevice', $commandDispatcher->reveal());
173+
174+
$this->expectException(CommandFailedException::class);
175+
$device->start();
176+
}
147177
}

test/Unit/SystemCtlTest.php

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

33
namespace icanhazstring\SystemCtl\Test\Unit;
44

5+
use icanhazstring\SystemCtl\Unit\Device;
56
use PHPUnit\Framework\TestCase;
67
use Prophecy\Argument;
78
use Prophecy\Prophecy\ObjectProphecy;
@@ -261,4 +262,40 @@ public function itShouldReturnATimerWithTheCorrectNameOnTimerGetting(): void
261262
$service = $systemctl->getService($unitName);
262263
$this->assertEquals('testService', $service->getName());
263264
}
265+
266+
/**
267+
* @test
268+
*/
269+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnDeviceGetting()
270+
{
271+
$unitName = 'testDevice';
272+
$output = ' testDevice.device Active running';
273+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
274+
$commandDispatcherStub
275+
->dispatch(...['list-units', $unitName . '*'])
276+
->willReturn($this->buildCommandStub($output));
277+
278+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
279+
280+
$device = $systemctl->getDevice($unitName);
281+
$this->assertInstanceOf(Device::class, $device);
282+
$this->assertEquals($unitName, $device->getName());
283+
}
284+
285+
/**
286+
* @test
287+
*/
288+
public function itShouldThrowAnExceptionIfNoDeviceCouldBeFound()
289+
{
290+
$unitName = 'testDevice';
291+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
292+
$commandDispatcherStub
293+
->dispatch(...['list-units', $unitName . '*'])
294+
->willReturn($this->buildCommandStub(''));
295+
296+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
297+
298+
$this->expectException(UnitNotFoundException::class);
299+
$systemctl->getSocket($unitName);
300+
}
264301
}

test/Unit/Unit/AbstractUnitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
5858
[
5959
'name' => 'test1.mount',
6060
],
61+
[
62+
'name' => 'test1.device',
63+
],
6164
[
6265
'name' => '[email protected]',
6366
],
@@ -113,6 +116,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
113116
'name' => 'test1.socket',
114117
'isMultiInstance' => false,
115118
],
119+
[
120+
'name' => 'test1.device',
121+
'isMultiInstance' => false,
122+
],
116123
[
117124
'name' => 'test1.scope',
118125
'isMultiInstance' => false,

test/Unit/Utils/OutputFetcherTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
3939
awesomeservice.service active running
4040
nonservice.timer active running
4141
nonservice.socket active running
42+
nonservice.device active running
4243
nonservice.scope active running
4344
superservice.mount active running
4445
awesomeservice.mount active running
4546
nonservice.timer active running
4647
nonservice.socket active running
48+
nonservice.device active running
4749
nonservice.scope active running
4850
superservice.service active running
4951
awesomeservice.service active running
@@ -66,6 +68,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
6668
'suffix' => 'socket',
6769
'amount' => 2,
6870
],
71+
[
72+
'output' => $output,
73+
'suffix' => 'device',
74+
'amount' => 2,
75+
],
6976
[
7077
'output' => $output,
7178
'suffix' => 'scope',
@@ -108,11 +115,13 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
108115
foo-bar.service Active running
109116
a-timer.timer Active running
110117
a-socket.socket Active running
118+
a-device.device Active running
111119
a-scope.scope Active running
112120
super.mount Active running
113121
awesome.mount Active running
114122
nonservice.timer Active running
115123
nonservice.socket Active running
124+
nonservice.device Active running
116125
nonservice.scope Active running
117126
[email protected] Active running
118127
[email protected] Active running
@@ -139,6 +148,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
139148
'nonservice',
140149
],
141150
],
151+
[
152+
'output' => $output,
153+
'suffix' => 'device',
154+
'units' => [
155+
'a-device',
156+
'nonservice',
157+
],
158+
],
142159
[
143160
'output' => $output,
144161
'suffix' => 'socket',

0 commit comments

Comments
 (0)