Skip to content

Commit 4f2299c

Browse files
peter279kicanhazstring
authored andcommitted
Add Unit socket (#27)
1 parent 9cca4f3 commit 4f2299c

File tree

6 files changed

+154
-2
lines changed

6 files changed

+154
-2
lines changed

src/SystemCtl.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SystemCtl\Exception\UnitTypeNotSupportedException;
99
use SystemCtl\Unit\Service;
1010
use SystemCtl\Unit\Timer;
11+
use SystemCtl\Unit\Socket;
1112
use SystemCtl\Unit\UnitInterface;
1213

1314
/**
@@ -193,6 +194,38 @@ public function getTimers(?string $unitPrefix = null): array
193194
}, $units);
194195
}
195196

197+
/**
198+
* @param string $name
199+
*
200+
* @return Socket
201+
*/
202+
public function getSocket(string $name): Socket
203+
{
204+
$units = $this->listUnits($name, [Socket::UNIT]);
205+
206+
$unitName = $this->searchForUnitInUnits($name, $units);
207+
208+
if (is_null($unitName)) {
209+
throw UnitNotFoundException::create(Socket::UNIT, $name);
210+
}
211+
212+
return new Socket($unitName, $this->getCommandDispatcher());
213+
}
214+
215+
/**
216+
* @param null|string $unitPrefix
217+
*
218+
* @return Socket[]
219+
*/
220+
public function getSockets(?string $unitPrefix = null): array
221+
{
222+
$units = $this->listUnits($unitPrefix, [Socket::UNIT]);
223+
224+
return array_map(function ($unitName) {
225+
return new Socket($unitName, $this->getCommandDispatcher());
226+
}, $units);
227+
}
228+
196229
/**
197230
* Restart the daemon to reload specs and new units
198231
*

src/Unit/Socket.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace SystemCtl\Unit;
4+
5+
/**
6+
* Class Socket
7+
*
8+
* @package SystemCtl\Unit
9+
*/
10+
class Socket extends AbstractUnit
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public const UNIT = 'socket';
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected function getUnitSuffix(): string
21+
{
22+
return static::UNIT;
23+
}
24+
}

tests/Integration/Unit/UnitTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SystemCtl\Exception\CommandFailedException;
1111
use SystemCtl\Unit\Service;
1212
use SystemCtl\Unit\Timer;
13+
use SystemCtl\Unit\Socket;
1314

1415
/**
1516
* Class UnitTest
@@ -84,4 +85,33 @@ public function testTimerCommandsIfProcessIsUnsuccessFulShouldRaiseException()
8485
$this->expectException(CommandFailedException::class);
8586
$timer->start();
8687
}
88+
89+
public function testSocketCommandsIfProcessIsSuccessfulShouldReturnTrue()
90+
{
91+
$command = $this->prophesize(CommandInterface::class);
92+
$command->isSuccessful()->willReturn(true);
93+
94+
$commandDispatcher = $this->createCommandDispatcherStub();
95+
$commandDispatcher->dispatch(Argument::cetera())->willReturn($command);
96+
97+
$socket = new Socket('AwesomeSocket', $commandDispatcher->reveal());
98+
99+
$this->assertTrue($socket->start());
100+
$this->assertTrue($socket->stop());
101+
$this->assertTrue($socket->enable());
102+
$this->assertTrue($socket->disable());
103+
$this->assertTrue($socket->reload());
104+
$this->assertTrue($socket->restart());
105+
}
106+
107+
public function testSocketCommandsIfProcessIsUnsuccessFulShouldRaiseException()
108+
{
109+
$commandDispatcher = $this->createCommandDispatcherStub();
110+
$commandDispatcher->dispatch(Argument::cetera())->willThrow(CommandFailedException::class);
111+
112+
$socket = new Socket('AwesomeSocket', $commandDispatcher->reveal());
113+
114+
$this->expectException(CommandFailedException::class);
115+
$socket->start();
116+
}
87117
}

tests/Unit/SystemCtlTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use SystemCtl\SystemCtl;
1212
use SystemCtl\Unit\Service;
1313
use SystemCtl\Unit\Timer;
14+
use SystemCtl\Unit\Socket;
1415

1516
/**
1617
* Class SystemCtlTest
@@ -135,6 +136,25 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnTimerGe
135136
$this->assertEquals($unitName, $timer->getName());
136137
}
137138

139+
/**
140+
* @test
141+
*/
142+
public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnSocketGetting()
143+
{
144+
$unitName = 'testSocket';
145+
$output = ' testSocket.socket Active running';
146+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
147+
$commandDispatcherStub
148+
->dispatch(...['list-units', $unitName . '*'])
149+
->willReturn($this->buildCommandStub($output));
150+
151+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
152+
153+
$socket = $systemctl->getSocket($unitName);
154+
$this->assertInstanceOf(Socket::class, $socket);
155+
$this->assertEquals($unitName, $socket->getName());
156+
}
157+
138158
/**
139159
* @test
140160
*/
@@ -152,6 +172,23 @@ public function itShouldThrowAnExeceptionIfNotTimerCouldBeFound()
152172
$systemctl->getTimer($unitName);
153173
}
154174

175+
/**
176+
* @test
177+
*/
178+
public function itShouldThrowAnExceptionIfNoSocketCouldBeFound()
179+
{
180+
$unitName = 'testSocket';
181+
$commandDispatcherStub = $this->buildCommandDispatcherStub();
182+
$commandDispatcherStub
183+
->dispatch(...['list-units', $unitName . '*'])
184+
->willReturn($this->buildCommandStub(''));
185+
186+
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
187+
188+
$this->expectException(UnitNotFoundException::class);
189+
$systemctl->getSocket($unitName);
190+
}
191+
155192
/**
156193
* @test
157194
*/

tests/Unit/Unit/AbstractUnitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public function itShouldReturnCorrectNameDataProvider(): array
4949
[
5050
'name' => 'test1.timer',
5151
],
52+
[
53+
'name' => 'test1.socket',
54+
],
5255
[
5356
'name' => 'test1.mount',
5457
],
@@ -103,6 +106,10 @@ public function itDetectsMultiInstanceUnitsCorrectlyDataProvider()
103106
'name' => 'test1.timer',
104107
'isMultiInstance' => false,
105108
],
109+
[
110+
'name' => 'test1.socket',
111+
'isMultiInstance' => false,
112+
],
106113
];
107114
}
108115

@@ -151,6 +158,10 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider()
151158
'name' => 'test1.timer',
152159
'instanceName' => null,
153160
],
161+
[
162+
'name' => 'test1.socket',
163+
'instanceName' => null,
164+
],
154165
];
155166
}
156167

tests/Unit/Utils/OutputFetcherTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
3838
superservice.service active running
3939
awesomeservice.service active running
4040
nonservice.timer active running
41+
nonservice.socket active running
4142
superservice.mount active running
4243
awesomeservice.mount active running
4344
nonservice.timer active running
45+
nonservice.socket active running
4446
superservice.service active running
4547
awesomeservice.service active running
46-
[email protected] loaded failed failed
48+
[email protected] loaded failed failed
4749
OUTPUT;
4850

4951
return [
@@ -57,6 +59,11 @@ public function itDeterminesTheCorrectAmountOfUnitsDataProvider(): array
5759
'suffix' => 'timer',
5860
'amount' => 2,
5961
],
62+
[
63+
'output' => $output,
64+
'suffix' => 'socket',
65+
'amount' => 2,
66+
],
6067
[
6168
'output' => $output,
6269
'suffix' => 'mount',
@@ -93,12 +100,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
93100
foo.service Active running
94101
foo-bar.service Active running
95102
a-timer.timer Active running
103+
a-socket.socket Active running
96104
super.mount Active running
97105
awesome.mount Active running
98106
nonservice.timer Active running
107+
nonservice.socket Active running
99108
[email protected] Active running
100109
[email protected] Active running
101-
[email protected] loaded failed failed
110+
[email protected] loaded failed failed
102111
OUTPUT;
103112

104113
return [
@@ -121,6 +130,14 @@ public function itOnlyExtractsTheUnitNamesDataProvider(): array
121130
'nonservice',
122131
],
123132
],
133+
[
134+
'output' => $output,
135+
'suffix' => 'socket',
136+
'units' => [
137+
'a-socket',
138+
'nonservice',
139+
],
140+
],
124141
[
125142
'output' => $output,
126143
'suffix' => 'mount',

0 commit comments

Comments
 (0)