<?php
// ADAPTED FROM tools/scripts/phpunit
-$GLOBALS['base_dir'] = dirname(dirname(dirname(__DIR__)));
-$tests_dir = $GLOBALS['base_dir'] . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'phpunit';
-$civi_pkgs_dir = $GLOBALS['base_dir'] . DIRECTORY_SEPARATOR . 'packages';
ini_set('safe_mode', 0);
-ini_set('include_path',
- "{$GLOBALS['base_dir']}" . PATH_SEPARATOR .
- "$tests_dir" . PATH_SEPARATOR .
- "$civi_pkgs_dir" . PATH_SEPARATOR
- . ini_get('include_path'));
+ini_set('include_path', dirname(__DIR__) . PATH_SEPARATOR . ini_get('include_path'));
# Relying on system timezone setting produces a warning,
# doing the following prevents the warning message
# Crank up the memory
ini_set('memory_limit', '2G');
+define('CIVICRM_TEST', 1);
+eval(cv('php:boot --level=settings', 'phpcode'));
-require_once $GLOBALS['base_dir'] . '/vendor/autoload.php';
+if (CIVICRM_UF === 'UnitTests') {
+ Civi\Test::headless()->apply();
+}
+
+// ------------------------------------------------------------------------------
-/*
-require $GLOBALS['base_dir'] . DIRECTORY_SEPARATOR .
-'packages' . DIRECTORY_SEPARATOR .
-'PHPUnit' . DIRECTORY_SEPARATOR .
-'Autoload.php';
+/**
+ * Call the "cv" command.
+ *
+ * @param string $cmd
+ * The rest of the command to send.
+ * @param string $decode
+ * Ex: 'json' or 'phpcode'.
+ * @return string
+ * Response output (if the command executed normally).
+ * @throws \RuntimeException
+ * If the command terminates abnormally.
*/
+function cv($cmd, $decode = 'json') {
+ $cmd = 'cv ' . $cmd;
+ $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
+ $oldOutput = getenv('CV_OUTPUT');
+ putenv("CV_OUTPUT=json");
+ $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
+ putenv("CV_OUTPUT=$oldOutput");
+ fclose($pipes[0]);
+ $result = stream_get_contents($pipes[1]);
+ fclose($pipes[1]);
+ if (proc_close($process) !== 0) {
+ throw new RuntimeException("Command failed ($cmd):\n$result");
+ }
+ switch ($decode) {
+ case 'raw':
+ return $result;
+
+ case 'phpcode':
+ // If the last output is /*PHPCODE*/, then we managed to complete execution.
+ if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
+ throw new \RuntimeException("Command failed ($cmd):\n$result");
+ }
+ return $result;
+
+ case 'json':
+ return json_decode($result, 1);
+
+ default:
+ throw new RuntimeException("Bad decoder format ($decode)");
+ }
+}