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);
29 if (substr($class, 0, 13) === 'CRM_Fake_DAO_') {
31 eval('namespace { class ' . $class . ' extends \CRM_Core_DAO { public static function &fields() { $r = []; return $r; }}}');
36 // ------------------------------------------------------------------------------
43 function _phpunit_mockoloader($prefix, $base_dir, $class) {
44 $len = strlen($prefix);
45 if (strncmp($prefix, $class, $len) !== 0) {
50 $relative_class = substr($class, $len);
51 $file = $civicrm_root . '/' . $base_dir . str_replace('\\', '/', $relative_class) . '.php';
52 if (file_exists($file)) {
58 * Call the "cv" command.
61 * The rest of the command to send.
62 * @param string $decode
63 * Ex: 'json' or 'phpcode'.
65 * Response output (if the command executed normally).
66 * @throws \RuntimeException
67 * If the command terminates abnormally.
69 function cv($cmd, $decode = 'json') {
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");
77 $result = stream_get_contents($pipes[1]);
79 if (proc_close($process) !== 0) {
80 throw new RuntimeException("Command failed ($cmd):\n$result");
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");
94 return json_decode($result, 1);
97 throw new RuntimeException("Bad decoder format ($decode)");