Merge pull request #19737 from sunilpawar/show_inactive_active_case_role
[civicrm-core.git] / tests / phpunit / CiviTest / bootstrap.php
1 <?php
2 // ADAPTED FROM tools/scripts/phpunit
3
4 ini_set('include_path', dirname(__DIR__) . PATH_SEPARATOR . ini_get('include_path'));
5
6 # Relying on system timezone setting produces a warning,
7 # doing the following prevents the warning message
8 if (file_exists('/etc/timezone')) {
9 $timezone = trim(file_get_contents('/etc/timezone'));
10 if (ini_set('date.timezone', $timezone) === FALSE) {
11 echo "ini_set( 'date.timezone', '$timezone' ) failed\n";
12 }
13 }
14
15 # Crank up the memory
16 ini_set('memory_limit', '2G');
17 define('CIVICRM_TEST', 1);
18 // phpcs:disable
19 eval(cv('php:boot --level=settings', 'phpcode'));
20 // phpcs:enable
21
22 if (CIVICRM_UF === 'UnitTests') {
23 Civi\Test::headless()->apply();
24 }
25
26 spl_autoload_register(function($class) {
27 _phpunit_mockoloader('api\\v4\\', "tests/phpunit/api/v4/", $class);
28 _phpunit_mockoloader('Civi\\Api4\\', "tests/phpunit/api/v4/Mock/Api4/", $class);
29 });
30
31 // ------------------------------------------------------------------------------
32
33 /**
34 * @param $prefix
35 * @param $base_dir
36 * @param $class
37 */
38 function _phpunit_mockoloader($prefix, $base_dir, $class) {
39 $len = strlen($prefix);
40 if (strncmp($prefix, $class, $len) !== 0) {
41 return;
42 }
43
44 global $civicrm_root;
45 $relative_class = substr($class, $len);
46 $file = $civicrm_root . '/' . $base_dir . str_replace('\\', '/', $relative_class) . '.php';
47 if (file_exists($file)) {
48 require $file;
49 }
50 }
51
52 /**
53 * Call the "cv" command.
54 *
55 * @param string $cmd
56 * The rest of the command to send.
57 * @param string $decode
58 * Ex: 'json' or 'phpcode'.
59 * @return string
60 * Response output (if the command executed normally).
61 * @throws \RuntimeException
62 * If the command terminates abnormally.
63 */
64 function cv($cmd, $decode = 'json') {
65 $cmd = 'cv ' . $cmd;
66 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
67 $oldOutput = getenv('CV_OUTPUT');
68 putenv("CV_OUTPUT=json");
69 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
70 putenv("CV_OUTPUT=$oldOutput");
71 fclose($pipes[0]);
72 $result = stream_get_contents($pipes[1]);
73 fclose($pipes[1]);
74 if (proc_close($process) !== 0) {
75 throw new RuntimeException("Command failed ($cmd):\n$result");
76 }
77 switch ($decode) {
78 case 'raw':
79 return $result;
80
81 case 'phpcode':
82 // If the last output is /*PHPCODE*/, then we managed to complete execution.
83 if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
84 throw new \RuntimeException("Command failed ($cmd):\n$result");
85 }
86 return $result;
87
88 case 'json':
89 return json_decode($result, 1);
90
91 default:
92 throw new RuntimeException("Bad decoder format ($decode)");
93 }
94 }