Skip to content

Commit 1dddf38

Browse files
peter279kicanhazstring
authored andcommitted
Resolves issue #13
1 parent a99d924 commit 1dddf38

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ composer.lock
55

66
# PHPUnit
77
build/
8+
.phpunit.result.cache
89

910
# Vagrant
1011
.vagrant

src/SystemCtl.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use icanhazstring\SystemCtl\Unit\Service;
1010
use icanhazstring\SystemCtl\Unit\Timer;
1111
use icanhazstring\SystemCtl\Unit\Socket;
12+
use icanhazstring\SystemCtl\Unit\Scope;
1213
use icanhazstring\SystemCtl\Unit\UnitInterface;
1314

1415
/**
@@ -229,6 +230,38 @@ public function getSockets(?string $unitPrefix = null): array
229230
}, $units);
230231
}
231232

233+
/**
234+
* @param string $name
235+
*
236+
* @return Scope
237+
*/
238+
public function getScope(string $name): Scope
239+
{
240+
$units = $this->listUnits($name, [Scope::UNIT]);
241+
242+
$unitName = $this->searchForUnitInUnits($name, $units);
243+
244+
if (is_null($unitName)) {
245+
throw UnitNotFoundException::create(Scope::UNIT, $name);
246+
}
247+
248+
return new Scope($unitName, $this->getCommandDispatcher());
249+
}
250+
251+
/**
252+
* @param null|string $unitPrefix
253+
*
254+
* @return Scope[]
255+
*/
256+
public function getScopes(?string $unitPrefix = null): array
257+
{
258+
$units = $this->listUnits($unitPrefix, [Scope::UNIT]);
259+
260+
return array_map(function ($unitName) {
261+
return new Scope($unitName, $this->getCommandDispatcher());
262+
}, $units);
263+
}
264+
232265
/**
233266
* Restart the daemon to reload specs and new units
234267
*

src/Unit/Scope.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 Scope
7+
*
8+
* @package icanhazstring\SystemCtl\Unit
9+
*/
10+
class Scope extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'scope';
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
@@ -11,6 +11,7 @@
1111
use icanhazstring\SystemCtl\Unit\Service;
1212
use icanhazstring\SystemCtl\Unit\Timer;
1313
use icanhazstring\SystemCtl\Unit\Socket;
14+
use icanhazstring\SystemCtl\Unit\Scope;
1415

1516
/**
1617
* Class UnitTest
@@ -114,4 +115,33 @@ public function testSocketCommandsIfProcessIsUnsuccessFulShouldRaiseException():
114115
$this->expectException(CommandFailedException::class);
115116
$socket->start();
116117
}
118+
119+
public function testScopeCommandsIfProcessIsSuccessfulShouldReturnTrue(): void
120+
{
121+
$command = $this->prophesize(CommandInterface::class);
122+
$command->isSuccessful()->willReturn(true);
123+
124+
$commandDispatcher = $this->createCommandDispatcherStub();
125+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
126+
127+
$scope = new Scope('AwesomeScope', $commandDispatcher->reveal());
128+
129+
$this->assertTrue($scope->start());
130+
$this->assertTrue($scope->stop());
131+
$this->assertTrue($scope->enable());
132+
$this->assertTrue($scope->disable());
133+
$this->assertTrue($scope->reload());
134+
$this->assertTrue($scope->restart());
135+
}
136+
137+
public function testScopeCommandsIfProcessIsUnsuccessFulShouldRaiseException(): void
138+
{
139+
$commandDispatcher = $this->createCommandDispatcherStub();
140+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
141+
142+
$scope = new Scope('AwesomeScope', $commandDispatcher->reveal());
143+
144+
$this->expectException(CommandFailedException::class);
145+
$scope->start();
146+
}
117147
}

test/Unit/SystemCtlTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use icanhazstring\SystemCtl\Unit\Service;
1313
use icanhazstring\SystemCtl\Unit\Timer;
1414
use icanhazstring\SystemCtl\Unit\Socket;
15+
use icanhazstring\SystemCtl\Unit\Scope;
1516

1617
/**
1718
* Class SystemCtlTest
@@ -155,6 +156,25 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnSocketG
155156
$this->assertEquals($unitName, $socket->getName());
156157
}
157158

159+
/**
160+
* @test
161+
*/
162+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnScopeGetting(): void
163+
{
164+
$unitName = 'testScope';
165+
$output = ' testScope.scope Active running';
166+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
167+
$commandDispatcherStub
168+
->dispatch(...['list-units', $unitName . '*'])
169+
->willReturn($this->buildCommandStub($output));
170+
171+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
172+
173+
$scope = $systemctl->getScope($unitName);
174+
$this->assertInstanceOf(Scope::class, $scope);
175+
$this->assertEquals($unitName, $scope->getName());
176+
}
177+
158178
/**
159179
* @test
160180
*/
@@ -189,6 +209,23 @@ public function itShouldThrowAnExceptionIfNoSocketCouldBeFound(): void
189209
$systemctl->getSocket($unitName);
190210
}
191211

212+
/**
213+
* @test
214+
*/
215+
public function itShouldThrowAnExceptionIfNoScopeCouldBeFound(): void
216+
{
217+
$unitName = 'testScope';
218+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
219+
$commandDispatcherStub
220+
->dispatch(...['list-units', $unitName . '*'])
221+
->willReturn($this->buildCommandStub(''));
222+
223+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
224+
225+
$this->expectException(UnitNotFoundException::class);
226+
$systemctl->getScope($unitName);
227+
}
228+
192229
/**
193230
* @test
194231
*/

test/Unit/Unit/AbstractUnitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
5252
[
5353
'name' => 'test1.socket',
5454
],
55+
[
56+
'name' => 'test1.scope',
57+
],
5558
[
5659
'name' => 'test1.mount',
5760
],
@@ -110,6 +113,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider(): array
110113
'name' => 'test1.socket',
111114
'isMultiInstance' => false,
112115
],
116+
[
117+
'name' => 'test1.scope',
118+
'isMultiInstance' => false,
119+
],
113120
];
114121
}
115122

@@ -162,6 +169,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider(): arra
162169
'name' => 'test1.socket',
163170
'instanceName' => null,
164171
],
172+
[
173+
'name' => 'test1.scope',
174+
'instanceName' => null,
175+
],
165176
];
166177
}
167178

test/Unit/Utils/OutputFetcherTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
3939
awesomeservice.service active running
4040
nonservice.timer active running
4141
nonservice.socket active running
42+
nonservice.scope active running
4243
superservice.mount active running
4344
awesomeservice.mount active running
4445
nonservice.timer active running
4546
nonservice.socket active running
47+
nonservice.scope active running
4648
superservice.service active running
4749
awesomeservice.service active running
4850
[email protected] loaded failed failed
@@ -64,6 +66,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
6466
'suffix' => 'socket',
6567
'amount' => 2,
6668
],
69+
[
70+
'output' => $output,
71+
'suffix' => 'scope',
72+
'amount' => 2,
73+
],
6774
[
6875
'output' => $output,
6976
'suffix' => 'mount',
@@ -101,10 +108,12 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
101108
foo-bar.service Active running
102109
a-timer.timer Active running
103110
a-socket.socket Active running
111+
a-scope.scope Active running
104112
super.mount Active running
105113
awesome.mount Active running
106114
nonservice.timer Active running
107115
nonservice.socket Active running
116+
nonservice.scope Active running
108117
[email protected] Active running
109118
[email protected] Active running
110119
[email protected] loaded failed failed
@@ -138,6 +147,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
138147
'nonservice',
139148
],
140149
],
150+
[
151+
'output' => $output,
152+
'suffix' => 'scope',
153+
'units' => [
154+
'a-scope',
155+
'nonservice',
156+
],
157+
],
141158
[
142159
'output' => $output,
143160
'suffix' => 'mount',

0 commit comments

Comments
 (0)