From 3dcc82c5fab40d9d3c66c27e9e43568f6f84f2d0 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 17 Jul 2024 13:57:12 +0000 Subject: [PATCH] Merge pull request #1867 from hydephp/serve-command-test Refactor the serve command and add more unit tests for it https://github.com/hydephp/develop/commit/d69bd8fb2a4ced3fcab3ab3b553001ad3a633185 --- src/Console/Commands/ServeCommand.php | 17 +++++++++------ tests/Unit/ServeCommandOptionsUnitTest.php | 25 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Console/Commands/ServeCommand.php b/src/Console/Commands/ServeCommand.php index 5bb8ce0b..bcb8e11a 100644 --- a/src/Console/Commands/ServeCommand.php +++ b/src/Console/Commands/ServeCommand.php @@ -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", '/'); @@ -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 + }; + } } diff --git a/tests/Unit/ServeCommandOptionsUnitTest.php b/tests/Unit/ServeCommandOptionsUnitTest.php index 663367a2..f76d70e3 100644 --- a/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/tests/Unit/ServeCommandOptionsUnitTest.php @@ -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) { @@ -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