Skip to content

Commit 72e8950

Browse files
peter279kicanhazstring
authored andcommitted
Resolves issue #10
1 parent 293ceca commit 72e8950

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

src/SystemCtl.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use icanhazstring\SystemCtl\Unit\Socket;
1313
use icanhazstring\SystemCtl\Unit\Scope;
1414
use icanhazstring\SystemCtl\Unit\Slice;
15+
use icanhazstring\SystemCtl\Unit\Swap;
1516
use icanhazstring\SystemCtl\Unit\Target;
1617
use icanhazstring\SystemCtl\Unit\UnitInterface;
1718

@@ -329,6 +330,38 @@ public function getTargets(?string $unitPrefix = null): array
329330
}, $units);
330331
}
331332

333+
/**
334+
* @param string $name
335+
*
336+
* @return Swap
337+
*/
338+
public function getSwap(string $name): Swap
339+
{
340+
$units = $this->listUnits($name, [Swap::UNIT]);
341+
342+
$unitName = $this->searchForUnitInUnits($name, $units);
343+
344+
if (is_null($unitName)) {
345+
throw UnitNotFoundException::create(Swap::UNIT, $name);
346+
}
347+
348+
return new Swap($unitName, $this->getCommandDispatcher());
349+
}
350+
351+
/**
352+
* @param null|string $unitPrefix
353+
*
354+
* @return Swap[]
355+
*/
356+
public function getSwaps(?string $unitPrefix = null): array
357+
{
358+
$units = $this->listUnits($unitPrefix, [Swap::UNIT]);
359+
360+
return array_map(function ($unitName) {
361+
return new Swap($unitName, $this->getCommandDispatcher());
362+
}, $units);
363+
}
364+
332365
/**
333366
* Restart the daemon to reload specs and new units
334367
*

src/Unit/Swap.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 Swap
7+
*
8+
* @package icanhazstring\SystemCtl\Unit
9+
*/
10+
class Swap extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'swap';
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected function getUnitSuffix(): string
21+
{
22+
return static::UNIT;
23+
}
24+
}

test/Integration/Unit/UnitTest.php

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

2021
/**
@@ -209,6 +210,36 @@ public function testTargetCommandsIfProcessIsUnsuccessFulShouldRaiseException()
209210
$target->start();
210211
}
211212

213+
214+
public function testSwapCommandsIfProcessIsSuccessfulShouldReturnTrue()
215+
{
216+
$command = $this->prophesize(CommandInterface::class);
217+
$command->isSuccessful()->willReturn(true);
218+
219+
$commandDispatcher = $this->createCommandDispatcherStub();
220+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
221+
222+
$swap = new Swap('AwesomeSwap', $commandDispatcher->reveal());
223+
224+
$this->assertTrue($swap->start());
225+
$this->assertTrue($swap->stop());
226+
$this->assertTrue($swap->enable());
227+
$this->assertTrue($swap->disable());
228+
$this->assertTrue($swap->reload());
229+
$this->assertTrue($swap->restart());
230+
}
231+
232+
public function testSwapCommandsIfProcessIsUnsuccessFulShouldRaiseException()
233+
{
234+
$commandDispatcher = $this->createCommandDispatcherStub();
235+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
236+
237+
$swap = new Swap('AwesomeSwap', $commandDispatcher->reveal());
238+
239+
$this->expectException(CommandFailedException::class);
240+
$swap->start();
241+
}
242+
212243
public function testDeviceCommandsIfProcessIsSuccessfulShouldReturnTrue()
213244
{
214245
$command = $this->prophesize(CommandInterface::class);

test/Unit/SystemCtlTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use icanhazstring\SystemCtl\Unit\Socket;
1717
use icanhazstring\SystemCtl\Unit\Scope;
1818
use icanhazstring\SystemCtl\Unit\Slice;
19+
use icanhazstring\SystemCtl\Unit\Swap;
1920
use icanhazstring\SystemCtl\Unit\Target;
2021

2122
/**
@@ -218,6 +219,25 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnTargetG
218219
$this->assertEquals($unitName, $target->getName());
219220
}
220221

222+
/**
223+
* @test
224+
*/
225+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnSwapGetting()
226+
{
227+
$unitName = 'testSwap';
228+
$output = ' testSwap.swap Active running';
229+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
230+
$commandDispatcherStub
231+
->dispatch(...['list-units', $unitName . '*'])
232+
->willReturn($this->buildCommandStub($output));
233+
234+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
235+
236+
$swap = $systemctl->getSwap($unitName);
237+
$this->assertInstanceOf(Swap::class, $swap);
238+
$this->assertEquals($unitName, $swap->getName());
239+
}
240+
221241
/**
222242
* @test
223243
*/
@@ -269,6 +289,23 @@ public function itShouldThrowAnExceptionIfNoTargetCouldBeFound()
269289
$systemctl->getTarget($unitName);
270290
}
271291

292+
/**
293+
* @test
294+
*/
295+
public function itShouldThrowAnExceptionIfNoSwapCouldBeFound()
296+
{
297+
$unitName = 'testSwap';
298+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
299+
$commandDispatcherStub
300+
->dispatch(...['list-units', $unitName . '*'])
301+
->willReturn($this->buildCommandStub(''));
302+
303+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
304+
305+
$this->expectException(UnitNotFoundException::class);
306+
$systemctl->getSwap($unitName);
307+
}
308+
272309
/**
273310
* @test
274311
*/

test/Unit/Unit/AbstractUnitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
6161
[
6262
'name' => 'test1.slice',
6363
],
64+
[
65+
'name' => 'test1.swap',
66+
],
6467
[
6568
'name' => 'test1.target',
6669
],
@@ -137,6 +140,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
137140
'name' => 'test1.slice',
138141
'isMultiInstance' => false,
139142
],
143+
[
144+
'name' => 'test1.swap',
145+
'isMultiInstance' => false,
146+
],
140147
[
141148
'name' => 'test1.target',
142149
'isMultiInstance' => false,
@@ -201,6 +208,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra
201208
'name' => 'test1.slice',
202209
'instanceName' => null,
203210
],
211+
[
212+
'name' => 'test1.swap',
213+
'instanceName' => null,
214+
],
204215
[
205216
'name' => 'test1.target',
206217
'instanceName' => null,

test/Unit/Utils/OutputFetcherTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
4545
nonservice.device active running
4646
nonservice.scope active running
4747
nonservice.slice active running
48+
nonservice.swap active running
4849
nonservice.target active running
4950
superservice.mount active running
5051
awesomeservice.mount active running
@@ -53,6 +54,7 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
5354
nonservice.device active running
5455
nonservice.scope active running
5556
nonservice.slice active running
57+
nonservice.swap active running
5658
nonservice.target active running
5759
superservice.service active running
5860
awesomeservice.service active running
@@ -90,6 +92,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
9092
'suffix' => 'slice',
9193
'amount' => 2,
9294
],
95+
[
96+
'output' => $output,
97+
'suffix' => 'swap',
98+
'amount' => 2,
99+
],
93100
[
94101
'output' => $output,
95102
'suffix' => 'target',
@@ -135,6 +142,7 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
135142
a-device.device Active running
136143
a-scope.scope Active running
137144
a-slice.slice Active running
145+
a-swap.swap Active running
138146
a-target.target Active running
139147
super.mount Active running
140148
awesome.mount Active running
@@ -143,6 +151,7 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
143151
nonservice.device Active running
144152
nonservice.scope Active running
145153
nonservice.slice Active running
154+
nonservice.swap Active running
146155
nonservice.target Active running
147156
[email protected] Active running
148157
[email protected] Active running
@@ -201,6 +210,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
201210
'nonservice',
202211
],
203212
],
213+
[
214+
'output' => $output,
215+
'suffix' => 'swap',
216+
'units' => [
217+
'a-swap',
218+
'nonservice',
219+
],
220+
],
204221
[
205222
'output' => $output,
206223
'suffix' => 'target',

0 commit comments

Comments
 (0)