Merge pull request #14499 from mattwire/extract_getdefaultroleid
[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);
7aca9a14 19// phpcs:disable
f6e5533a 20eval(cv('php:boot --level=settings', 'phpcode'));
7aca9a14 21// phpcs:enable
534e937c 22
bc560b0a 23if (CIVICRM_UF === 'UnitTests') {
728bbd5b 24 Civi\Test::headless()->apply();
bc560b0a
TO
25}
26
534e937c
TO
27// ------------------------------------------------------------------------------
28
29/**
30 * Call the "cv" command.
31 *
32 * @param string $cmd
33 * The rest of the command to send.
f6e5533a
TO
34 * @param string $decode
35 * Ex: 'json' or 'phpcode'.
534e937c
TO
36 * @return string
37 * Response output (if the command executed normally).
38 * @throws \RuntimeException
39 * If the command terminates abnormally.
40 */
f6e5533a 41function cv($cmd, $decode = 'json') {
534e937c
TO
42 $cmd = 'cv ' . $cmd;
43 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
2f928b89
TO
44 $oldOutput = getenv('CV_OUTPUT');
45 putenv("CV_OUTPUT=json");
46 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
47 putenv("CV_OUTPUT=$oldOutput");
534e937c 48 fclose($pipes[0]);
f6e5533a 49 $result = stream_get_contents($pipes[1]);
534e937c
TO
50 fclose($pipes[1]);
51 if (proc_close($process) !== 0) {
f6e5533a
TO
52 throw new RuntimeException("Command failed ($cmd):\n$result");
53 }
54 switch ($decode) {
55 case 'raw':
56 return $result;
57
58 case 'phpcode':
59 // If the last output is /*PHPCODE*/, then we managed to complete execution.
60 if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
61 throw new \RuntimeException("Command failed ($cmd):\n$result");
62 }
63 return $result;
64
65 case 'json':
66 return json_decode($result, 1);
67
68 default:
69 throw new RuntimeException("Bad decoder format ($decode)");
534e937c 70 }
534e937c 71}