From f6e5533a167a78cb4fc8b70a2a44fe02eab9a3e7 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 22 Jan 2016 15:08:09 -0800 Subject: [PATCH] CRM-17860 - CiviTest cv() - Guard better against bootstrap failures --- tests/phpunit/CiviTest/bootstrap.php | 30 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/CiviTest/bootstrap.php b/tests/phpunit/CiviTest/bootstrap.php index 7bf9877551..2dd6a2c442 100644 --- a/tests/phpunit/CiviTest/bootstrap.php +++ b/tests/phpunit/CiviTest/bootstrap.php @@ -16,7 +16,7 @@ if (file_exists('/etc/timezone')) { # Crank up the memory ini_set('memory_limit', '2G'); define('CIVICRM_TEST', 1); -eval(cv('php:boot', 1)); +eval(cv('php:boot --level=settings', 'phpcode')); // This is exists to support CiviUnitTestCase::populateDB(). That doesn't make it a good idea. require_once "DB.php"; @@ -34,23 +34,39 @@ $GLOBALS['mysql_db'] = $dsninfo['database']; * * @param string $cmd * The rest of the command to send. - * @param bool $raw - * If TRUE, return the raw output. If FALSE, parse JSON output. + * @param string $decode + * Ex: 'json' or 'phpcode'. * @return string * Response output (if the command executed normally). * @throws \RuntimeException * If the command terminates abnormally. */ -function cv($cmd, $raw = FALSE) { +function cv($cmd, $decode = 'json') { $cmd = 'cv ' . $cmd; $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR); $env = $_ENV + array('CV_OUTPUT' => 'json'); $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__, $env); fclose($pipes[0]); - $bootCode = stream_get_contents($pipes[1]); + $result = stream_get_contents($pipes[1]); fclose($pipes[1]); if (proc_close($process) !== 0) { - throw new RuntimeException("Command failed ($cmd)"); + throw new RuntimeException("Command failed ($cmd):\n$result"); + } + switch ($decode) { + case 'raw': + return $result; + + case 'phpcode': + // If the last output is /*PHPCODE*/, then we managed to complete execution. + if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { + throw new \RuntimeException("Command failed ($cmd):\n$result"); + } + return $result; + + case 'json': + return json_decode($result, 1); + + default: + throw new RuntimeException("Bad decoder format ($decode)"); } - return $raw ? $bootCode : json_decode($bootCode, 1); } -- 2.25.1