Skip to content

Commit 0980b37

Browse files
committed
Remove default wildcard in listUnits()
1 parent 6644f61 commit 0980b37

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

src/SystemCtl.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ public static function setAssetPath(string $assetPath): void
101101
}
102102

103103
/**
104-
* List all supported units
104+
* List all supported units by using a given unit prefix.
105+
*
106+
* This prefix can be used with any wildcard combination.
107+
* E.G.:
108+
* - *name*
109+
* - name*
110+
* - *name
111+
* - *n*e*
105112
*
106113
* @param null|string $unitPrefix
107114
* @param string[] $unitTypes
@@ -113,7 +120,7 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S
113120
$commands = ['list-units'];
114121

115122
if ($unitPrefix) {
116-
$commands[] = $unitPrefix . '*';
123+
$commands[] = $unitPrefix;
117124
}
118125

119126
$output = $this->getCommandDispatcher()->dispatch(...$commands)->getOutput();
@@ -132,32 +139,13 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S
132139
*/
133140
public function getService(string $name): Service
134141
{
135-
$units = $this->listUnits($name, [Service::UNIT]);
136-
137-
$unitName = $this->searchForUnitInUnits($name, $units);
142+
$unitNames = $this->listUnits($name, [Service::UNIT]);
138143

139-
if (is_null($unitName)) {
144+
if (empty($unitNames)) {
140145
throw UnitNotFoundException::create(Service::UNIT, $name);
141146
}
142147

143-
return new Service($unitName, $this->getCommandDispatcher());
144-
}
145-
146-
/**
147-
* @param string $unitName
148-
* @param string[] $units
149-
*
150-
* @return null|string
151-
*/
152-
protected function searchForUnitInUnits(string $unitName, array $units): ?string
153-
{
154-
foreach ($units as $unit) {
155-
if ($unit === $unitName) {
156-
return $unit;
157-
}
158-
}
159-
160-
return null;
148+
return new Service($unitNames[0], $this->getCommandDispatcher());
161149
}
162150

163151
/**
@@ -181,15 +169,13 @@ public function getServices(?string $unitPrefix = null): array
181169
*/
182170
public function getTimer(string $name): Timer
183171
{
184-
$units = $this->listUnits($name, [Timer::UNIT]);
185-
186-
$unitName = $this->searchForUnitInUnits($name, $units);
172+
$unitNames = $this->listUnits($name, [Timer::UNIT]);
187173

188-
if (is_null($unitName)) {
174+
if (empty($unitNames)) {
189175
throw UnitNotFoundException::create(Timer::UNIT, $name);
190176
}
191177

192-
return new Timer($unitName, $this->getCommandDispatcher());
178+
return new Timer($unitNames[0], $this->getCommandDispatcher());
193179
}
194180

195181
/**

tests/Integration/SystemCtlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function itShouldReturnUnitAfterInstall()
212212

213213
$commandDispatcherStub = $this->buildCommandDispatcherStub();
214214
$commandDispatcherStub
215-
->dispatch('list-units', $unitName . '*')
215+
->dispatch('list-units', $unitName)
216216
->willReturn($this->buildCommandStub('testService.service Active Running'))
217217
->shouldBeCalled();
218218

tests/Unit/SystemCtlTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnService
9393
$output = ' testService.service Active running';
9494
$commandDispatcherStub = $this->buildCommandDispatcherStub();
9595
$commandDispatcherStub
96-
->dispatch('list-units', $unitName . '*')
96+
->dispatch('list-units', $unitName)
9797
->willReturn($this->buildCommandStub($output));
9898

9999
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
@@ -112,7 +112,7 @@ public function itShouldReturnAServiceOnServiceGetting()
112112
$output = ' testService.service Active running';
113113
$commandDispatcherStub = $this->buildCommandDispatcherStub();
114114
$commandDispatcherStub
115-
->dispatch('list-units', $unitName . '*')
115+
->dispatch('list-units', $unitName)
116116
->willReturn($this->buildCommandStub($output));
117117

118118
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
@@ -130,7 +130,7 @@ public function itShouldReturnAServiceWithTheCorrectNameOnServiceGetting()
130130
$output = ' testService.service Active running';
131131
$commandDispatcherStub = $this->buildCommandDispatcherStub();
132132
$commandDispatcherStub
133-
->dispatch('list-units', $unitName . '*')
133+
->dispatch('list-units', $unitName)
134134
->willReturn($this->buildCommandStub($output));
135135

136136
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
@@ -147,7 +147,7 @@ public function itShouldThrowAnExceptionIfNoServiceCouldBeFound()
147147
$unitName = 'testService';
148148
$commandDispatcherStub = $this->buildCommandDispatcherStub();
149149
$commandDispatcherStub
150-
->dispatch('list-units', $unitName . '*')
150+
->dispatch('list-units', $unitName)
151151
->willReturn($this->buildCommandStub(''));
152152

153153
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
@@ -165,7 +165,7 @@ public function itShouldCallCommandDispatcherWithListUnitsAndUnitPrefixOnTimerGe
165165
$output = ' testTimer.timer Active running';
166166
$commandDispatcherStub = $this->buildCommandDispatcherStub();
167167
$commandDispatcherStub
168-
->dispatch('list-units', $unitName . '*')
168+
->dispatch('list-units', $unitName)
169169
->willReturn($this->buildCommandStub($output));
170170

171171
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
@@ -183,7 +183,7 @@ public function itShouldThrowAnExeceptionIfNotTimerCouldBeFound()
183183
$unitName = 'testTimer';
184184
$commandDispatcherStub = $this->buildCommandDispatcherStub();
185185
$commandDispatcherStub
186-
->dispatch('list-units', $unitName . '*')
186+
->dispatch('list-units', $unitName)
187187
->willReturn($this->buildCommandStub(''));
188188

189189
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
@@ -201,7 +201,7 @@ public function itShouldReturnATimerOnTimerGetting()
201201
$output = ' testService.service Active running';
202202
$commandDispatcherStub = $this->buildCommandDispatcherStub();
203203
$commandDispatcherStub
204-
->dispatch('list-units', $unitName . '*')
204+
->dispatch('list-units', $unitName)
205205
->willReturn($this->buildCommandStub($output));
206206

207207
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());
@@ -219,7 +219,7 @@ public function itShouldReturnATimerWithTheCorrectNameOnTimerGetting()
219219
$output = ' testService.service Active running';
220220
$commandDispatcherStub = $this->buildCommandDispatcherStub();
221221
$commandDispatcherStub
222-
->dispatch('list-units', $unitName . '*')
222+
->dispatch('list-units', $unitName)
223223
->willReturn($this->buildCommandStub($output));
224224

225225
$systemctl = (new SystemCtl())->setCommandDispatcher($commandDispatcherStub->reveal());

0 commit comments

Comments
 (0)