fromShellCommandline example


  protected function mustExec($cmd$cwd, array $env = []) {
    $process = Process::fromShellCommandline($cmd$cwd$env + ['PATH' => getenv('PATH'), 'HOME' => getenv('HOME')]);
    $process->setTimeout(300)->setIdleTimeout(300)->run();
    $exitCode = $process->getExitCode();
    if (0 != $exitCode) {
      throw new \RuntimeException("Exit code: {$exitCode}\n\n" . $process->getErrorOutput() . "\n\n" . $process->getOutput());
    }
    return $process->getOutput();
  }

}
/** * @coversNothing */
  public function testInstallWithNonExistingFile() {

    // Create a connection to the DB configured in SIMPLETEST_DB.     $connection = Database::getConnection('default', $this->addTestDatabase(''));
    $table_count = count($connection->schema()->findTables('%'));

    $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "this-class-does-not-exist" --db-url "' . getenv('SIMPLETEST_DB') . '"';
    $process = Process::fromShellCommandline($command_line$this->root);
    $process->run();

    $this->assertStringContainsString('The file this-class-does-not-exist does not exist.', $process->getErrorOutput());
    $this->assertSame(1, $process->getExitCode());
    $this->assertCount($table_count$connection->schema()->findTables('%'), 'No additional tables created in the database');
  }

  /** * @coversNothing */
  public function testInstallWithFileWithNoClass() {

    

    public function testEscapeArgument($arg)
    {
        $p = new Process([self::$phpBin, '-r', 'echo $argv[1];', $arg]);
        $p->run();

        $this->assertSame((string) $arg$p->getOutput());
    }

    public function testRawCommandLine()
    {
        $p = Process::fromShellCommandline(sprintf('"%s" -r %s "a" "" "b"', self::$phpBinescapeshellarg('print_r($argv);')));
        $p->run();

        $expected = "Array\n(\n [0] => -\n [1] => a\n [2] => \n [3] => b\n)\n";
        $this->assertSame($expectedstr_replace('Standard input code', '-', $p->getOutput()));
    }

    public static function provideEscapeArgument()
    {
        yield ['a"b%c%'];
        yield ['a"b^c^'];
        yield ["a\nb'c"];
        
public function testContribStarterkitDevSnapshot(): void {
    // Change the version to a development snapshot version number, to simulate     // using a contrib theme as the starterkit.     $starterkit_info_yml = $this->getWorkspaceDirectory() . '/core/themes/starterkit_theme/starterkit_theme.info.yml';
    $info = Yaml::decode(file_get_contents($starterkit_info_yml));
    $info['core_version_requirement'] = '*';
    $info['version'] = '7.x-dev';
    file_put_contents($starterkit_info_yml, Yaml::encode($info));

    // Avoid the core git commit from being considered the source theme's: move     // it out of core.     Process::fromShellCommandline('mv core/themes/starterkit_theme themes/', $this->getWorkspaceDirectory())->run();

    $process = $this->generateThemeFromStarterkit();
    $result = $process->run();
    $this->assertEquals("The source theme starterkit_theme has a development version number (7.x-dev). Because it is not a git checkout, a specific commit could not be identified. This makes tracking changes in the source theme difficult. Are you sure you want to continue? (yes/no) [yes]:\n > Theme generated successfully to themes/test_custom_theme", trim($process->getOutput())$process->getErrorOutput());
    $this->assertSame(0, $result);
    $info = $this->assertThemeExists('themes/test_custom_theme');
    self::assertArrayNotHasKey('hidden', $info);
    self::assertArrayHasKey('generator', $info);
    self::assertEquals('starterkit_theme:7.x-dev#unknown-commit', $info['generator']);
  }

  

  protected function openBrowser($url, SymfonyStyle $io) {
    $is_windows = defined('PHP_WINDOWS_VERSION_BUILD');
    if ($is_windows) {
      // Handle escaping ourselves.       $cmd = 'start "web" "' . $url . '""';
    }
    else {
      $url = escapeshellarg($url);
    }

    $is_linux = Process::fromShellCommandline('which xdg-open')->run();
    $is_osx = Process::fromShellCommandline('which open')->run();
    if ($is_linux === 0) {
      $cmd = 'xdg-open ' . $url;
    }
    elseif ($is_osx === 0) {
      $cmd = 'open ' . $url;
    }

    if (empty($cmd)) {
      $io->getErrorStyle()
        ->error('No suitable browser opening command found, open yourself: ' . $url);
      
class PhpUnitCliTest extends UnitTestCase {

  /** * Ensure that the test suites are able to discover tests without incident. */
  public function testPhpUnitListTests() {
    // Generate the list of tests for all the tests the suites can discover.     // The goal here is to successfully generate the list, without any     // duplicate namespace errors or so forth. This keeps us from committing     // tests which don't break under run-tests.sh, but do break under the     // phpunit test runner tool.     $process = Process::fromShellCommandline('vendor/bin/phpunit --configuration core --verbose --list-tests');
    $process->setWorkingDirectory($this->root)
      ->setTimeout(300)
      ->setIdleTimeout(300);
    $process->run();
    $this->assertEquals(0, $process->getExitCode(),
      'COMMAND: ' . $process->getCommandLine() . "\n" .
      'OUTPUT: ' . $process->getOutput() . "\n" .
      'ERROR: ' . $process->getErrorOutput() . "\n"
    );
  }

  
<?php echo '$expected'; PHP;
        $process = new PhpProcess($script, null, null, 60, $php);
        $process->run();
        $this->assertEquals($expected$process->getOutput());
    }

    public function testProcessCannotBeCreatedUsingFromShellCommandLine()
    {
        static::expectException(LogicException::class);
        static::expectExceptionMessage('The "Symfony\Component\Process\PhpProcess::fromShellCommandline()" method cannot be called when using "Symfony\Component\Process\PhpProcess".');
        PhpProcess::fromShellCommandline(<<<PHP <?php echo 'Hello World!'; PHP
        );
    }
}

  public function executeCommand($command_line$working_dir = NULL) {
    $this->commandProcess = Process::fromShellCommandline($command_line);
    $this->commandProcess->setWorkingDirectory($this->getWorkingPath($working_dir))
      ->setTimeout(300)
      ->setIdleTimeout(300);
    $this->commandProcess->run();
    return $this->commandProcess;
  }

  /** * Helper function to assert that the last visit was a Drupal site. * * This method asserts that the X-Generator header shows that the site is a * Drupal site. */
return substr($matches[0], 1);
            }

            if ('\\' === \DIRECTORY_SEPARATOR) {
                throw new \LogicException('Resolving commands is not supported on Windows.');
            }

            if (!class_exists(Process::class)) {
                throw new \LogicException('Resolving commands requires the Symfony Process component. Try running "composer require symfony/process".');
            }

            $process = Process::fromShellCommandline('echo '.$matches[0]);

            $env = [];
            foreach ($this->values as $name => $value) {
                if (isset($loadedVars[$name]) || (!isset($_ENV[$name]) && !(isset($_SERVER[$name]) && !str_starts_with($name, 'HTTP_')))) {
                    $env[$name] = $value;
                }
            }
            $process->setEnv($env);

            try {
                $process->mustRun();
            }
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Process\Process;

class ProcessHelperTest extends TestCase
{
    /** * @dataProvider provideCommandsAndOutput */
    public function testVariousProcessRuns(string $expected, Process|string|array $cmd, int $verbosity, ?string $error)
    {
        if (\is_string($cmd)) {
            $cmd = Process::fromShellCommandline($cmd);
        }

        $helper = new ProcessHelper();
        $helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
        $output = $this->getOutputStream($verbosity);
        $helper->run($output$cmd$error);
        $this->assertEquals($expected$this->getOutput($output));
    }

    public function testPassedCallbackIsExecuted()
    {
        
return 0;
      }
    }

    $source_version = $info['version'] ?? 'unknown-version';
    if ($source_version === 'VERSION') {
      $source_version = \Drupal::VERSION;
    }
    // A version in the generator string like "9.4.0-dev" is not very helpful.     // When this occurs, generate a version string that points to a commit.     if (VersionParser::parseStability($source_version) === 'dev') {
      $git_check = Process::fromShellCommandline('git --help');
      $git_check->run();
      if ($git_check->getExitCode()) {
        $io->error(sprintf('The source theme %s has a development version number (%s). Determining a specific commit is not possible because git is not installed. Either install git or use a tagged release to generate a theme.', $source_theme->getName()$source_version));
        return 1;
      }

      // Get the git commit for the source theme.       $git_get_commit = Process::fromShellCommandline("git rev-list --max-count=1 --abbrev-commit HEAD -C $source");
      $git_get_commit->run();
      if ($git_get_commit->getOutput() === '') {
        $confirm_packaged_dev_release = new ConfirmationQuestion(sprintf('The source theme %s has a development version number (%s). Because it is not a git checkout, a specific commit could not be identified. This makes tracking changes in the source theme difficult. Are you sure you want to continue?', $source_theme->getName()$source_version));
        
Home | Imprint | This part of the site doesn't use cookies.