CiviTest - Eliminate global mysql variables. Simplify bootstrap.php.
[civicrm-core.git] / tests / phpunit / CiviTest / bootstrap.php
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 // ------------------------------------------------------------------------------
22
23 /**
24 * Call the "cv" command.
25 *
26 * @param string $cmd
27 * The rest of the command to send.
28 * @param string $decode
29 * Ex: 'json' or 'phpcode'.
30 * @return string
31 * Response output (if the command executed normally).
32 * @throws \RuntimeException
33 * If the command terminates abnormally.
34 */
35 function cv($cmd, $decode = 'json') {
36 $cmd = 'cv ' . $cmd;
37 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
38 $env = $_ENV + array('CV_OUTPUT' => 'json');
39 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__, $env);
40 fclose($pipes[0]);
41 $result = stream_get_contents($pipes[1]);
42 fclose($pipes[1]);
43 if (proc_close($process) !== 0) {
44 throw new RuntimeException("Command failed ($cmd):\n$result");
45 }
46 switch ($decode) {
47 case 'raw':
48 return $result;
49
50 case 'phpcode':
51 // If the last output is /*PHPCODE*/, then we managed to complete execution.
52 if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
53 throw new \RuntimeException("Command failed ($cmd):\n$result");
54 }
55 return $result;
56
57 case 'json':
58 return json_decode($result, 1);
59
60 default:
61 throw new RuntimeException("Bad decoder format ($decode)");
62 }
63 }