Skip to content

Commit a4ccae8

Browse files
peter279kicanhazstring
authored andcommitted
Resolves issue #8
1 parent d62b870 commit a4ccae8

File tree

6 files changed

+143
-1
lines changed

6 files changed

+143
-1
lines changed

src/SystemCtl.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use icanhazstring\SystemCtl\Unit\Swap;
1616
use icanhazstring\SystemCtl\Unit\Target;
1717
use icanhazstring\SystemCtl\Unit\Automount;
18+
use icanhazstring\SystemCtl\Unit\Mount;
1819
use icanhazstring\SystemCtl\Unit\UnitInterface;
1920

2021
/**
@@ -476,4 +477,37 @@ public function getAutomounts(?string $unitPrefix = null): array
476477
return new Automount($unitName, $this->getCommandDispatcher());
477478
}, $units);
478479
}
480+
481+
482+
/**
483+
* @param string $name
484+
*
485+
* @return Mount
486+
*/
487+
public function getMount(string $name): Mount
488+
{
489+
$units = $this->listUnits($name, [Mount::UNIT]);
490+
491+
$unitName = $this->searchForUnitInUnits($name, $units);
492+
493+
if (is_null($unitName)) {
494+
throw UnitNotFoundException::create(Mount::UNIT, $name);
495+
}
496+
497+
return new Mount($unitName, $this->getCommandDispatcher());
498+
}
499+
500+
/**
501+
* @param null|string $unitPrefix
502+
*
503+
* @return Mount[]
504+
*/
505+
public function getMounts(?string $unitPrefix = null): array
506+
{
507+
$units = $this->listUnits($unitPrefix, [Mount::UNIT]);
508+
509+
return array_map(function ($unitName) {
510+
return new Mount($unitName, $this->getCommandDispatcher());
511+
}, $units);
512+
}
479513
}

src/Unit/Mount.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 Mount
7+
*
8+
* @package icanhazstring\SystemCtl\Unit
9+
*/
10+
class Mount extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'mount';
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
@@ -18,6 +18,7 @@
1818
use icanhazstring\SystemCtl\Unit\Swap;
1919
use icanhazstring\SystemCtl\Unit\Target;
2020
use icanhazstring\SystemCtl\Unit\Automount;
21+
use icanhazstring\SystemCtl\Unit\Mount;
2122

2223
/**
2324
* Class UnitTest
@@ -298,4 +299,33 @@ public function testAutomountCommandsIfProcessIsUnsuccessFulShouldRaiseException
298299
$this->expectException(CommandFailedException::class);
299300
$automount->start();
300301
}
302+
303+
public function testMountCommandsIfProcessIsSuccessfulShouldReturnTrue()
304+
{
305+
$command = $this->prophesize(CommandInterface::class);
306+
$command->isSuccessful()->willReturn(true);
307+
308+
$commandDispatcher = $this->createCommandDispatcherStub();
309+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
310+
311+
$mount = new Mount('AwesomeMount', $commandDispatcher->reveal());
312+
313+
$this->assertTrue($mount->start());
314+
$this->assertTrue($mount->stop());
315+
$this->assertTrue($mount->enable());
316+
$this->assertTrue($mount->disable());
317+
$this->assertTrue($mount->reload());
318+
$this->assertTrue($mount->restart());
319+
}
320+
321+
public function testMountCommandsIfProcessIsUnsuccessFulShouldRaiseException()
322+
{
323+
$commandDispatcher = $this->createCommandDispatcherStub();
324+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
325+
326+
$mount = new Mount('AwesomeMount', $commandDispatcher->reveal());
327+
328+
$this->expectException(CommandFailedException::class);
329+
$mount->start();
330+
}
301331
}

test/Unit/SystemCtlTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use icanhazstring\SystemCtl\Unit\Swap;
2020
use icanhazstring\SystemCtl\Unit\Target;
2121
use icanhazstring\SystemCtl\Unit\Automount;
22+
use icanhazstring\SystemCtl\Unit\Mount;
2223

2324
/**
2425
* Class SystemCtlTest
@@ -108,6 +109,25 @@ public function itShouldReturnAServiceWithTheCorrectNameOnServiceGetting(): void
108109
self::assertEquals('testService', $service->getName());
109110
}
110111

112+
/**
113+
* @test
114+
*/
115+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnMountGetting()
116+
{
117+
$unitName = 'testMount';
118+
$output = ' testMount.mount Active running';
119+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
120+
$commandDispatcherStub
121+
->dispatch(...['list-units', $unitName . '*'])
122+
->willReturn($this->buildCommandStub($output));
123+
124+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
125+
126+
$mount = $systemctl->getMount($unitName);
127+
$this->assertInstanceOf(Mount::class, $mount);
128+
$this->assertEquals($unitName, $mount->getName());
129+
}
130+
111131
/**
112132
* @test
113133
*/
@@ -448,4 +468,21 @@ public function itShouldThrowAnExceptionIfNoAutomountCouldBeFound()
448468
$this->expectException(UnitNotFoundException::class);
449469
$systemctl->getAutomount($unitName);
450470
}
471+
472+
/**
473+
* @test
474+
*/
475+
public function itShouldThrowAnExceptionIfNoMountCouldBeFound()
476+
{
477+
$unitName = 'testMount';
478+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
479+
$commandDispatcherStub
480+
->dispatch(...['list-units', $unitName . '*'])
481+
->willReturn($this->buildCommandStub(''));
482+
483+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
484+
485+
$this->expectException(UnitNotFoundException::class);
486+
$systemctl->getMount($unitName);
487+
}
451488
}

test/Unit/Unit/AbstractUnitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
7373
[
7474
'name' => 'test1.automount',
7575
],
76+
[
77+
'name' => 'test1.mount',
78+
],
7679
[
7780
'name' => 'test1.device',
7881
],
@@ -155,6 +158,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
155158
'name' => 'test1.target',
156159
'isMultiInstance' => false,
157160
],
161+
[
162+
'name' => 'test1.mount',
163+
'isMultiInstance' => false,
164+
],
158165
];
159166
}
160167

@@ -223,6 +230,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra
223230
'name' => 'test1.target',
224231
'instanceName' => null,
225232
],
233+
[
234+
'name' => 'test1.mount',
235+
'instanceName' => null,
236+
],
226237
];
227238
}
228239

test/Unit/Utils/OutputFetcherTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
4747
nonservice.slice active running
4848
nonservice.swap active running
4949
nonservice.target active running
50+
nonservice.mount active running
5051
superservice.mount active running
5152
awesomeservice.mount active running
5253
superservice.automount active running
@@ -58,6 +59,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
5859
nonservice.slice active running
5960
nonservice.swap active running
6061
nonservice.target active running
62+
nonservice.mount active running
6163
superservice.service active running
6264
awesomeservice.service active running
6365
[email protected] loaded failed failed
@@ -107,7 +109,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
107109
[
108110
'output' => $output,
109111
'suffix' => 'mount',
110-
'amount' => 2,
112+
'amount' => 4,
111113
],
112114
[
113115
'output' => $output,
@@ -151,6 +153,7 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
151153
a-slice.slice Active running
152154
a-swap.swap Active running
153155
a-target.target Active running
156+
a-mount.mount Active running
154157
super.mount Active running
155158
awesome.mount Active running
156159
super.automount Active running
@@ -162,6 +165,7 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
162165
nonservice.slice Active running
163166
nonservice.swap Active running
164167
nonservice.target Active running
168+
nonservice.mount Active running
165169
[email protected] Active running
166170
[email protected] Active running
167171
[email protected] loaded failed failed
@@ -239,8 +243,10 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
239243
'output' => $output,
240244
'suffix' => 'mount',
241245
'units' => [
246+
'a-mount',
242247
'super',
243248
'awesome',
249+
'nonservice',
244250
],
245251
],
246252
[

0 commit comments

Comments
 (0)