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