From a4ccae89b903e0b62238365c3cbdbb7fe966cce4 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 8 Nov 2020 18:42:52 +0800 Subject: [PATCH] Resolves issue #8 --- src/SystemCtl.php | 34 ++++++++++++++++++++++++ src/Unit/Mount.php | 24 +++++++++++++++++ test/Integration/Unit/UnitTest.php | 30 ++++++++++++++++++++++ test/Unit/SystemCtlTest.php | 37 +++++++++++++++++++++++++++ test/Unit/Unit/AbstractUnitTest.php | 11 ++++++++ test/Unit/Utils/OutputFetcherTest.php | 8 +++++- 6 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/Unit/Mount.php diff --git a/src/SystemCtl.php b/src/SystemCtl.php index 8b02371..0352e3b 100644 --- a/src/SystemCtl.php +++ b/src/SystemCtl.php @@ -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; /** @@ -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); + } } diff --git a/src/Unit/Mount.php b/src/Unit/Mount.php new file mode 100644 index 0000000..62c94f8 --- /dev/null +++ b/src/Unit/Mount.php @@ -0,0 +1,24 @@ +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(); + } } diff --git a/test/Unit/SystemCtlTest.php b/test/Unit/SystemCtlTest.php index 175079c..7cc26a2 100644 --- a/test/Unit/SystemCtlTest.php +++ b/test/Unit/SystemCtlTest.php @@ -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 @@ -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 */ @@ -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); + } } diff --git a/test/Unit/Unit/AbstractUnitTest.php b/test/Unit/Unit/AbstractUnitTest.php index 341e8be..09dd662 100644 --- a/test/Unit/Unit/AbstractUnitTest.php +++ b/test/Unit/Unit/AbstractUnitTest.php @@ -73,6 +73,9 @@ public function itShouldReturnCorrectNameDataProvider(): array [ 'name' => 'test1.automount', ], + [ + 'name' => 'test1.mount', + ], [ 'name' => 'test1.device', ], @@ -155,6 +158,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array 'name' => 'test1.target', 'isMultiInstance' => false, ], + [ + 'name' => 'test1.mount', + 'isMultiInstance' => false, + ], ]; } @@ -223,6 +230,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra 'name' => 'test1.target', 'instanceName' => null, ], + [ + 'name' => 'test1.mount', + 'instanceName' => null, + ], ]; } diff --git a/test/Unit/Utils/OutputFetcherTest.php b/test/Unit/Utils/OutputFetcherTest.php index 9c10507..18c35ed 100644 --- a/test/Unit/Utils/OutputFetcherTest.php +++ b/test/Unit/Utils/OutputFetcherTest.php @@ -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 @@ -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 ● failed-service@foo.service loaded failed failed @@ -107,7 +109,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array [ 'output' => $output, 'suffix' => 'mount', - 'amount' => 2, + 'amount' => 4, ], [ 'output' => $output, @@ -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 @@ -162,6 +165,7 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array nonservice.slice Active running nonservice.swap Active running nonservice.target Active running + nonservice.mount Active running instance-service@1.service Active running instance-service@foo.service Active running ● failed-service@foo.service loaded failed failed @@ -239,8 +243,10 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array 'output' => $output, 'suffix' => 'mount', 'units' => [ + 'a-mount', 'super', 'awesome', + 'nonservice', ], ], [