2 // ADAPTED FROM tools/scripts/phpunit
4 ini_set('safe_mode', 0);
5 ini_set('include_path', dirname(__DIR__
) . PATH_SEPARATOR
. ini_get('include_path'));
7 # Relying on system timezone setting produces a warning,
8 # doing the following prevents the warning message
9 if (file_exists('/etc/timezone')) {
10 $timezone = trim(file_get_contents('/etc/timezone'));
11 if (ini_set('date.timezone', $timezone) === FALSE) {
12 echo "ini_set( 'date.timezone', '$timezone' ) failed\n";
17 ini_set('memory_limit', '2G');
18 define('CIVICRM_TEST', 1);
20 eval(cv('php:boot --level=settings', 'phpcode'));
23 if (CIVICRM_UF
=== 'UnitTests') {
24 Civi\Test
::headless()->apply();
27 spl_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);
32 // ------------------------------------------------------------------------------
39 function _phpunit_mockoloader($prefix, $base_dir, $class) {
40 $len = strlen($prefix);
41 if (strncmp($prefix, $class, $len) !== 0) {
46 $relative_class = substr($class, $len);
47 $file = $civicrm_root . '/' . $base_dir . str_replace('\\', '/', $relative_class) . '.php';
48 if (file_exists($file)) {
54 * Call the "cv" command.
57 * The rest of the command to send.
58 * @param string $decode
59 * Ex: 'json' or 'phpcode'.
61 * Response output (if the command executed normally).
62 * @throws \RuntimeException
63 * If the command terminates abnormally.
65 function cv($cmd, $decode = 'json') {
67 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR
);
68 $oldOutput = getenv('CV_OUTPUT');
69 putenv("CV_OUTPUT=json");
70 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__
);
71 putenv("CV_OUTPUT=$oldOutput");
73 $result = stream_get_contents($pipes[1]);
75 if (proc_close($process) !== 0) {
76 throw new RuntimeException("Command failed ($cmd):\n$result");
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");
90 return json_decode($result, 1);
93 throw new RuntimeException("Bad decoder format ($decode)");