Merge pull request #19985 from civicrm/5.36
[civicrm-core.git] / tests / phpunit / CiviTest / bootstrap.php
CommitLineData
781cfdd0
TO
1<?php
2// ADAPTED FROM tools/scripts/phpunit
3
534e937c 4ini_set('include_path', dirname(__DIR__) . PATH_SEPARATOR . ini_get('include_path'));
781cfdd0
TO
5
6# Relying on system timezone setting produces a warning,
7# doing the following prevents the warning message
481a74f4
TO
8if (file_exists('/etc/timezone')) {
9 $timezone = trim(file_get_contents('/etc/timezone'));
10 if (ini_set('date.timezone', $timezone) === FALSE) {
6c6e6187
TO
11 echo "ini_set( 'date.timezone', '$timezone' ) failed\n";
12 }
781cfdd0
TO
13}
14
15# Crank up the memory
16ini_set('memory_limit', '2G');
3b2c20b7 17define('CIVICRM_TEST', 1);
7aca9a14 18// phpcs:disable
f6e5533a 19eval(cv('php:boot --level=settings', 'phpcode'));
7aca9a14 20// phpcs:enable
534e937c 21
bc560b0a 22if (CIVICRM_UF === 'UnitTests') {
728bbd5b 23 Civi\Test::headless()->apply();
bc560b0a
TO
24}
25
0e0f7410
C
26spl_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
534e937c
TO
31// ------------------------------------------------------------------------------
32
0e0f7410
C
33/**
34 * @param $prefix
35 * @param $base_dir
36 * @param $class
37 */
38function _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
534e937c
TO
52/**
53 * Call the "cv" command.
54 *
55 * @param string $cmd
56 * The rest of the command to send.
f6e5533a
TO
57 * @param string $decode
58 * Ex: 'json' or 'phpcode'.
534e937c
TO
59 * @return string
60 * Response output (if the command executed normally).
61 * @throws \RuntimeException
62 * If the command terminates abnormally.
63 */
f6e5533a 64function cv($cmd, $decode = 'json') {
534e937c
TO
65 $cmd = 'cv ' . $cmd;
66 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
2f928b89
TO
67 $oldOutput = getenv('CV_OUTPUT');
68 putenv("CV_OUTPUT=json");
69 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
70 putenv("CV_OUTPUT=$oldOutput");
534e937c 71 fclose($pipes[0]);
f6e5533a 72 $result = stream_get_contents($pipes[1]);
534e937c
TO
73 fclose($pipes[1]);
74 if (proc_close($process) !== 0) {
f6e5533a
TO
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)");
534e937c 93 }
534e937c 94}