CRM-17860 - CiviTest bootstrap - Move test define()s from `cv` to `civicrm.settings...
[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
TO
18define('CIVICRM_TEST', 1);
19eval(cv('php:boot', 1));
534e937c
TO
20
21// This is exists to support CiviUnitTestCase::populateDB(). That doesn't make it a good idea.
22require_once "DB.php";
23$dsninfo = DB::parseDSN(CIVICRM_DSN);
24$GLOBALS['mysql_host'] = $dsninfo['hostspec'];
25$GLOBALS['mysql_port'] = @$dsninfo['port'];
26$GLOBALS['mysql_user'] = $dsninfo['username'];
27$GLOBALS['mysql_pass'] = $dsninfo['password'];
28$GLOBALS['mysql_db'] = $dsninfo['database'];
29
30// ------------------------------------------------------------------------------
31
32/**
33 * Call the "cv" command.
34 *
35 * @param string $cmd
36 * The rest of the command to send.
37 * @param bool $raw
38 * If TRUE, return the raw output. If FALSE, parse JSON output.
39 * @return string
40 * Response output (if the command executed normally).
41 * @throws \RuntimeException
42 * If the command terminates abnormally.
43 */
44function cv($cmd, $raw = FALSE) {
45 $cmd = 'cv ' . $cmd;
46 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
47 $env = $_ENV + array('CV_OUTPUT' => 'json');
48 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__, $env);
49 fclose($pipes[0]);
50 $bootCode = stream_get_contents($pipes[1]);
51 fclose($pipes[1]);
52 if (proc_close($process) !== 0) {
53 throw new RuntimeException("Command failed ($cmd)");
54 }
55 return $raw ? $bootCode : json_decode($bootCode, 1);
56}