Skip to content

Commit 2f5e6ef

Browse files
peter279kicanhazstring
authored andcommitted
Resolves issue #12
1 parent 84a8a91 commit 2f5e6ef

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

src/SystemCtl.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use icanhazstring\SystemCtl\Unit\Timer;
1212
use icanhazstring\SystemCtl\Unit\Socket;
1313
use icanhazstring\SystemCtl\Unit\Scope;
14+
use icanhazstring\SystemCtl\Unit\Slice;
1415
use icanhazstring\SystemCtl\Unit\UnitInterface;
1516

1617
/**
@@ -263,6 +264,38 @@ public function getScopes(?string $unitPrefix = null): array
263264
}, $units);
264265
}
265266

267+
/**
268+
* @param string $name
269+
*
270+
* @return Slice
271+
*/
272+
public function getSlice(string $name): Slice
273+
{
274+
$units = $this->listUnits($name, [Slice::UNIT]);
275+
276+
$unitName = $this->searchForUnitInUnits($name, $units);
277+
278+
if (is_null($unitName)) {
279+
throw UnitNotFoundException::create(Slice::UNIT, $name);
280+
}
281+
282+
return new Slice($unitName, $this->getCommandDispatcher());
283+
}
284+
285+
/**
286+
* @param null|string $unitPrefix
287+
*
288+
* @return Slice[]
289+
*/
290+
public function getSlices(?string $unitPrefix = null): array
291+
{
292+
$units = $this->listUnits($unitPrefix, [Slice::UNIT]);
293+
294+
return array_map(function ($unitName) {
295+
return new Slice($unitName, $this->getCommandDispatcher());
296+
}, $units);
297+
}
298+
266299
/**
267300
* Restart the daemon to reload specs and new units
268301
*

src/Unit/Slice.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 Slice
7+
*
8+
* @package icanhazstring\SystemCtl\Unit
9+
*/
10+
class Slice extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'slice';
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
@@ -14,6 +14,7 @@
1414
use icanhazstring\SystemCtl\Unit\Timer;
1515
use icanhazstring\SystemCtl\Unit\Socket;
1616
use icanhazstring\SystemCtl\Unit\Scope;
17+
use icanhazstring\SystemCtl\Unit\Slice;
1718

1819
/**
1920
* Class UnitTest
@@ -149,6 +150,35 @@ public function testScopeCommandsIfProcessIsUnsuccessFulShouldRaiseException():
149150
$scope->start();
150151
}
151152

153+
public function testSliceCommandsIfProcessIsSuccessfulShouldReturnTrue()
154+
{
155+
$command = $this->prophesize(CommandInterface::class);
156+
$command->isSuccessful()->willReturn(true);
157+
158+
$commandDispatcher = $this->createCommandDispatcherStub();
159+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
160+
161+
$slice = new Slice('AwesomeSlice', $commandDispatcher->reveal());
162+
163+
$this->assertTrue($slice->start());
164+
$this->assertTrue($slice->stop());
165+
$this->assertTrue($slice->enable());
166+
$this->assertTrue($slice->disable());
167+
$this->assertTrue($slice->reload());
168+
$this->assertTrue($slice->restart());
169+
}
170+
171+
public function testSliceCommandsIfProcessIsUnsuccessFulShouldRaiseException()
172+
{
173+
$commandDispatcher = $this->createCommandDispatcherStub();
174+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
175+
176+
$slice = new Slice('AwesomeSlice', $commandDispatcher->reveal());
177+
178+
$this->expectException(CommandFailedException::class);
179+
$slice->start();
180+
}
181+
152182
public function testDeviceCommandsIfProcessIsSuccessfulShouldReturnTrue()
153183
{
154184
$command = $this->prophesize(CommandInterface::class);

test/Unit/SystemCtlTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use icanhazstring\SystemCtl\Unit\Timer;
1616
use icanhazstring\SystemCtl\Unit\Socket;
1717
use icanhazstring\SystemCtl\Unit\Scope;
18+
use icanhazstring\SystemCtl\Unit\Slice;
1819

1920
/**
2021
* Class SystemCtlTest
@@ -178,6 +179,25 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnScopeGe
178179
self::assertEquals($unitName, $scope->getName());
179180
}
180181

182+
/**
183+
* @test
184+
*/
185+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnSliceGetting()
186+
{
187+
$unitName = 'testSlice';
188+
$output = ' testSlice.slice Active running';
189+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
190+
$commandDispatcherStub
191+
->dispatch(...['list-units', $unitName . '*'])
192+
->willReturn($this->buildCommandStub($output));
193+
194+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
195+
196+
$slice = $systemctl->getSlice($unitName);
197+
$this->assertInstanceOf(Slice::class, $slice);
198+
$this->assertEquals($unitName, $slice->getName());
199+
}
200+
181201
/**
182202
* @test
183203
*/
@@ -229,6 +249,23 @@ public function itShouldThrowAnExceptionIfNoScopeCouldBeFound(): void
229249
$systemctl->getScope($unitName);
230250
}
231251

252+
/**
253+
* @test
254+
*/
255+
public function itShouldThrowAnExceptionIfNoSliceCouldBeFound()
256+
{
257+
$unitName = 'testSlice';
258+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
259+
$commandDispatcherStub
260+
->dispatch(...['list-units', $unitName . '*'])
261+
->willReturn($this->buildCommandStub(''));
262+
263+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
264+
265+
$this->expectException(UnitNotFoundException::class);
266+
$systemctl->getSlice($unitName);
267+
}
268+
232269
/**
233270
* @test
234271
*/

test/Unit/Unit/AbstractUnitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
5858
[
5959
'name' => 'test1.scope',
6060
],
61+
[
62+
'name' => 'test1.slice',
63+
],
6164
[
6265
'name' => 'test1.mount',
6366
],
@@ -127,6 +130,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
127130
'name' => 'test1.scope',
128131
'isMultiInstance' => false,
129132
],
133+
[
134+
'name' => 'test1.slice',
135+
'isMultiInstance' => false,
136+
],
130137
];
131138
}
132139

@@ -183,6 +190,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra
183190
'name' => 'test1.scope',
184191
'instanceName' => null,
185192
],
193+
[
194+
'name' => 'test1.slice',
195+
'instanceName' => null,
196+
],
186197
];
187198
}
188199

test/Unit/Utils/OutputFetcherTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
4444
nonservice.socket active running
4545
nonservice.device active running
4646
nonservice.scope active running
47+
nonservice.slice active running
4748
superservice.mount active running
4849
awesomeservice.mount active running
4950
nonservice.timer active running
5051
nonservice.socket active running
5152
nonservice.device active running
5253
nonservice.scope active running
54+
nonservice.slice active running
5355
superservice.service active running
5456
awesomeservice.service active running
5557
[email protected] loaded failed failed
@@ -81,6 +83,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
8183
'suffix' => 'scope',
8284
'amount' => 2,
8385
],
86+
[
87+
'output' => $output,
88+
'suffix' => 'slice',
89+
'amount' => 2,
90+
],
8491
[
8592
'output' => $output,
8693
'suffix' => 'mount',
@@ -120,12 +127,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
120127
a-socket.socket Active running
121128
a-device.device Active running
122129
a-scope.scope Active running
130+
a-slice.slice Active running
123131
super.mount Active running
124132
awesome.mount Active running
125133
nonservice.timer Active running
126134
nonservice.socket Active running
127135
nonservice.device Active running
128136
nonservice.scope Active running
137+
nonservice.slice Active running
129138
[email protected] Active running
130139
[email protected] Active running
131140
[email protected] loaded failed failed
@@ -175,6 +184,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
175184
'nonservice',
176185
],
177186
],
187+
[
188+
'output' => $output,
189+
'suffix' => 'slice',
190+
'units' => [
191+
'a-slice',
192+
'nonservice',
193+
],
194+
],
178195
[
179196
'output' => $output,
180197
'suffix' => 'mount',

0 commit comments

Comments
 (0)