From 75c335fa40b0ba42adaa417724a5f50481dba148 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 17 Jul 2024 15:44:42 +0200 Subject: [PATCH 1/6] Extract helper method to get the open command --- .../src/Console/Commands/ServeCommand.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 943b6528155..e39f94403b5 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/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(); $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 + { + return match (PHP_OS_FAMILY) { + 'Windows' => 'start', + 'Darwin' => 'open', + 'Linux' => 'xdg-open', + default => null + }; + } } From 30974d8ea309ac8356d97a95360a675266a09016 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 17 Jul 2024 15:48:07 +0200 Subject: [PATCH 2/6] Add testing class to get mock --- .../Unit/ServeCommandOptionsUnitTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 23061551f5d..4ce40e1f595 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -360,6 +360,29 @@ protected function openInBrowser(string $path = '/'): void } }; } + + protected function getMockWithCustomOS(string $osFamily): ServeCommandMock + { + return new class($osFamily) extends ServeCommandMock { + private string $osFamily; + + public function __construct(string $osFamily) + { + parent::__construct(); + $this->osFamily = $osFamily; + } + + protected function getOpenCommand(): ?string + { + return match ($this->osFamily) { + 'Windows' => 'start', + 'Darwin' => 'open', + 'Linux' => 'xdg-open', + default => null + }; + } + }; + } } /** From d0d6dff262118e01995763466f05d22050069f85 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 17 Jul 2024 15:48:45 +0200 Subject: [PATCH 3/6] Test the open command getter for all paths --- .../Unit/ServeCommandOptionsUnitTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 4ce40e1f595..3944a559f76 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -319,6 +319,30 @@ public function testOpenInBrowserThatFails() $command->openInBrowser(); } + public function testGetOpenCommandForWindows() + { + $command = $this->getMockWithCustomOS('Windows'); + $this->assertSame('start', $command->getOpenCommand()); + } + + public function testGetOpenCommandForDarwin() + { + $command = $this->getMockWithCustomOS('Darwin'); + $this->assertSame('open', $command->getOpenCommand()); + } + + public function testGetOpenCommandForLinux() + { + $command = $this->getMockWithCustomOS('Linux'); + $this->assertSame('xdg-open', $command->getOpenCommand()); + } + + public function testGetOpenCommandForUnknownOS() + { + $command = $this->getMockWithCustomOS('UnknownOS'); + $this->assertNull($command->getOpenCommand()); + } + protected function getTestRunnerBinary(): string { return match (PHP_OS_FAMILY) { From c64ac7133a8e62f0ebdadc44e8f656650b8ac2a3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 17 Jul 2024 15:48:15 +0200 Subject: [PATCH 4/6] Revert "Add testing class to get mock" This reverts commit 3611da878586866b062626c0b1ed2b3127b472fa. --- .../Unit/ServeCommandOptionsUnitTest.php | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 3944a559f76..7a7bb7af5f2 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -384,29 +384,6 @@ protected function openInBrowser(string $path = '/'): void } }; } - - protected function getMockWithCustomOS(string $osFamily): ServeCommandMock - { - return new class($osFamily) extends ServeCommandMock { - private string $osFamily; - - public function __construct(string $osFamily) - { - parent::__construct(); - $this->osFamily = $osFamily; - } - - protected function getOpenCommand(): ?string - { - return match ($this->osFamily) { - 'Windows' => 'start', - 'Darwin' => 'open', - 'Linux' => 'xdg-open', - default => null - }; - } - }; - } } /** From 3041bcbbba80158f109c205450d17e153df5d892 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 17 Jul 2024 15:51:53 +0200 Subject: [PATCH 5/6] Refactor to use a parameter around the constant --- packages/framework/src/Console/Commands/ServeCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index e39f94403b5..86008ee805e 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -146,7 +146,7 @@ protected function checkArgvForOption(string $name): ?string protected function openInBrowser(string $path = '/'): void { - $binary = $this->getOpenCommand(); + $binary = $this->getOpenCommand(PHP_OS_FAMILY); $command = sprintf('%s http://%s:%d', $binary, $this->getHostSelection(), $this->getPortSelection()); $command = rtrim("$command/$path", '/'); @@ -160,9 +160,9 @@ protected function openInBrowser(string $path = '/'): void } } - protected function getOpenCommand(): ?string + protected function getOpenCommand(string $osFamily): ?string { - return match (PHP_OS_FAMILY) { + return match ($osFamily) { 'Windows' => 'start', 'Darwin' => 'open', 'Linux' => 'xdg-open', From 5ae8812cf53e2a506cbc54ed7daa0e8462c331e8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 17 Jul 2024 15:52:04 +0200 Subject: [PATCH 6/6] Update the unit test to use cleaned up code --- .../tests/Unit/ServeCommandOptionsUnitTest.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 7a7bb7af5f2..ea003f8feaf 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -321,26 +321,22 @@ public function testOpenInBrowserThatFails() public function testGetOpenCommandForWindows() { - $command = $this->getMockWithCustomOS('Windows'); - $this->assertSame('start', $command->getOpenCommand()); + $this->assertSame('start', $this->getMock()->getOpenCommand('Windows')); } public function testGetOpenCommandForDarwin() { - $command = $this->getMockWithCustomOS('Darwin'); - $this->assertSame('open', $command->getOpenCommand()); + $this->assertSame('open', $this->getMock()->getOpenCommand('Darwin')); } public function testGetOpenCommandForLinux() { - $command = $this->getMockWithCustomOS('Linux'); - $this->assertSame('xdg-open', $command->getOpenCommand()); + $this->assertSame('xdg-open', $this->getMock()->getOpenCommand('Linux')); } public function testGetOpenCommandForUnknownOS() { - $command = $this->getMockWithCustomOS('UnknownOS'); - $this->assertNull($command->getOpenCommand()); + $this->assertNull($this->getMock()->getOpenCommand('UnknownOS')); } protected function getTestRunnerBinary(): string @@ -412,6 +408,11 @@ public function option($key = null) { return $this->input->getOption($key); } + + public function getOpenCommand(string $osFamily): ?string + { + return parent::getOpenCommand($osFamily); + } } class InputMock