Merge pull request #19057 from ixiam/dev/core#2173
[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
0e0f7410
C
27spl_autoload_register(function($class) {
28 _phpunit_mockoloader('api\\v4\\', "tests/phpunit/api/v4/", $class);
29 _phpunit_mockoloader('Civi\\Api4\\', "tests/phpunit/api/v4/Mock/Api4/", $class);
30});
31
534e937c
TO
32// ------------------------------------------------------------------------------
33
0e0f7410
C
34/**
35 * @param $prefix
36 * @param $base_dir
37 * @param $class
38 */
39function _phpunit_mockoloader($prefix, $base_dir, $class) {
40 $len = strlen($prefix);
41 if (strncmp($prefix, $class, $len) !== 0) {
42 return;
43 }
44
45 global $civicrm_root;
46 $relative_class = substr($class, $len);
47 $file = $civicrm_root . '/' . $base_dir . str_replace('\\', '/', $relative_class) . '.php';
48 if (file_exists($file)) {
49 require $file;
50 }
51}
52
534e937c
TO
53/**
54 * Call the "cv" command.
55 *
56 * @param string $cmd
57 * The rest of the command to send.
f6e5533a
TO
58 * @param string $decode
59 * Ex: 'json' or 'phpcode'.
534e937c
TO
60 * @return string
61 * Response output (if the command executed normally).
62 * @throws \RuntimeException
63 * If the command terminates abnormally.
64 */
f6e5533a 65function cv($cmd, $decode = 'json') {
534e937c
TO
66 $cmd = 'cv ' . $cmd;
67 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
2f928b89
TO
68 $oldOutput = getenv('CV_OUTPUT');
69 putenv("CV_OUTPUT=json");
70 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
71 putenv("CV_OUTPUT=$oldOutput");
534e937c 72 fclose($pipes[0]);
f6e5533a 73 $result = stream_get_contents($pipes[1]);
534e937c
TO
74 fclose($pipes[1]);
75 if (proc_close($process) !== 0) {
f6e5533a
TO
76 throw new RuntimeException("Command failed ($cmd):\n$result");
77 }
78 switch ($decode) {
79 case 'raw':
80 return $result;
81
82 case 'phpcode':
83 // If the last output is /*PHPCODE*/, then we managed to complete execution.
84 if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
85 throw new \RuntimeException("Command failed ($cmd):\n$result");
86 }
87 return $result;
88
89 case 'json':
90 return json_decode($result, 1);
91
92 default:
93 throw new RuntimeException("Bad decoder format ($decode)");
534e937c 94 }
534e937c 95}