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