Skip to content

Commit

Permalink
Resolves issue icanhazstring#8
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k authored and icanhazstring committed Nov 8, 2020
1 parent d62b870 commit a4ccae8
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/SystemCtl.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use icanhazstring\SystemCtl\Unit\Swap;
use icanhazstring\SystemCtl\Unit\Target;
use icanhazstring\SystemCtl\Unit\Automount;
use icanhazstring\SystemCtl\Unit\Mount;
use icanhazstring\SystemCtl\Unit\UnitInterface;

/**
Expand Down Expand Up @@ -476,4 +477,37 @@ public function getAutomounts(?string $unitPrefix = null): array
return new Automount($unitName, $this->getCommandDispatcher());
}, $units);
}


/**
* @param string $name
*
* @return Mount
*/
public function getMount(string $name): Mount
{
$units = $this->listUnits($name, [Mount::UNIT]);

$unitName = $this->searchForUnitInUnits($name, $units);

if (is_null($unitName)) {
throw UnitNotFoundException::create(Mount::UNIT, $name);
}

return new Mount($unitName, $this->getCommandDispatcher());
}

/**
* @param null|string $unitPrefix
*
* @return Mount[]
*/
public function getMounts(?string $unitPrefix = null): array
{
$units = $this->listUnits($unitPrefix, [Mount::UNIT]);

return array_map(function ($unitName) {
return new Mount($unitName, $this->getCommandDispatcher());
}, $units);
}
}
24 changes: 24 additions & 0 deletions src/Unit/Mount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace icanhazstring\SystemCtl\Unit;

/**
* Class Mount
*
* @package icanhazstring\SystemCtl\Unit
*/
class Mount extends AbstractUnit
{
/**
* @var string
*/
public const UNIT = 'mount';

/**
* @inheritdoc
*/
protected function getUnitSuffix(): string
{
return static::UNIT;
}
}
30 changes: 30 additions & 0 deletions test/Integration/Unit/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use icanhazstring\SystemCtl\Unit\Swap;
use icanhazstring\SystemCtl\Unit\Target;
use icanhazstring\SystemCtl\Unit\Automount;
use icanhazstring\SystemCtl\Unit\Mount;

/**
* Class UnitTest
Expand Down Expand Up @@ -298,4 +299,33 @@ public function testAutomountCommandsIfProcessIsUnsuccessFulShouldRaiseException
$this->expectException(CommandFailedException::class);
$automount->start();
}

public function testMountCommandsIfProcessIsSuccessfulShouldReturnTrue()
{
$command = $this->prophesize(CommandInterface::class);
$command->isSuccessful()->willReturn(true);

$commandDispatcher = $this->createCommandDispatcherStub();
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);

$mount = new Mount('AwesomeMount', $commandDispatcher->reveal());

$this->assertTrue($mount->start());
$this->assertTrue($mount->stop());
$this->assertTrue($mount->enable());
$this->assertTrue($mount->disable());
$this->assertTrue($mount->reload());
$this->assertTrue($mount->restart());
}

public function testMountCommandsIfProcessIsUnsuccessFulShouldRaiseException()
{
$commandDispatcher = $this->createCommandDispatcherStub();
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);

$mount = new Mount('AwesomeMount', $commandDispatcher->reveal());

$this->expectException(CommandFailedException::class);
$mount->start();
}
}
37 changes: 37 additions & 0 deletions test/Unit/SystemCtlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use icanhazstring\SystemCtl\Unit\Swap;
use icanhazstring\SystemCtl\Unit\Target;
use icanhazstring\SystemCtl\Unit\Automount;
use icanhazstring\SystemCtl\Unit\Mount;

/**
* Class SystemCtlTest
Expand Down Expand Up @@ -108,6 +109,25 @@ public function itShouldReturnAServiceWithTheCorrectNameOnServiceGetting(): void
self::assertEquals('testService', $service->getName());
}

/**
* @test
*/
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnMountGetting()
{
$unitName = 'testMount';
$output = ' testMount.mount Active running';
$commandDispatcherStub = $this->buildCommandDispatcherStub();
$commandDispatcherStub
->dispatch(...['list-units', $unitName . '*'])
->willReturn($this->buildCommandStub($output));

$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());

$mount = $systemctl->getMount($unitName);
$this->assertInstanceOf(Mount::class, $mount);
$this->assertEquals($unitName, $mount->getName());
}

/**
* @test
*/
Expand Down Expand Up @@ -448,4 +468,21 @@ public function itShouldThrowAnExceptionIfNoAutomountCouldBeFound()
$this->expectException(UnitNotFoundException::class);
$systemctl->getAutomount($unitName);
}

/**
* @test
*/
public function itShouldThrowAnExceptionIfNoMountCouldBeFound()
{
$unitName = 'testMount';
$commandDispatcherStub = $this->buildCommandDispatcherStub();
$commandDispatcherStub
->dispatch(...['list-units', $unitName . '*'])
->willReturn($this->buildCommandStub(''));

$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());

$this->expectException(UnitNotFoundException::class);
$systemctl->getMount($unitName);
}
}
11 changes: 11 additions & 0 deletions test/Unit/Unit/AbstractUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
[
'name' => 'test1.automount',
],
[
'name' => 'test1.mount',
],
[
'name' => 'test1.device',
],
Expand Down Expand Up @@ -155,6 +158,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
'name' => 'test1.target',
'isMultiInstance' => false,
],
[
'name' => 'test1.mount',
'isMultiInstance' => false,
],
];
}

Expand Down Expand Up @@ -223,6 +230,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra
'name' => 'test1.target',
'instanceName' => null,
],
[
'name' => 'test1.mount',
'instanceName' => null,
],
];
}

Expand Down
8 changes: 7 additions & 1 deletion test/Unit/Utils/OutputFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
nonservice.slice active running
nonservice.swap active running
nonservice.target active running
nonservice.mount active running
superservice.mount active running
awesomeservice.mount active running
superservice.automount active running
Expand All @@ -58,6 +59,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
nonservice.slice active running
nonservice.swap active running
nonservice.target active running
nonservice.mount active running
superservice.service active running
awesomeservice.service active running
[email protected] loaded failed failed
Expand Down Expand Up @@ -107,7 +109,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
[
'output' => $output,
'suffix' => 'mount',
'amount' => 2,
'amount' => 4,
],
[
'output' => $output,
Expand Down Expand Up @@ -151,6 +153,7 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
a-slice.slice Active running
a-swap.swap Active running
a-target.target Active running
a-mount.mount Active running
super.mount Active running
awesome.mount Active running
super.automount Active running
Expand All @@ -162,6 +165,7 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
nonservice.slice Active running
nonservice.swap Active running
nonservice.target Active running
nonservice.mount Active running
[email protected] Active running
[email protected] Active running
[email protected] loaded failed failed
Expand Down Expand Up @@ -239,8 +243,10 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
'output' => $output,
'suffix' => 'mount',
'units' => [
'a-mount',
'super',
'awesome',
'nonservice',
],
],
[
Expand Down

0 comments on commit a4ccae8

Please sign in to comment.