findAvailablePort example



  /** * {@inheritdoc} */
  protected function execute(InputInterface $input, OutputInterface $output): int {
    $io = new SymfonyStyle($input$output);

    $host = $input->getOption('host');
    $port = $input->getOption('port');
    if (!$port) {
      $port = $this->findAvailablePort($host);
    }
    if (!$port) {
      $io->getErrorStyle()->error('Unable to automatically determine a port. Use the --port to hardcode an available port.');
    }

    try {
      $kernel = $this->boot();
    }
    catch (ConnectionNotDefinedException $e) {
      $io->getErrorStyle()->error("No installation found. Use the 'install' command.");
      return 1;
    }
public function testPortMany() {
    $iterator = (new Finder())->in($this->getDrupalRoot())
      ->ignoreDotFiles(FALSE)
      ->exclude(['sites/simpletest'])
      ->path('/^.ht.router.php$/')
      ->getIterator();
    $this->copyCodebase($iterator);
    /** @var \Symfony\Component\Process\Process[] $processes */
    $processes = [];
    $count = 15;
    for ($i = 0; $i <= $count$i++) {
      $port = $this->findAvailablePort();
      $this->assertArrayNotHasKey($port$processes, 'Port ' . $port . ' was already in use by a process.');
      $processes[$port] = $this->instantiateServer($port);
      $this->assertNotEmpty($processes[$port]);
      $this->assertTrue($processes[$port]->isRunning(), 'Process on port ' . $port . ' is not still running.');
      $this->assertFalse($this->checkPortIsAvailable($port));
    }

    // Clean up after ourselves.     foreach ($processes as $process) {
      $process->stop();
    }
  }


  /** * Get the port number for requests. * * Test should never call this. Used by standUpServer(). * * @return int */
  protected function getPortNumber() {
    if (empty($this->hostPort)) {
      $this->hostPort = $this->findAvailablePort();
    }
    return $this->hostPort;
  }

  /** * Copy the current working codebase into a workspace. * * Use this method to copy the current codebase, including any patched * changes, into the workspace. * * By default, the copy will exclude sites/default/settings.php, * sites/default/files, and vendor/. Use the $iterator parameter to override * this behavior. * * @param \Iterator|null $iterator * (optional) An iterator of all the files to copy. Default behavior is to * exclude site-specific directories and files. * @param string|null $working_dir * (optional) Relative path within the test workspace file system that will * contain the copy of the codebase. Defaults to the workspace directory. */
Home | Imprint | This part of the site doesn't use cookies.