Skip to content

Commit d69bd8f

Browse files
authored
Merge pull request #1867 from hydephp/serve-command-test
Refactor the serve command and add more unit tests for it
2 parents 2ed18ba + 5ae8812 commit d69bd8f

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/framework/src/Console/Commands/ServeCommand.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,7 @@ protected function checkArgvForOption(string $name): ?string
146146

147147
protected function openInBrowser(string $path = '/'): void
148148
{
149-
$binary = match (PHP_OS_FAMILY) {
150-
'Windows' => 'start',
151-
'Darwin' => 'open',
152-
'Linux' => 'xdg-open',
153-
default => null
154-
};
149+
$binary = $this->getOpenCommand(PHP_OS_FAMILY);
155150

156151
$command = sprintf('%s http://%s:%d', $binary, $this->getHostSelection(), $this->getPortSelection());
157152
$command = rtrim("$command/$path", '/');
@@ -164,4 +159,14 @@ protected function openInBrowser(string $path = '/'): void
164159
$this->newLine();
165160
}
166161
}
162+
163+
protected function getOpenCommand(string $osFamily): ?string
164+
{
165+
return match ($osFamily) {
166+
'Windows' => 'start',
167+
'Darwin' => 'open',
168+
'Linux' => 'xdg-open',
169+
default => null
170+
};
171+
}
167172
}

packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,26 @@ public function testOpenInBrowserThatFails()
319319
$command->openInBrowser();
320320
}
321321

322+
public function testGetOpenCommandForWindows()
323+
{
324+
$this->assertSame('start', $this->getMock()->getOpenCommand('Windows'));
325+
}
326+
327+
public function testGetOpenCommandForDarwin()
328+
{
329+
$this->assertSame('open', $this->getMock()->getOpenCommand('Darwin'));
330+
}
331+
332+
public function testGetOpenCommandForLinux()
333+
{
334+
$this->assertSame('xdg-open', $this->getMock()->getOpenCommand('Linux'));
335+
}
336+
337+
public function testGetOpenCommandForUnknownOS()
338+
{
339+
$this->assertNull($this->getMock()->getOpenCommand('UnknownOS'));
340+
}
341+
322342
protected function getTestRunnerBinary(): string
323343
{
324344
return match (PHP_OS_FAMILY) {
@@ -388,6 +408,11 @@ public function option($key = null)
388408
{
389409
return $this->input->getOption($key);
390410
}
411+
412+
public function getOpenCommand(string $osFamily): ?string
413+
{
414+
return parent::getOpenCommand($osFamily);
415+
}
391416
}
392417

393418
class InputMock

0 commit comments

Comments
 (0)