Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves issue #12 #36

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/SystemCtl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use icanhazstring\SystemCtl\Unit\Timer;
use icanhazstring\SystemCtl\Unit\Socket;
use icanhazstring\SystemCtl\Unit\Scope;
use icanhazstring\SystemCtl\Unit\Slice;
use icanhazstring\SystemCtl\Unit\UnitInterface;

/**
Expand Down Expand Up @@ -263,6 +264,38 @@ public function getScopes(?string $unitPrefix = null): array
}, $units);
}

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

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

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

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

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

return array_map(function ($unitName) {
return new Slice($unitName, $this->getCommandDispatcher());
}, $units);
}

/**
* Restart the daemon to reload specs and new units
*
Expand Down
24 changes: 24 additions & 0 deletions src/Unit/Slice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace icanhazstring\SystemCtl\Unit;

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

/**
* @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 @@ -14,6 +14,7 @@
use icanhazstring\SystemCtl\Unit\Timer;
use icanhazstring\SystemCtl\Unit\Socket;
use icanhazstring\SystemCtl\Unit\Scope;
use icanhazstring\SystemCtl\Unit\Slice;

/**
* Class UnitTest
Expand Down Expand Up @@ -149,6 +150,35 @@ public function testScopeCommandsIfProcessIsUnsuccessFulShouldRaiseException():
$scope->start();
}

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

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

$slice = new Slice('AwesomeSlice', $commandDispatcher->reveal());

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

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

$slice = new Slice('AwesomeSlice', $commandDispatcher->reveal());

$this->expectException(CommandFailedException::class);
$slice->start();
}

public function testDeviceCommandsIfProcessIsSuccessfulShouldReturnTrue()
{
$command = $this->prophesize(CommandInterface::class);
Expand Down
37 changes: 37 additions & 0 deletions test/Unit/SystemCtlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use icanhazstring\SystemCtl\Unit\Timer;
use icanhazstring\SystemCtl\Unit\Socket;
use icanhazstring\SystemCtl\Unit\Scope;
use icanhazstring\SystemCtl\Unit\Slice;

/**
* Class SystemCtlTest
Expand Down Expand Up @@ -178,6 +179,25 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnScopeGe
self::assertEquals($unitName, $scope->getName());
}

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

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

$slice = $systemctl->getSlice($unitName);
$this->assertInstanceOf(Slice::class, $slice);
$this->assertEquals($unitName, $slice->getName());
}

/**
* @test
*/
Expand Down Expand Up @@ -229,6 +249,23 @@ public function itShouldThrowAnExceptionIfNoScopeCouldBeFound(): void
$systemctl->getScope($unitName);
}

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

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

$this->expectException(UnitNotFoundException::class);
$systemctl->getSlice($unitName);
}

/**
* @test
*/
Expand Down
11 changes: 11 additions & 0 deletions test/Unit/Unit/AbstractUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
[
'name' => 'test1.scope',
],
[
'name' => 'test1.slice',
],
[
'name' => 'test1.mount',
],
Expand Down Expand Up @@ -127,6 +130,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
'name' => 'test1.scope',
'isMultiInstance' => false,
],
[
'name' => 'test1.slice',
'isMultiInstance' => false,
],
];
}

Expand Down Expand Up @@ -183,6 +190,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra
'name' => 'test1.scope',
'instanceName' => null,
],
[
'name' => 'test1.slice',
'instanceName' => null,
],
];
}

Expand Down
17 changes: 17 additions & 0 deletions test/Unit/Utils/OutputFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
nonservice.socket active running
nonservice.device active running
nonservice.scope active running
nonservice.slice active running
superservice.mount active running
awesomeservice.mount active running
nonservice.timer active running
nonservice.socket active running
nonservice.device active running
nonservice.scope active running
nonservice.slice active running
superservice.service active running
awesomeservice.service active running
● failed-service@foo.service loaded failed failed
Expand Down Expand Up @@ -81,6 +83,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
'suffix' => 'scope',
'amount' => 2,
],
[
'output' => $output,
'suffix' => 'slice',
'amount' => 2,
],
[
'output' => $output,
'suffix' => 'mount',
Expand Down Expand Up @@ -120,12 +127,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
a-socket.socket Active running
a-device.device Active running
a-scope.scope Active running
a-slice.slice Active running
super.mount Active running
awesome.mount Active running
nonservice.timer Active running
nonservice.socket Active running
nonservice.device Active running
nonservice.scope Active running
nonservice.slice Active running
instance-service@1.service Active running
instance-service@foo.service Active running
● failed-service@foo.service loaded failed failed
Expand Down Expand Up @@ -175,6 +184,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
'nonservice',
],
],
[
'output' => $output,
'suffix' => 'slice',
'units' => [
'a-slice',
'nonservice',
],
],
[
'output' => $output,
'suffix' => 'mount',
Expand Down