escapeArg() public static method

Escape a string to be used as a shell argument.
public static escapeArg ( $s ) : string
return string
Beispiel #1
0
 /**
  * Runs single test.
  * @param  int RUN_ASYNC | RUN_COLLECT_ERRORS
  * @return void
  */
 public function run($flags = NULL)
 {
     putenv(Environment::RUNNER . '=1');
     putenv(Environment::COLORS . '=' . (int) Environment::$useColors);
     $this->proc = proc_open($this->interpreter->getCommandLine() . ' -n -d register_argc_argv=on ' . \Tester\Helpers::escapeArg($this->file) . ' ' . implode(' ', $this->args), array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, dirname($this->file), NULL, array('bypass_shell' => TRUE));
     list($stdin, $this->stdout, $stderr) = $pipes;
     fclose($stdin);
     if ($flags & self::RUN_COLLECT_ERRORS) {
         $this->stderr = $stderr;
     } else {
         fclose($stderr);
     }
     if ($flags & self::RUN_ASYNC) {
         stream_set_blocking($this->stdout, 0);
         // on Windows does not work with proc_open()
         if ($this->stderr) {
             stream_set_blocking($this->stderr, 0);
         }
     } else {
         while ($this->isRunning()) {
             usleep(self::RUN_USLEEP);
             // stream_select() doesn't work with proc_open()
         }
     }
 }
Beispiel #2
0
 /**
  * Runs single test.
  * @param  int RUN_ASYNC | RUN_COLLECT_ERRORS
  * @return void
  */
 public function run($flags = NULL)
 {
     foreach ($this->envVars as $name => $value) {
         putenv("{$name}={$value}");
     }
     $this->proc = proc_open($this->interpreter->getCommandLine() . ' -d register_argc_argv=on ' . Helpers::escapeArg($this->file) . ' ' . implode(' ', $this->args), [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes, dirname($this->file), NULL, ['bypass_shell' => TRUE]);
     foreach (array_keys($this->envVars) as $name) {
         putenv($name);
     }
     list($stdin, $this->stdout, $stderr) = $pipes;
     fclose($stdin);
     if ($flags & self::RUN_COLLECT_ERRORS) {
         $this->stderr = $stderr;
     } else {
         fclose($stderr);
     }
     if ($flags & self::RUN_ASYNC) {
         stream_set_blocking($this->stdout, 0);
         // on Windows does not work with proc_open()
         if ($this->stderr) {
             stream_set_blocking($this->stderr, 0);
         }
     } else {
         while ($this->isRunning()) {
             usleep(self::RUN_USLEEP);
             // stream_select() doesn't work with proc_open()
         }
     }
 }
 public function __construct($path, $args = NULL)
 {
     $this->path = Helpers::escapeArg($path);
     $proc = proc_open("{$this->path} -n {$args} -V", array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
     $output = stream_get_contents($pipes[1]);
     $this->error = trim(stream_get_contents($pipes[2]));
     if (proc_close($proc)) {
         throw new \Exception("Unable to run '{$path}': " . preg_replace('#[\\r\\n ]+#', ' ', $this->error));
     } elseif (!preg_match('#^PHP ([\\w.-]+)#im', $output, $matches)) {
         throw new \Exception("Unable to detect PHP version (output: {$output}).");
     } elseif (version_compare($matches[1], '7.0.0', '<')) {
         throw new \Exception('Unable to use phpdbg on PHP < 7.0.0.');
     }
     $this->version = $matches[1];
     $this->arguments = $args;
 }
 public function __construct($path, $args = NULL)
 {
     $this->path = \Tester\Helpers::escapeArg($path);
     $proc = @proc_open("{$this->path} -n {$args} -v", array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
     $output = stream_get_contents($pipes[1]);
     $error = stream_get_contents($pipes[2]);
     if (proc_close($proc)) {
         throw new \Exception("Unable to run '{$path}': " . preg_replace('#[\\r\\n ]+#', ' ', $error));
     } elseif (!preg_match('#^PHP (\\S+).*c(g|l)i#i', $output, $matches)) {
         throw new \Exception("Unable to detect PHP version (output: {$output}).");
     }
     $this->version = $matches[1];
     $this->cgi = strcasecmp($matches[2], 'g') === 0;
     $this->xdebug = strpos($output, 'Xdebug') > 0;
     $this->arguments = $args;
 }
Beispiel #5
0
 /**
  * Runs single test.
  * @param  bool  wait till process ends
  * @return void
  */
 public function run($blocking = TRUE)
 {
     putenv(Environment::RUNNER . '=1');
     putenv(Environment::COLORS . '=' . (int) Environment::$useColors);
     $this->proc = proc_open($this->php->getCommandLine() . ' -n -d register_argc_argv=on ' . \Tester\Helpers::escapeArg($this->file) . ' ' . $this->args, array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, dirname($this->file), NULL, array('bypass_shell' => TRUE));
     list($stdin, $this->stdout, $stderr) = $pipes;
     fclose($stdin);
     fclose($stderr);
     if ($blocking) {
         while ($this->isRunning()) {
             usleep(self::RUN_USLEEP);
             // stream_select() doesn't work with proc_open()
         }
     } else {
         stream_set_blocking($this->stdout, 0);
     }
 }
Beispiel #6
0
 public function __construct($path, $args = NULL)
 {
     $this->path = Helpers::escapeArg($path);
     $proc = @proc_open("{$this->path} --php {$args} -r " . Helpers::escapeArg('echo HHVM_VERSION . "|" . PHP_VERSION;'), array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
     $output = stream_get_contents($pipes[1]);
     $error = stream_get_contents($pipes[2]);
     if (proc_close($proc)) {
         throw new \Exception("Unable to run '{$path}': " . preg_replace('#[\\r\\n ]+#', ' ', $error));
     } elseif (count($tmp = explode('|', $output)) !== 2) {
         throw new \Exception("Unable to detect HHVM version (output: {$output}).");
     }
     list($this->version, $this->phpVersion) = $tmp;
     if (version_compare($this->version, '3.3.0', '<')) {
         throw new \Exception('HHVM below version 3.3.0 is not supported.');
     }
     $this->arguments = '--php -d hhvm.log.always_log_unhandled_exceptions=false ' . ($args ? " {$args}" : '');
     // HHVM issue #3019
 }
 public function __construct($path, $args = NULL)
 {
     $this->path = Helpers::escapeArg($path);
     $proc = proc_open("{$this->path} -n {$args} -v", array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
     $output = stream_get_contents($pipes[1]);
     $this->error = trim(stream_get_contents($pipes[2]));
     if (proc_close($proc)) {
         throw new \Exception("Unable to run '{$path}': " . preg_replace('#[\\r\\n ]+#', ' ', $this->error));
     } elseif (!preg_match('#^PHP (\\S+).*c(g|l)i#i', $output, $matches)) {
         throw new \Exception("Unable to detect PHP version (output: {$output}).");
     }
     $this->version = $matches[1];
     $this->cgi = strcasecmp($matches[2], 'g') === 0;
     $this->arguments = $args;
     $job = new Job(__DIR__ . '/info.php', $this, array('xdebug'));
     $job->run();
     $this->xdebug = !$job->getExitCode();
 }
Beispiel #8
0
 /** @return void */
 private function createPhpInterpreter()
 {
     $args = '';
     if ($this->options['-c']) {
         $args .= ' -c ' . Helpers::escapeArg($this->options['-c']);
     } elseif (!$this->options['--info']) {
         echo "Note: No php.ini is used.\n";
     }
     foreach ($this->options['-d'] as $item) {
         $args .= ' -d ' . Helpers::escapeArg($item);
     }
     // Is the executable Zend PHP or HHVM?
     $proc = @proc_open($this->options['-p'] . ' --version', array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
     $output = stream_get_contents($pipes[1]);
     $error = stream_get_contents($pipes[2]);
     if (proc_close($proc)) {
         throw new \Exception("Unable to run '{$this->options['-p']}': " . preg_replace('#[\\r\\n ]+#', ' ', $error));
     }
     if (preg_match('#HipHop VM#', $output)) {
         $this->interpreter = new HhvmPhpInterpreter($this->options['-p'], $args);
     } else {
         $this->interpreter = new ZendPhpInterpreter($this->options['-p'], $args);
     }
 }
Beispiel #9
0
 private function initiateTestCase($foo, PhpExecutable $php, $file)
 {
     $job = new Job($file, $php, Helpers::escapeArg(Tester\TestCase::LIST_METHODS));
     $job->run();
     if (in_array($job->getExitCode(), array(Job::CODE_ERROR, Job::CODE_FAIL, Job::CODE_SKIP))) {
         return array($job->getExitCode() === Job::CODE_SKIP ? Runner::SKIPPED : Runner::FAILED, $job->getOutput());
     }
     $methods = json_decode(strrchr($job->getOutput(), '['));
     if (!is_array($methods)) {
         return array(Runner::FAILED, "Cannot list TestCase methods in file '{$file}'. Do you call TestCase::run() in it?");
     } elseif (!$methods) {
         return array(Runner::SKIPPED, "TestCase in file '{$file}' does not contain test methods.");
     }
     foreach ($methods as $method) {
         $this->runner->addJob(new Job($file, $php, Helpers::escapeArg($method)));
     }
     return TRUE;
 }
Beispiel #10
0
 /** @return void */
 private function createPhpInterpreter()
 {
     $args = '';
     if ($this->options['-c']) {
         $args .= ' -c ' . Helpers::escapeArg($this->options['-c']);
     } elseif (!$this->options['--info']) {
         echo "Note: No php.ini is used.\n";
     }
     if (in_array($this->options['-o'], array('tap', 'junit'))) {
         $args .= ' -d html_errors=off';
     }
     foreach ($this->options['-d'] as $item) {
         $args .= ' -d ' . Helpers::escapeArg($item);
     }
     // Is the executable Zend PHP or HHVM?
     $proc = @proc_open($this->options['-p'] . ' --version', array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
     if ($proc === FALSE) {
         throw new \Exception('Cannot run PHP interpreter ' . $this->options['-p'] . '. Use -p option.');
     }
     $output = stream_get_contents($pipes[1]);
     $error = stream_get_contents($pipes[2]);
     if (proc_close($proc)) {
         throw new \Exception("Unable to run '{$this->options['-p']}': " . preg_replace('#[\\r\\n ]+#', ' ', $error));
     }
     if (preg_match('#HipHop VM#', $output)) {
         $this->interpreter = new HhvmPhpInterpreter($this->options['-p'], $args);
     } else {
         $this->interpreter = new ZendPhpInterpreter($this->options['-p'], $args);
     }
     if ($this->interpreter->getErrorOutput()) {
         echo Dumper::color('red', 'PHP startup error: ' . $this->interpreter->getErrorOutput()) . "\n";
         if ($this->interpreter->isCgi()) {
             echo "(note that PHP CLI generates better error messages)\n";
         }
     }
 }
Beispiel #11
0
 private function initiateTestCase($foo, PhpInterpreter $interpreter, $file)
 {
     $job = new Job($file, $interpreter, array(Helpers::escapeArg('--method=' . Tester\TestCase::LIST_METHODS)));
     $job->run();
     if (in_array($job->getExitCode(), array(Job::CODE_ERROR, Job::CODE_FAIL, Job::CODE_SKIP), TRUE)) {
         return array($job->getExitCode() === Job::CODE_SKIP ? Runner::SKIPPED : Runner::FAILED, $job->getOutput());
     }
     if (!preg_match('#\\[([^[]*)]#', strrchr($job->getOutput(), '['), $m)) {
         return array(Runner::FAILED, "Cannot list TestCase methods in file '{$file}'. Do you call TestCase::run() in it?");
     } elseif (!strlen($m[1])) {
         return array(Runner::SKIPPED, "TestCase in file '{$file}' does not contain test methods.");
     }
     return array('method', explode(',', $m[1]));
 }
Beispiel #12
0
use Bitbang\Http\Tests;
use Tester\Helpers;
require __DIR__ . '/server/BackgroundProcess.php';
echo "\n";
if (defined('HHVM_VERSION')) {
    echo "# HTTP server cannot start under HHVM, run Tester by Zend PHP.\n";
} else {
    @mkdir(__DIR__ . '/temp');
    # @ = directory may already exist
    $config = parse_ini_file(__DIR__ . '/server.ini', TRUE)['listen'];
    echo "# Starting HTTP server for tests on {$config['address']}:{$config['port']}... ";
    $server = new Tests\BackgroundProcess();
    $server->start(Helpers::escapeArg(PHP_BINARY) . " -S {$config['address']}:{$config['port']} -d always_populate_raw_post_data=-1" . Helpers::escapeArg(__DIR__ . '/server/index.php'), __DIR__ . '/temp/http.log', __DIR__ . '/temp/http.log');
    echo "done\n";
    putenv("TESTS_HTTP_LISTEN={$config['address']}:{$config['port']}");
    echo "# Starting SSL wrapper for tests on {$config['address']}:{$config['port_ssl']}... ";
    $wrapper = new Tests\BackgroundProcess();
    $wrapper->start(Helpers::escapeArg(PHP_BINARY) . ' ' . Helpers::escapeArg(__DIR__ . '/server/ssl-wrapper.php'), __DIR__ . '/temp/ssl-wrapper.log', __DIR__ . '/temp/ssl-wrapper.log');
    echo "done\n";
    putenv("TESTS_HTTPS_LISTEN={$config['address']}:{$config['port_ssl']}");
    register_shutdown_function(function () use($server, $wrapper) {
        echo "\n";
        echo '# Shutting down SSL wrapper... ';
        $wrapper->terminate();
        echo "done\n";
        echo '# Shutting down HTTP server... ';
        $server->terminate();
        echo "done\n";
    });
}
echo "\n";
Beispiel #13
0
 /**
  * @param  string
  * @param  string
  */
 public function addPhpIniOption($name, $value = NULL)
 {
     $this->commandLine .= ' -d ' . Helpers::escapeArg($name . ($value === NULL ? '' : "={$value}"));
 }