Skip to content

Commit 293ceca

Browse files
peter279kicanhazstring
authored andcommitted
Resolves issue #11
1 parent 9dbd65c commit 293ceca

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
@@ -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\Target;
1516
use icanhazstring\SystemCtl\Unit\UnitInterface;
1617

1718
/**
@@ -296,6 +297,38 @@ public function getSlices(?string $unitPrefix = null): array
296297
}, $units);
297298
}
298299

300+
/**
301+
* @param string $name
302+
*
303+
* @return Target
304+
*/
305+
public function getTarget(string $name): Target
306+
{
307+
$units = $this->listUnits($name, [Target::UNIT]);
308+
309+
$unitName = $this->searchForUnitInUnits($name, $units);
310+
311+
if (is_null($unitName)) {
312+
throw UnitNotFoundException::create(Target::UNIT, $name);
313+
}
314+
315+
return new Target($unitName, $this->getCommandDispatcher());
316+
}
317+
318+
/**
319+
* @param null|string $unitPrefix
320+
*
321+
* @return Target[]
322+
*/
323+
public function getTargets(?string $unitPrefix = null): array
324+
{
325+
$units = $this->listUnits($unitPrefix, [Target::UNIT]);
326+
327+
return array_map(function ($unitName) {
328+
return new Target($unitName, $this->getCommandDispatcher());
329+
}, $units);
330+
}
331+
299332
/**
300333
* Restart the daemon to reload specs and new units
301334
*

src/Unit/Target.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 Target
7+
*
8+
* @package icanhazstring\SystemCtl\Unit
9+
*/
10+
class Target extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'target';
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
@@ -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\Target;
1819

1920
/**
2021
* Class UnitTest
@@ -179,6 +180,35 @@ public function testSliceCommandsIfProcessIsUnsuccessFulShouldRaiseException()
179180
$slice->start();
180181
}
181182

183+
public function testTargetCommandsIfProcessIsSuccessfulShouldReturnTrue()
184+
{
185+
$command = $this->prophesize(CommandInterface::class);
186+
$command->isSuccessful()->willReturn(true);
187+
188+
$commandDispatcher = $this->createCommandDispatcherStub();
189+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
190+
191+
$target = new Target('AwesomeTarget', $commandDispatcher->reveal());
192+
193+
$this->assertTrue($target->start());
194+
$this->assertTrue($target->stop());
195+
$this->assertTrue($target->enable());
196+
$this->assertTrue($target->disable());
197+
$this->assertTrue($target->reload());
198+
$this->assertTrue($target->restart());
199+
}
200+
201+
public function testTargetCommandsIfProcessIsUnsuccessFulShouldRaiseException()
202+
{
203+
$commandDispatcher = $this->createCommandDispatcherStub();
204+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
205+
206+
$target = new Target('AwesomeTarget', $commandDispatcher->reveal());
207+
208+
$this->expectException(CommandFailedException::class);
209+
$target->start();
210+
}
211+
182212
public function testDeviceCommandsIfProcessIsSuccessfulShouldReturnTrue()
183213
{
184214
$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\Target;
1920

2021
/**
2122
* Class SystemCtlTest
@@ -198,6 +199,25 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnSliceGe
198199
$this->assertEquals($unitName, $slice->getName());
199200
}
200201

202+
/**
203+
* @test
204+
*/
205+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnTargetGetting()
206+
{
207+
$unitName = 'testTarget';
208+
$output = ' testTarget.target Active running';
209+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
210+
$commandDispatcherStub
211+
->dispatch(...['list-units', $unitName . '*'])
212+
->willReturn($this->buildCommandStub($output));
213+
214+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
215+
216+
$target = $systemctl->getTarget($unitName);
217+
$this->assertInstanceOf(Target::class, $target);
218+
$this->assertEquals($unitName, $target->getName());
219+
}
220+
201221
/**
202222
* @test
203223
*/
@@ -232,6 +252,23 @@ public function itShouldThrowAnExceptionIfNoSocketCouldBeFound(): void
232252
$systemctl->getSocket($unitName);
233253
}
234254

255+
/**
256+
* @test
257+
*/
258+
public function itShouldThrowAnExceptionIfNoTargetCouldBeFound()
259+
{
260+
$unitName = 'testTarget';
261+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
262+
$commandDispatcherStub
263+
->dispatch(...['list-units', $unitName . '*'])
264+
->willReturn($this->buildCommandStub(''));
265+
266+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
267+
268+
$this->expectException(UnitNotFoundException::class);
269+
$systemctl->getTarget($unitName);
270+
}
271+
235272
/**
236273
* @test
237274
*/

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.target',
66+
],
6467
[
6568
'name' => 'test1.mount',
6669
],
@@ -134,6 +137,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
134137
'name' => 'test1.slice',
135138
'isMultiInstance' => false,
136139
],
140+
[
141+
'name' => 'test1.target',
142+
'isMultiInstance' => false,
143+
],
137144
];
138145
}
139146

@@ -194,6 +201,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra
194201
'name' => 'test1.slice',
195202
'instanceName' => null,
196203
],
204+
[
205+
'name' => 'test1.target',
206+
'instanceName' => null,
207+
],
197208
];
198209
}
199210

test/Unit/Utils/OutputFetcherTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
4545
nonservice.device active running
4646
nonservice.scope active running
4747
nonservice.slice active running
48+
nonservice.target active running
4849
superservice.mount active running
4950
awesomeservice.mount active running
5051
nonservice.timer active running
5152
nonservice.socket active running
5253
nonservice.device active running
5354
nonservice.scope active running
5455
nonservice.slice active running
56+
nonservice.target active running
5557
superservice.service active running
5658
awesomeservice.service active running
5759
[email protected] loaded failed failed
@@ -88,6 +90,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
8890
'suffix' => 'slice',
8991
'amount' => 2,
9092
],
93+
[
94+
'output' => $output,
95+
'suffix' => 'target',
96+
'amount' => 2,
97+
],
9198
[
9299
'output' => $output,
93100
'suffix' => 'mount',
@@ -128,13 +135,15 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
128135
a-device.device Active running
129136
a-scope.scope Active running
130137
a-slice.slice Active running
138+
a-target.target Active running
131139
super.mount Active running
132140
awesome.mount Active running
133141
nonservice.timer Active running
134142
nonservice.socket Active running
135143
nonservice.device Active running
136144
nonservice.scope Active running
137145
nonservice.slice Active running
146+
nonservice.target Active running
138147
[email protected] Active running
139148
[email protected] Active running
140149
[email protected] loaded failed failed
@@ -192,6 +201,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
192201
'nonservice',
193202
],
194203
],
204+
[
205+
'output' => $output,
206+
'suffix' => 'target',
207+
'units' => [
208+
'a-target',
209+
'nonservice',
210+
],
211+
],
195212
[
196213
'output' => $output,
197214
'suffix' => 'mount',

0 commit comments

Comments
 (0)