Skip to content

Commit 5a658a1

Browse files
func0dericanhazstring
authored andcommitted
Add tests to prove broken behavior. (#17)
* Add tests to prove broken behavior. Fix OutputFetcher to properly fetch only the unit name. Introduce new abstract method 'getUnitSuffix' Revert AbstractUnit tests back to use UnitStub. Fix command execution. Fix Integration test for unit. Change fetching of specific units from simply instacing them to properly fetching them from the systemctl output. Fix command execution to apply to correct unit. Add PHPDoc. * Fix identation. * Remove travis test-report tasks for pull requeusts.
1 parent 95afade commit 5a658a1

18 files changed

+514
-59
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ before_script:
1818
script:
1919
- composer cs
2020
- composer test -- --coverage-clover=build/logs/clover.xml --coverage-text
21-
- ./vendor/bin/test-reporter
21+
- 'if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./vendor/bin/test-reporter; fi'

src/Command/CommandDispatcherInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use SystemCtl\Exception\CommandFailedException;
66

7+
/**
8+
* Interface CommandDispatcherInterface
9+
*
10+
* @package SystemCtl\Command
11+
*/
712
interface CommandDispatcherInterface
813
{
914
/**

src/Exception/CommandFailedException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace SystemCtl\Exception;
44

5+
/**
6+
* Class CommandFailedException
7+
*
8+
* @package SystemCtl\Exception
9+
*/
510
class CommandFailedException extends \Exception
611
{
712
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace SystemCtl\Exception;
4+
5+
use Throwable;
6+
7+
/**
8+
* Interface ExceptionInterface
9+
*
10+
* @package SystemCtl\Exception
11+
*/
12+
interface ExceptionInterface extends Throwable
13+
{
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace SystemCtl\Exception;
4+
5+
use RuntimeException;
6+
7+
/**
8+
* Class UnitNotFoundException
9+
*
10+
* @package SystemCtl\Exception
11+
*/
12+
class UnitNotFoundException extends RuntimeException implements ExceptionInterface
13+
{
14+
/**
15+
* @param string $type
16+
* @param string $name
17+
*
18+
* @return UnitNotFoundException
19+
*/
20+
public static function create(string $type, string $name): self
21+
{
22+
return new self(
23+
sprintf('Could not find %s "%s"', ucfirst($type), $name)
24+
);
25+
}
26+
}

src/Exception/UnitTypeNotSupportedException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
namespace SystemCtl\Exception;
55

6+
/**
7+
* Class UnitTypeNotSupportedException
8+
*
9+
* @package SystemCtl\Exception
10+
*/
611
class UnitTypeNotSupportedException extends \Exception
712
{
813

src/SystemCtl.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
use SystemCtl\Command\CommandDispatcherInterface;
66
use SystemCtl\Command\SymfonyCommandDispatcher;
7+
use SystemCtl\Exception\UnitNotFoundException;
78
use SystemCtl\Exception\UnitTypeNotSupportedException;
89
use SystemCtl\Unit\Service;
910
use SystemCtl\Unit\Timer;
1011
use SystemCtl\Unit\UnitInterface;
1112

13+
/**
14+
* Class SystemCtl
15+
*
16+
* @package SystemCtl
17+
*/
1218
class SystemCtl
1319
{
1420
/** @var string systemctl binary path */
@@ -94,7 +100,7 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S
94100
$commands = ['list-units'];
95101

96102
if ($unitPrefix) {
97-
$commands[] = [$unitPrefix . '*'];
103+
$commands[] = $unitPrefix . '*';
98104
}
99105

100106
$output = $this->getCommandDispatcher()->dispatch(...$commands)->getOutput();
@@ -113,7 +119,32 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S
113119
*/
114120
public function getService(string $name): Service
115121
{
116-
return new Service($name, $this->getCommandDispatcher());
122+
$units = $this->listUnits($name, [Service::UNIT]);
123+
124+
$unitName = $this->searchForUnitInUnits($name, $units);
125+
126+
if (is_null($unitName)) {
127+
throw UnitNotFoundException::create(Service::UNIT, $name);
128+
}
129+
130+
return new Service($unitName, $this->getCommandDispatcher());
131+
}
132+
133+
/**
134+
* @param string $unitName
135+
* @param array[] $units
136+
*
137+
* @return null|string
138+
*/
139+
protected function searchForUnitInUnits(string $unitName, array $units): ?string
140+
{
141+
foreach ($units as $unit) {
142+
if ($unit === $unitName) {
143+
return $unit;
144+
}
145+
}
146+
147+
return null;
117148
}
118149

119150
/**
@@ -137,7 +168,15 @@ public function getServices(?string $unitPrefix = null): array
137168
*/
138169
public function getTimer(string $name): Timer
139170
{
140-
return new Timer($name, $this->getCommandDispatcher());
171+
$units = $this->listUnits($name, [Timer::UNIT]);
172+
173+
$unitName = $this->searchForUnitInUnits($name, $units);
174+
175+
if (is_null($unitName)) {
176+
throw UnitNotFoundException::create(Timer::UNIT, $name);
177+
}
178+
179+
return new Timer($unitName, $this->getCommandDispatcher());
141180
}
142181

143182
/**

src/Unit/AbstractUnit.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace SystemCtl\Unit;
44

5-
use Symfony\Component\Process\Process;
65
use SystemCtl\Command\CommandDispatcherInterface;
6+
use SystemCtl\Command\CommandInterface;
77

8+
/**
9+
* Class AbstractUnit
10+
*
11+
* @package SystemCtl\Unit
12+
*/
813
abstract class AbstractUnit implements UnitInterface
914
{
1015
/** @var string */
@@ -13,6 +18,11 @@ abstract class AbstractUnit implements UnitInterface
1318
/** @var CommandDispatcherInterface */
1419
protected $commandDispatcher;
1520

21+
/**
22+
* @var string
23+
*/
24+
protected $unitSuffix;
25+
1626
/**
1727
* Create new service with given name
1828
*
@@ -57,60 +67,83 @@ public function getInstanceName(): ?string
5767
return $instanceName;
5868
}
5969

70+
/**
71+
* @return string
72+
*/
73+
abstract protected function getUnitSuffix(): string;
74+
75+
/**
76+
* @param array $commands
77+
*
78+
* @return CommandInterface
79+
*/
80+
public function execute(...$commands): CommandInterface
81+
{
82+
$commands[] = implode(
83+
'.',
84+
[
85+
$this->name,
86+
$this->getUnitSuffix(),
87+
]
88+
);
89+
90+
return $this->commandDispatcher->dispatch(...$commands);
91+
}
92+
6093
/**
6194
* @return bool
6295
*/
6396
public function start(): bool
6497
{
65-
return $this->commandDispatcher->dispatch(__FUNCTION__)->isSuccessful();
98+
return $this->execute(__FUNCTION__)->isSuccessful();
6699
}
67100

68101
/**
69102
* @return bool
70103
*/
71104
public function stop(): bool
72105
{
73-
return $this->commandDispatcher->dispatch(__FUNCTION__)->isSuccessful();
106+
return $this->execute(__FUNCTION__)->isSuccessful();
74107
}
75108

76109
/**
77110
* @return bool
78111
*/
79112
public function disable(): bool
80113
{
81-
return $this->commandDispatcher->dispatch(__FUNCTION__)->isSuccessful();
114+
return $this->execute(__FUNCTION__)->isSuccessful();
82115
}
83116

84117
/**
85118
* @return bool
86119
*/
87120
public function reload(): bool
88121
{
89-
return $this->commandDispatcher->dispatch(__FUNCTION__)->isSuccessful();
122+
return $this->execute(__FUNCTION__)->isSuccessful();
90123
}
91124

92125
/**
93126
* @return bool
94127
*/
95128
public function restart(): bool
96129
{
97-
return $this->commandDispatcher->dispatch(__FUNCTION__)->isSuccessful();
130+
return $this->execute(__FUNCTION__)->isSuccessful();
98131
}
99132

100133
/**
101134
* @return bool
102135
*/
103136
public function enable(): bool
104137
{
105-
return $this->commandDispatcher->dispatch(__FUNCTION__)->isSuccessful();
138+
return $this->execute(__FUNCTION__)->isSuccessful();
106139
}
107140

108141
/**
109142
* @return bool
110143
*/
111144
public function isEnabled(): bool
112145
{
113-
$output = $this->commandDispatcher->dispatch('is-enabled')->getOutput();
146+
$output = $this->execute('is-enabled')->getOutput();
114147

115148
return trim($output) === 'enabled';
116149
}
@@ -120,7 +153,7 @@ public function isEnabled(): bool
120153
*/
121154
public function isActive(): bool
122155
{
123-
$output = $this->commandDispatcher->dispatch('is-active')->getOutput();
156+
$output = $this->execute('is-active')->getOutput();
124157

125158
return trim($output) === 'active';
126159
}

src/Unit/Service.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,23 @@
22

33
namespace SystemCtl\Unit;
44

5+
/**
6+
* Class Service
7+
*
8+
* @package SystemCtl\Unit
9+
*/
510
class Service extends AbstractUnit
611
{
712
/**
813
* @var string
914
*/
1015
public const UNIT = 'service';
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected function getUnitSuffix(): string
21+
{
22+
return static::UNIT;
23+
}
1124
}

src/Unit/Timer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,23 @@
22

33
namespace SystemCtl\Unit;
44

5+
/**
6+
* Class Timer
7+
*
8+
* @package SystemCtl\Unit
9+
*/
510
class Timer extends AbstractUnit
611
{
712
/**
813
* @var string
914
*/
1015
public const UNIT = 'timer';
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected function getUnitSuffix(): string
21+
{
22+
return static::UNIT;
23+
}
1124
}

0 commit comments

Comments
 (0)