Skip to content

Commit dae8d3d

Browse files
committed
Change signature for unit methods
Adjust PSR-4 autoload namespace for tests. Split unit test into seperate classes
1 parent c5300bd commit dae8d3d

9 files changed

Lines changed: 185 additions & 79 deletions

File tree

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
},
1313
"autoload": {
1414
"psr-4": {
15-
"SystemCtl\\": [
16-
"src/",
17-
"tests/"
18-
]
15+
"SystemCtl\\": "src/",
16+
"SystemCtl\\Test\\": "tests/"
1917
}
2018
},
2119
"license": "MIT",

src/SystemCtl.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public static function unitFromSuffix(string $unitSuffix, string $unitName): Uni
8989
/**
9090
* List all supported units
9191
*
92-
* @param null|string $unitPrefix
9392
* @param string[] $unitTypes
94-
* @return array|\string[]
93+
* @param null|string $unitPrefix
94+
* @return array
9595
*/
96-
public function listUnits(?string $unitPrefix = null, array $unitTypes = self::SUPPORTED_UNITS): array
96+
public function listUnits(array $unitTypes, ?string $unitPrefix = null): array
9797
{
9898
$processBuilder = $this->getProcessBuilder()
9999
->add('list-units');
@@ -113,6 +113,15 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S
113113
}, []);
114114
}
115115

116+
/**
117+
* Invoke getUnit or getUnits depending on the requested method.
118+
* The method name needs to contain the unit type u want to call.
119+
*
120+
* @param $name
121+
* @param $arguments
122+
* @return array|AbstractUnit
123+
* @throws UnitTypeNotSupportedException
124+
*/
116125
public function __call($name, $arguments)
117126
{
118127
preg_match('/get(?<unit>[^s]+)(?<plural>s)?/', $name, $match);
@@ -121,7 +130,7 @@ public function __call($name, $arguments)
121130
$unitName = strtolower($match['unit']);
122131

123132
if (!in_array($unitName, self::SUPPORTED_UNITS)) {
124-
throw new UnitTypeNotSupportedException("Unit {$unitName} not supported");
133+
throw new UnitTypeNotSupportedException("Unit '{$unitName}'' not supported");
125134
}
126135

127136
if ($isPlural) {
@@ -152,7 +161,7 @@ private function getUnit(string $unitClass, $args): AbstractUnit
152161
private function getUnits(string $unitName, $arguments): array
153162
{
154163
$unitPrefix = $arguments[0] ?? null;
155-
$units = $this->listUnits($unitPrefix, [strtolower($unitName)]);
164+
$units = $this->listUnits([strtolower($unitName)], $unitPrefix);
156165
$unitClass = '\SystemCtl\Unit\\' . $unitName;
157166

158167
return array_map(function ($unitName) use ($unitClass) {

src/Unit/AbstractUnit.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,50 @@ protected function execute(string $command, bool $raise = true): bool
8080
return $process->isSuccessful();
8181
}
8282

83-
public function start(bool $raise = true): bool
83+
/**
84+
* @inheritdoc
85+
*/
86+
public function start(bool $raise = false): bool
8487
{
8588
return $this->execute(__FUNCTION__, $raise);
8689
}
8790

88-
public function stop(bool $raise = true): bool
91+
/**
92+
* @inheritdoc
93+
*/
94+
public function stop(bool $raise = false): bool
8995
{
9096
return $this->execute(__FUNCTION__, $raise);
9197
}
9298

93-
public function disable(bool $raise = true): bool
99+
/**
100+
* @inheritdoc
101+
*/
102+
public function disable(bool $raise = false): bool
94103
{
95104
return $this->execute(__FUNCTION__, $raise);
96105
}
97106

98-
public function reload(bool $raise = true): bool
107+
/**
108+
* @inheritdoc
109+
*/
110+
public function reload(bool $raise = false): bool
99111
{
100112
return $this->execute(__FUNCTION__, $raise);
101113
}
102114

103-
public function restart(bool $raise = true): bool
115+
/**
116+
* @inheritdoc
117+
*/
118+
public function restart(bool $raise = false): bool
104119
{
105120
return $this->execute(__FUNCTION__, $raise);
106121
}
107122

108-
public function enable(bool $raise = true): bool
123+
/**
124+
* @inheritdoc
125+
*/
126+
public function enable(bool $raise = false): bool
109127
{
110128
return $this->execute(__FUNCTION__, $raise);
111129
}

src/Unit/Service.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace SystemCtl\Unit;
54

65
use Symfony\Component\Process\ProcessBuilder;
@@ -9,6 +8,9 @@ class Service extends AbstractUnit
98
{
109
public const UNIT = 'service';
1110

11+
/**
12+
* @inheritdoc
13+
*/
1214
public function __construct($name, ProcessBuilder $processBuilder)
1315
{
1416
parent::__construct($name, $processBuilder);

src/Unit/UnitInterface.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,48 @@ public function getInstanceName(): ?string;
3333
/**
3434
* Start command
3535
*
36+
* @param bool $raise Raise exception on failure instead of process result
3637
* @return bool
3738
*/
38-
public function start(): bool;
39+
public function start(bool $raise = false): bool;
3940

4041
/**
4142
* Stop command
4243
*
44+
* @param bool $raise Raise exception on failure instead of process result
4345
* @return bool
4446
*/
45-
public function stop(): bool;
47+
public function stop(bool $raise = false): bool;
4648

4749
/**
4850
* Disable command
4951
*
52+
* @param bool $raise Raise exception on failure instead of process result
5053
* @return bool
5154
*/
52-
public function disable(): bool;
55+
public function disable(bool $raise = false): bool;
5356

5457
/**
5558
* Reload command
5659
*
60+
* @param bool $raise Raise exception on failure instead of process result
5761
* @return bool
5862
*/
59-
public function reload(): bool;
63+
public function reload(bool $raise = false): bool;
6064

6165
/**
6266
* Restart command
6367
*
68+
* @param bool $raise Raise exception on failure instead of process result
6469
* @return bool
6570
*/
66-
public function restart(): bool;
71+
public function restart(bool $raise = false): bool;
6772

6873
/**
6974
* Enable command
7075
*
76+
* @param bool $raise Raise exception on failure instead of process result
7177
* @return bool
7278
*/
73-
public function enable(): bool;
79+
public function enable(bool $raise = false): bool;
7480
}

tests/SystemCtlTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testListUnitsWithAvailableUnits()
5959
cron.service loaded active running
6060
EOT;
6161
$systemctl = $this->buildSystemCtlMock($output);
62-
$units = $systemctl->listUnits(null, SystemCtl::AVAILABLE_UNITS);
62+
$units = $systemctl->listUnits(SystemCtl::AVAILABLE_UNITS);
6363
$this->assertCount(11, $units);
6464
}
6565

@@ -79,7 +79,7 @@ public function testListUnitsWithSupportedUnits()
7979
cron.service loaded active running
8080
EOT;
8181
$systemctl = $this->buildSystemCtlMock($output);
82-
$units = $systemctl->listUnits();
82+
$units = $systemctl->listUnits(SystemCtl::SUPPORTED_UNITS);
8383
$this->assertCount(5, $units);
8484
}
8585

tests/Unit/ServiceTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace SystemCtl\Test\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Process\Process;
7+
use Symfony\Component\Process\ProcessBuilder;
8+
use SystemCtl\Exception\CommandFailedException;
9+
use SystemCtl\SystemCtl;
10+
11+
class ServiceTest extends TestCase
12+
{
13+
protected function getSystemCtlMock(bool $processState = true): SystemCtl
14+
{
15+
$process = $this->getMockBuilder(Process::class)
16+
->disableOriginalConstructor()
17+
->setMethods(['isSuccessful'])
18+
->getMock();
19+
20+
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
21+
->disableOriginalConstructor()
22+
->setMethods(['getProcess'])
23+
->getMock();
24+
25+
$processBuilder->method('getProcess')->willReturn($process);
26+
27+
/** @var \PHPUnit_Framework_MockObject_MockObject|SystemCtl $systemctl */
28+
$systemctl = $this->getMockBuilder(SystemCtl::class)
29+
->setMethods(['getProcessBuilder'])
30+
->getMock();
31+
32+
$systemctl->method('getProcessBuilder')->willReturn($processBuilder);
33+
$process->method('isSuccessful')->willReturn($processState);
34+
35+
return $systemctl;
36+
}
37+
38+
public function testServiceCommandsIfProcessIsSuccessfulShouldReturnTrue()
39+
{
40+
$systemctl = $this->getSystemCtlMock();
41+
$service = $systemctl->getService('AwesomeService');
42+
43+
$this->assertTrue($service->start());
44+
$this->assertTrue($service->stop());
45+
$this->assertTrue($service->enable());
46+
$this->assertTrue($service->disable());
47+
$this->assertTrue($service->reload());
48+
$this->assertTrue($service->restart());
49+
}
50+
51+
public function testServiceCommandsIfProcessIsUnsuccessFulShouldRaiseException()
52+
{
53+
$systemctl = $this->getSystemCtlMock(false);
54+
55+
$service = $systemctl->getService('AwesomeService');
56+
57+
$this->expectException(CommandFailedException::class);
58+
$this->expectExceptionMessage('Failed to start service AwesomeService');
59+
60+
$service->start(true);
61+
}
62+
}

tests/Unit/TimerTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace SystemCtl\Test\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Process\Process;
7+
use Symfony\Component\Process\ProcessBuilder;
8+
use SystemCtl\Exception\CommandFailedException;
9+
use SystemCtl\SystemCtl;
10+
11+
class TimerTest extends TestCase
12+
{
13+
protected function getSystemCtlMock(bool $processState = true): SystemCtl
14+
{
15+
$process = $this->getMockBuilder(Process::class)
16+
->disableOriginalConstructor()
17+
->setMethods(['isSuccessful'])
18+
->getMock();
19+
20+
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
21+
->disableOriginalConstructor()
22+
->setMethods(['getProcess'])
23+
->getMock();
24+
25+
$processBuilder->method('getProcess')->willReturn($process);
26+
27+
/** @var \PHPUnit_Framework_MockObject_MockObject|SystemCtl $systemctl */
28+
$systemctl = $this->getMockBuilder(SystemCtl::class)
29+
->setMethods(['getProcessBuilder'])
30+
->getMock();
31+
32+
$systemctl->method('getProcessBuilder')->willReturn($processBuilder);
33+
$process->method('isSuccessful')->willReturn($processState);
34+
35+
return $systemctl;
36+
}
37+
38+
public function testTimerCommandsIfProcessIsSuccessfulShouldReturnTrue()
39+
{
40+
$systemctl = $this->getSystemCtlMock();
41+
42+
$timer = $systemctl->getTimer('AwesomeTimer');
43+
44+
$this->assertTrue($timer->start());
45+
$this->assertTrue($timer->stop());
46+
$this->assertTrue($timer->enable());
47+
$this->assertTrue($timer->disable());
48+
$this->assertTrue($timer->reload());
49+
$this->assertTrue($timer->restart());
50+
}
51+
52+
public function testTimerCommandsIfProcessIsUnsuccessFulShouldRaiseException()
53+
{
54+
$systemctl = $this->getSystemCtlMock(false);
55+
$timer = $systemctl->getTimer('AwesomeTimer');
56+
57+
$this->expectException(CommandFailedException::class);
58+
$timer->start(true);
59+
}
60+
}

0 commit comments

Comments
 (0)