Skip to content

Commit

Permalink
Merge pull request #678 from hydephp/serve-command-test
Browse files Browse the repository at this point in the history
Refactor the serve command and add more unit tests for it
  • Loading branch information
caendesilva authored Dec 21, 2024
2 parents d407e1c + 3dcc82c commit 326982c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,7 @@ protected function checkArgvForOption(string $name): ?string

protected function openInBrowser(string $path = '/'): void
{
$binary = match (PHP_OS_FAMILY) {
'Windows' => 'start',
'Darwin' => 'open',
'Linux' => 'xdg-open',
default => null
};
$binary = $this->getOpenCommand(PHP_OS_FAMILY);

$command = sprintf('%s http://%s:%d', $binary, $this->getHostSelection(), $this->getPortSelection());
$command = rtrim("$command/$path", '/');
Expand All @@ -164,4 +159,14 @@ protected function openInBrowser(string $path = '/'): void
$this->newLine();
}
}

protected function getOpenCommand(string $osFamily): ?string
{
return match ($osFamily) {
'Windows' => 'start',
'Darwin' => 'open',
'Linux' => 'xdg-open',
default => null
};
}
}
25 changes: 25 additions & 0 deletions tests/Unit/ServeCommandOptionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,26 @@ public function testOpenInBrowserThatFails()
$command->openInBrowser();
}

public function testGetOpenCommandForWindows()
{
$this->assertSame('start', $this->getMock()->getOpenCommand('Windows'));
}

public function testGetOpenCommandForDarwin()
{
$this->assertSame('open', $this->getMock()->getOpenCommand('Darwin'));
}

public function testGetOpenCommandForLinux()
{
$this->assertSame('xdg-open', $this->getMock()->getOpenCommand('Linux'));
}

public function testGetOpenCommandForUnknownOS()
{
$this->assertNull($this->getMock()->getOpenCommand('UnknownOS'));
}

protected function getTestRunnerBinary(): string
{
return match (PHP_OS_FAMILY) {
Expand Down Expand Up @@ -384,6 +404,11 @@ public function option($key = null)
{
return $this->input->getOption($key);
}

public function getOpenCommand(string $osFamily): ?string
{
return parent::getOpenCommand($osFamily);
}
}

class InputMock
Expand Down

0 comments on commit 326982c

Please sign in to comment.