Skip to content

Commit f1339a5

Browse files
committed
Provide is-active and is-enabled
1 parent e551485 commit f1339a5

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

src/Unit/AbstractUnit.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,42 @@ public function enable(bool $raise = false): bool
129129
{
130130
return $this->execute(__FUNCTION__, $raise);
131131
}
132+
133+
/**
134+
* @inheritdoc
135+
*/
136+
public function isActive(bool $raise = false): bool
137+
{
138+
$process = $this->processBuilder
139+
->setArguments(['is-active', $this->getName()])
140+
->getProcess();
141+
142+
$process->run();
143+
144+
if (!$process->isSuccessful() && $raise) {
145+
$exceptionCall = CommandFailedException::class . '::from' . ucfirst($this->type);
146+
throw call_user_func_array($exceptionCall, [$this->getName(), 'is-active']);
147+
}
148+
149+
return trim($process->getOutput()) === 'active';
150+
}
151+
152+
/**
153+
* @inheritDoc
154+
*/
155+
public function isEnabled(bool $raise = false): bool
156+
{
157+
$process = $this->processBuilder
158+
->setArguments(['is-enabled', $this->getName()])
159+
->getProcess();
160+
161+
$process->run();
162+
163+
if (!$process->isSuccessful() && $raise) {
164+
$exceptionCall = CommandFailedException::class . '::from' . ucfirst($this->type);
165+
throw call_user_func_array($exceptionCall, [$this->getName(), 'is-enabled']);
166+
}
167+
168+
return trim($process->getOutput()) === 'enabled';
169+
}
132170
}

src/Unit/UnitInterface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function getInstanceName(): ?string;
3434
* Start command
3535
*
3636
* @param bool $raise Raise exception on failure instead of process result
37+
*
3738
* @return bool
3839
*/
3940
public function start(bool $raise = false): bool;
@@ -42,6 +43,7 @@ public function start(bool $raise = false): bool;
4243
* Stop command
4344
*
4445
* @param bool $raise Raise exception on failure instead of process result
46+
*
4547
* @return bool
4648
*/
4749
public function stop(bool $raise = false): bool;
@@ -50,6 +52,7 @@ public function stop(bool $raise = false): bool;
5052
* Disable command
5153
*
5254
* @param bool $raise Raise exception on failure instead of process result
55+
*
5356
* @return bool
5457
*/
5558
public function disable(bool $raise = false): bool;
@@ -58,6 +61,7 @@ public function disable(bool $raise = false): bool;
5861
* Reload command
5962
*
6063
* @param bool $raise Raise exception on failure instead of process result
64+
*
6165
* @return bool
6266
*/
6367
public function reload(bool $raise = false): bool;
@@ -66,6 +70,7 @@ public function reload(bool $raise = false): bool;
6670
* Restart command
6771
*
6872
* @param bool $raise Raise exception on failure instead of process result
73+
*
6974
* @return bool
7075
*/
7176
public function restart(bool $raise = false): bool;
@@ -74,7 +79,26 @@ public function restart(bool $raise = false): bool;
7479
* Enable command
7580
*
7681
* @param bool $raise Raise exception on failure instead of process result
82+
*
7783
* @return bool
7884
*/
7985
public function enable(bool $raise = false): bool;
86+
87+
/**
88+
* Check whether unit is active
89+
*
90+
* @param bool $raise Raise exception on failure instead of process result
91+
*
92+
* @return bool
93+
*/
94+
public function isActive(bool $raise = false): bool;
95+
96+
/**
97+
* Check whether unit is enabled
98+
*
99+
* @param bool $raise Raise exception on failure instead of process result
100+
*
101+
* @return bool
102+
*/
103+
public function isEnabled(bool $raise = false): bool;
80104
}

tests/Unit/UnitTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
class UnitTest extends TestCase
1212
{
13-
protected function getSystemCtlMock(bool $processState = true): SystemCtl
13+
protected function getSystemCtlMock(bool $processState = true, string $processOutput = ''): SystemCtl
1414
{
1515
$process = $this->getMockBuilder(Process::class)
1616
->disableOriginalConstructor()
17-
->setMethods(['isSuccessful'])
17+
->setMethods(['isSuccessful', 'getOutput'])
1818
->getMock();
1919

2020
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
@@ -31,6 +31,7 @@ protected function getSystemCtlMock(bool $processState = true): SystemCtl
3131

3232
$systemctl->method('getProcessBuilder')->willReturn($processBuilder);
3333
$process->method('isSuccessful')->willReturn($processState);
34+
$process->method('getOutput')->willReturn($processOutput);
3435

3536
return $systemctl;
3637
}
@@ -67,4 +68,30 @@ public function testProcessShouldReturnExitCode()
6768
$this->assertFalse($service->restart());
6869
$this->assertFalse($service->reload());
6970
}
71+
72+
public function testIsEnabled()
73+
{
74+
$systemctl = $this->getSystemCtlMock(true, 'enabled');
75+
$service = $systemctl->getService('TestService');
76+
77+
$this->assertTrue($service->isEnabled());
78+
79+
$systemctl = $this->getSystemCtlMock(true, 'disabled');
80+
$service = $systemctl->getService('TestService');
81+
82+
$this->assertFalse($service->isEnabled());
83+
}
84+
85+
public function testIsActive()
86+
{
87+
$systemctl = $this->getSystemCtlMock(true, 'active');
88+
$service = $systemctl->getService('TestService');
89+
90+
$this->assertTrue($service->isActive());
91+
92+
$systemctl = $this->getSystemCtlMock(true, 'inactive');
93+
$service = $systemctl->getService('TestService');
94+
95+
$this->assertFalse($service->isActive());
96+
}
7097
}

0 commit comments

Comments
 (0)