diff --git a/src/Command/Serve.php b/src/Command/Serve.php index 33567e76..af57566f 100644 --- a/src/Command/Serve.php +++ b/src/Command/Serve.php @@ -79,7 +79,7 @@ public function configure(): void $this->defaultWorkers ) ->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.') - ->addOption('open', 'o', InputOption::VALUE_OPTIONAL, 'Opens the serving server in the default browser.') + ->addOption('open', 'o', InputOption::VALUE_OPTIONAL, 'Opens the serving server in the default browser.', false) ->addOption('xdebug', 'x', InputOption::VALUE_OPTIONAL, 'Enables XDEBUG session.', false); } @@ -133,13 +133,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($this->isAddressTaken($address)) { if ($this->isWindows()) { - $io->error("Port {$port} is taken by another process"); + $io->error("Port {$port} is taken by another process."); return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; } $runningCommandPIDs = trim((string) shell_exec('lsof -ti :8080 -s TCP:LISTEN')); if (empty($runningCommandPIDs)) { - $io->error("Port {$port} is taken by another process"); + $io->error("Port {$port} is taken by another process."); return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; } @@ -234,22 +234,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $openInBrowser = $input->hasOption('open') && $input->getOption('open') === null; - //if ($openInBrowser) { - // passthru('open http://' . $address); - //} + if ($openInBrowser) { + passthru('open http://' . $address); + } passthru($command, $result); - //$descriptorspec = array( - // 0 => array("pipe", "r"), // stdin - канал, из которого дочерний процесс будет читать - // 1 => array("pipe", "w"), // stdout - канал, в который дочерний процесс будет записывать - // 2 => array("file", "/tmp/error-output.txt", "a") // stderr - файл для записи - //); - - //$cwd = dirname($documentRoot); - //$process = proc_open($command, $descriptorspec, $cwd); - // - //var_dump($process); - - return 0; + + return $result; } /** diff --git a/tests/ServeCommandTest.php b/tests/ServeCommandTest.php index e71d42bf..f8b5b52c 100644 --- a/tests/ServeCommandTest.php +++ b/tests/ServeCommandTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\Console\Tests; +use RuntimeException; use Symfony\Component\Console\Tester\CommandCompletionTester; use Symfony\Component\Console\Tester\CommandTester; use Yiisoft\Yii\Console\Command\Serve; @@ -124,10 +125,17 @@ public function testErrorWhenAddressIsTaken(): void $output ); } else { - $socket = socket_create_listen(8080); + $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + + $address = '127.0.0.1'; + $port = 8081; + socket_bind($sock, $address, $port) or throw new RuntimeException('Could not bind to address'); + + socket_listen($sock); $commandCreate->execute([ - 'address' => '127.0.0.1:8080', + 'address' => '127.0.0.1', + '--port' => $port, '--docroot' => 'tests', '--env' => 'test', ]); @@ -137,11 +145,10 @@ public function testErrorWhenAddressIsTaken(): void $this->assertSame(Serve::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS, $commandCreate->getStatusCode()); $this->assertStringContainsString( - '[ERROR] http://127.0.0.1:8080 is taken by another process.', + '[ERROR] Port ' . $port . ' is taken by another process.', $output ); - - socket_close($socket); + socket_close($sock); } }