Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jun 24, 2024
1 parent b2f45d1 commit 5a19634
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
26 changes: 8 additions & 18 deletions src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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'));

Check failure on line 140 in src/Command/Serve.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ForbiddenCode

src/Command/Serve.php:140:49: ForbiddenCode: Unsafe shell_exec (see https://psalm.dev/002)

Check failure on line 140 in src/Command/Serve.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

ForbiddenCode

src/Command/Serve.php:140:49: ForbiddenCode: Unsafe shell_exec (see https://psalm.dev/002)

Check failure on line 140 in src/Command/Serve.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

ForbiddenCode

src/Command/Serve.php:140:49: ForbiddenCode: Unsafe shell_exec (see https://psalm.dev/002)

Check failure on line 140 in src/Command/Serve.php

View workflow job for this annotation

GitHub Actions / psalm80 / PHP 8.0-ubuntu-latest

ForbiddenCode

src/Command/Serve.php:140:49: ForbiddenCode: Unsafe shell_exec (see https://psalm.dev/002)
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;
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down
17 changes: 12 additions & 5 deletions tests/ServeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
]);
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 5a19634

Please sign in to comment.