| 1 | <?php |
| 2 | // ADAPTED FROM tools/scripts/phpunit |
| 3 | |
| 4 | ini_set('safe_mode', 0); |
| 5 | ini_set('include_path', dirname(__DIR__) . PATH_SEPARATOR . ini_get('include_path')); |
| 6 | |
| 7 | # Relying on system timezone setting produces a warning, |
| 8 | # doing the following prevents the warning message |
| 9 | if (file_exists('/etc/timezone')) { |
| 10 | $timezone = trim(file_get_contents('/etc/timezone')); |
| 11 | if (ini_set('date.timezone', $timezone) === FALSE) { |
| 12 | echo "ini_set( 'date.timezone', '$timezone' ) failed\n"; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | # Crank up the memory |
| 17 | ini_set('memory_limit', '2G'); |
| 18 | define('CIVICRM_TEST', 1); |
| 19 | eval(cv('php:boot --level=settings', 'phpcode')); |
| 20 | |
| 21 | if (CIVICRM_UF === 'UnitTests') { |
| 22 | Civi\Test::headless()->apply(); |
| 23 | } |
| 24 | |
| 25 | // ------------------------------------------------------------------------------ |
| 26 | |
| 27 | /** |
| 28 | * Call the "cv" command. |
| 29 | * |
| 30 | * @param string $cmd |
| 31 | * The rest of the command to send. |
| 32 | * @param string $decode |
| 33 | * Ex: 'json' or 'phpcode'. |
| 34 | * @return string |
| 35 | * Response output (if the command executed normally). |
| 36 | * @throws \RuntimeException |
| 37 | * If the command terminates abnormally. |
| 38 | */ |
| 39 | function cv($cmd, $decode = 'json') { |
| 40 | $cmd = 'cv ' . $cmd; |
| 41 | $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR); |
| 42 | $oldOutput = getenv('CV_OUTPUT'); |
| 43 | putenv("CV_OUTPUT=json"); |
| 44 | $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); |
| 45 | putenv("CV_OUTPUT=$oldOutput"); |
| 46 | fclose($pipes[0]); |
| 47 | $result = stream_get_contents($pipes[1]); |
| 48 | fclose($pipes[1]); |
| 49 | if (proc_close($process) !== 0) { |
| 50 | throw new RuntimeException("Command failed ($cmd):\n$result"); |
| 51 | } |
| 52 | switch ($decode) { |
| 53 | case 'raw': |
| 54 | return $result; |
| 55 | |
| 56 | case 'phpcode': |
| 57 | // If the last output is /*PHPCODE*/, then we managed to complete execution. |
| 58 | if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { |
| 59 | throw new \RuntimeException("Command failed ($cmd):\n$result"); |
| 60 | } |
| 61 | return $result; |
| 62 | |
| 63 | case 'json': |
| 64 | return json_decode($result, 1); |
| 65 | |
| 66 | default: |
| 67 | throw new RuntimeException("Bad decoder format ($decode)"); |
| 68 | } |
| 69 | } |