CRM-17860 - CiviTest - Use unified civicrm.settings.php
authorTim Otten <totten@civicrm.org>
Fri, 22 Jan 2016 11:24:44 +0000 (03:24 -0800)
committerTim Otten <totten@civicrm.org>
Tue, 2 Feb 2016 04:56:23 +0000 (21:56 -0700)
The wiring to load civicrm.settings.php is all over the place.  With this
patch, most use-cases (runtime, headless test, web test) will go through one
`civicrm.settings.php`.

This requires the `cv` command.

tests/phpunit/CiviTest/CiviSeleniumTestCase.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/CiviTest/bootstrap.php
tests/phpunit/WebTest/Utils/RedirectTest.php

index 2a3019038962fd4b1765ca2687c9d490260732e2..a4d69e577555be1d5fcbcf0a89be2d235b21c541 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-/**
- *  Include configuration
- */
-define('CIVICRM_SETTINGS_PATH', __DIR__ . '/civicrm.settings.dist.php');
-define('CIVICRM_SETTINGS_LOCAL_PATH', __DIR__ . '/civicrm.settings.local.php');
 define('CIVICRM_WEBTEST', 1);
 
-if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH)) {
-  require_once CIVICRM_SETTINGS_LOCAL_PATH;
-}
-require_once CIVICRM_SETTINGS_PATH;
-
 /**
  *  Base class for CiviCRM Selenium tests
  *
index 958a2acb1e7add188b1d8ce3cc24c6258559c928..c952af0dfb4cbb4949a1a9d464170834720b3a2d 100755 (executable)
 
 use Civi\Payment\System;
 
-/**
- *  Include configuration
- */
-define('CIVICRM_SETTINGS_PATH', __DIR__ . '/civicrm.settings.dist.php');
-define('CIVICRM_SETTINGS_LOCAL_PATH', __DIR__ . '/civicrm.settings.local.php');
-
-if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH)) {
-  require_once CIVICRM_SETTINGS_LOCAL_PATH;
-}
-require_once CIVICRM_SETTINGS_PATH;
 /**
  *  Include class definitions
  */
index e688e6d26caf724fff9ac6c09d229c6cd1ed3e11..b87d9e2522dbfe646fd17a1dffa5a6f2408dcc53 100644 (file)
@@ -1,15 +1,8 @@
 <?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
@@ -23,15 +16,46 @@ if (file_exists('/etc/timezone')) {
 # Crank up the memory
 ini_set('memory_limit', '2G');
 
-require_once $GLOBALS['base_dir'] . '/vendor/autoload.php';
-
-/*
-require $GLOBALS['base_dir'] . DIRECTORY_SEPARATOR .
-'packages' . DIRECTORY_SEPARATOR .
-'PHPUnit' . DIRECTORY_SEPARATOR .
-'Autoload.php';
- */
-
+// TODO Consider moving into main civicrm.settings.php so that `cv('api')` works better.
 if (!defined('CIVICRM_UF') && getenv('CIVICRM_UF')) {
   define('CIVICRM_UF', getenv('CIVICRM_UF'));
-}
\ No newline at end of file
+}
+
+eval(cv('php:boot --test', 1));
+
+// This is exists to support CiviUnitTestCase::populateDB(). That doesn't make it a good idea.
+require_once "DB.php";
+$dsninfo = DB::parseDSN(CIVICRM_DSN);
+$GLOBALS['mysql_host'] = $dsninfo['hostspec'];
+$GLOBALS['mysql_port'] = @$dsninfo['port'];
+$GLOBALS['mysql_user'] = $dsninfo['username'];
+$GLOBALS['mysql_pass'] = $dsninfo['password'];
+$GLOBALS['mysql_db'] = $dsninfo['database'];
+
+// ------------------------------------------------------------------------------
+
+/**
+ * Call the "cv" command.
+ *
+ * @param string $cmd
+ *   The rest of the command to send.
+ * @param bool $raw
+ *   If TRUE, return the raw output. If FALSE, parse JSON output.
+ * @return string
+ *   Response output (if the command executed normally).
+ * @throws \RuntimeException
+ *   If the command terminates abnormally.
+ */
+function cv($cmd, $raw = FALSE) {
+  $cmd = 'cv ' . $cmd;
+  $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
+  $env = $_ENV + array('CV_OUTPUT' => 'json');
+  $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__, $env);
+  fclose($pipes[0]);
+  $bootCode = stream_get_contents($pipes[1]);
+  fclose($pipes[1]);
+  if (proc_close($process) !== 0) {
+    throw new RuntimeException("Command failed ($cmd)");
+  }
+  return $raw ? $bootCode : json_decode($bootCode, 1);
+}
index 39d5df94d0178adf0336da5de48476b2b74cfc0f..910d050bf2594cd50cd6b990383fe94ae59adb46 100644 (file)
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
  */
-
 require_once 'CiviTest/CiviUnitTestCase.php';
 require_once 'CiviTest/CiviSeleniumSettings.php';
 
-/**
- *  Include configuration
- */
-define('CIVICRM_SETTINGS_PATH', __DIR__ . '/civicrm.settings.dist.php');
-define('CIVICRM_SETTINGS_LOCAL_PATH', __DIR__ . '/civicrm.settings.local.php');
 define('CIVICRM_WEBTEST', 1);
 
-if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH)) {
-  require_once CIVICRM_SETTINGS_LOCAL_PATH;
-}
-require_once CIVICRM_SETTINGS_PATH;
-
-
 /**
  * Check that we handle redirects appropriately.
  */