Skip to content

Commit d62b870

Browse files
peter279kicanhazstring
authored andcommitted
Resolves issue #9
1 parent 1517426 commit d62b870

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
@@ -14,6 +14,7 @@
1414
use icanhazstring\SystemCtl\Unit\Slice;
1515
use icanhazstring\SystemCtl\Unit\Swap;
1616
use icanhazstring\SystemCtl\Unit\Target;
17+
use icanhazstring\SystemCtl\Unit\Automount;
1718
use icanhazstring\SystemCtl\Unit\UnitInterface;
1819

1920
/**
@@ -443,4 +444,36 @@ public function getDevices(?string $unitPrefix = null): array
443444
return new Device($unitName, $this->getCommandDispatcher());
444445
}, $units);
445446
}
447+
448+
/**
449+
* @param string $name
450+
*
451+
* @return Automount
452+
*/
453+
public function getAutomount(string $name): Automount
454+
{
455+
$units = $this->listUnits($name, [Automount::UNIT]);
456+
457+
$unitName = $this->searchForUnitInUnits($name, $units);
458+
459+
if (is_null($unitName)) {
460+
throw UnitNotFoundException::create(Automount::UNIT, $name);
461+
}
462+
463+
return new Automount($unitName, $this->getCommandDispatcher());
464+
}
465+
466+
/**
467+
* @param null|string $unitPrefix
468+
*
469+
* @return Automount[]
470+
*/
471+
public function getAutomounts(?string $unitPrefix = null): array
472+
{
473+
$units = $this->listUnits($unitPrefix, [Automount::UNIT]);
474+
475+
return array_map(function ($unitName) {
476+
return new Automount($unitName, $this->getCommandDispatcher());
477+
}, $units);
478+
}
446479
}

src/Unit/Automount.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 Automount
7+
*
8+
* @package icanhazstring\SystemCtl\Unit
9+
*/
10+
class Automount extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'automount';
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
@@ -17,6 +17,7 @@
1717
use icanhazstring\SystemCtl\Unit\Slice;
1818
use icanhazstring\SystemCtl\Unit\Swap;
1919
use icanhazstring\SystemCtl\Unit\Target;
20+
use icanhazstring\SystemCtl\Unit\Automount;
2021

2122
/**
2223
* Class UnitTest
@@ -268,4 +269,33 @@ public function testDeviceCommandsIfProcessIsUnsuccessFulShouldRaiseException()
268269
$this->expectException(CommandFailedException::class);
269270
$device->start();
270271
}
272+
273+
public function testAutomountCommandsIfProcessIsSuccessfulShouldReturnTrue()
274+
{
275+
$command = $this->prophesize(CommandInterface::class);
276+
$command->isSuccessful()->willReturn(true);
277+
278+
$commandDispatcher = $this->createCommandDispatcherStub();
279+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
280+
281+
$automount = new Automount('AwesomeAutomount', $commandDispatcher->reveal());
282+
283+
$this->assertTrue($automount->start());
284+
$this->assertTrue($automount->stop());
285+
$this->assertTrue($automount->enable());
286+
$this->assertTrue($automount->disable());
287+
$this->assertTrue($automount->reload());
288+
$this->assertTrue($automount->restart());
289+
}
290+
291+
public function testAutomountCommandsIfProcessIsUnsuccessFulShouldRaiseException()
292+
{
293+
$commandDispatcher = $this->createCommandDispatcherStub();
294+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
295+
296+
$automount = new Automount('AwesomeAutomount', $commandDispatcher->reveal());
297+
298+
$this->expectException(CommandFailedException::class);
299+
$automount->start();
300+
}
271301
}

test/Unit/SystemCtlTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use icanhazstring\SystemCtl\Unit\Slice;
1919
use icanhazstring\SystemCtl\Unit\Swap;
2020
use icanhazstring\SystemCtl\Unit\Target;
21+
use icanhazstring\SystemCtl\Unit\Automount;
2122

2223
/**
2324
* Class SystemCtlTest
@@ -238,6 +239,25 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnSwapGet
238239
$this->assertEquals($unitName, $swap->getName());
239240
}
240241

242+
/**
243+
* @test
244+
*/
245+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnAutomountGetting()
246+
{
247+
$unitName = 'testAutomount';
248+
$output = ' testAutomount.automount Active running';
249+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
250+
$commandDispatcherStub
251+
->dispatch(...['list-units', $unitName . '*'])
252+
->willReturn($this->buildCommandStub($output));
253+
254+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
255+
256+
$automount = $systemctl->getAutomount($unitName);
257+
$this->assertInstanceOf(Automount::class, $automount);
258+
$this->assertEquals($unitName, $automount->getName());
259+
}
260+
241261
/**
242262
* @test
243263
*/
@@ -411,4 +431,21 @@ public function itShouldThrowAnExceptionIfNoDeviceCouldBeFound()
411431
$this->expectException(UnitNotFoundException::class);
412432
$systemctl->getSocket($unitName);
413433
}
434+
435+
/**
436+
* @test
437+
*/
438+
public function itShouldThrowAnExceptionIfNoAutomountCouldBeFound()
439+
{
440+
$unitName = 'testAutomount';
441+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
442+
$commandDispatcherStub
443+
->dispatch(...['list-units', $unitName . '*'])
444+
->willReturn($this->buildCommandStub(''));
445+
446+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
447+
448+
$this->expectException(UnitNotFoundException::class);
449+
$systemctl->getAutomount($unitName);
450+
}
414451
}

test/Unit/Unit/AbstractUnitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
7070
[
7171
'name' => 'test1.mount',
7272
],
73+
[
74+
'name' => 'test1.automount',
75+
],
7376
[
7477
'name' => 'test1.device',
7578
],
@@ -116,6 +119,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
116119
'name' => '[email protected]',
117120
'isMultiInstance' => true,
118121
],
122+
[
123+
'name' => '[email protected]',
124+
'isMultiInstance' => true,
125+
],
119126
[
120127
'name' => 'test1.service',
121128
'isMultiInstance' => false,

test/Unit/Utils/OutputFetcherTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
4949
nonservice.target active running
5050
superservice.mount active running
5151
awesomeservice.mount active running
52+
superservice.automount active running
53+
awesomeservice.automount active running
5254
nonservice.timer active running
5355
nonservice.socket active running
5456
nonservice.device active running
@@ -107,6 +109,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
107109
'suffix' => 'mount',
108110
'amount' => 2,
109111
],
112+
[
113+
'output' => $output,
114+
'suffix' => 'automount',
115+
'amount' => 2,
116+
],
110117
[
111118
'output' => $output,
112119
'suffix' => 'notThere',
@@ -146,6 +153,8 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
146153
a-target.target Active running
147154
super.mount Active running
148155
awesome.mount Active running
156+
super.automount Active running
157+
awesome.automount Active running
149158
nonservice.timer Active running
150159
nonservice.socket Active running
151160
nonservice.device Active running
@@ -234,6 +243,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
234243
'awesome',
235244
],
236245
],
246+
[
247+
'output' => $output,
248+
'suffix' => 'automount',
249+
'units' => [
250+
'super',
251+
'awesome',
252+
],
253+
],
237254
];
238255
}
239256
}

0 commit comments

Comments
 (0)