2 // ADAPTED FROM tools/scripts/phpunit
4 ini_set('include_path', dirname(__DIR__
) . PATH_SEPARATOR
. ini_get('include_path'));
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";
16 ini_set('memory_limit', '2G');
17 define('CIVICRM_TEST', 1);
19 eval(cv('php:boot --level=settings', 'phpcode'));
22 if (CIVICRM_UF
=== 'UnitTests') {
23 Civi\Test
::headless()->apply();
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);
31 // ------------------------------------------------------------------------------
38 function _phpunit_mockoloader($prefix, $base_dir, $class) {
39 $len = strlen($prefix);
40 if (strncmp($prefix, $class, $len) !== 0) {
45 $relative_class = substr($class, $len);
46 $file = $civicrm_root . '/' . $base_dir . str_replace('\\', '/', $relative_class) . '.php';
47 if (file_exists($file)) {
53 * Call the "cv" command.
56 * The rest of the command to send.
57 * @param string $decode
58 * Ex: 'json' or 'phpcode'.
60 * Response output (if the command executed normally).
61 * @throws \RuntimeException
62 * If the command terminates abnormally.
64 function cv($cmd, $decode = 'json') {
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");
72 $result = stream_get_contents($pipes[1]);
74 if (proc_close($process) !== 0) {
75 throw new RuntimeException("Command failed ($cmd):\n$result");
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");
89 return json_decode($result, 1);
92 throw new RuntimeException("Bad decoder format ($decode)");