civicrm.settings.php.template, ExternalBatch - Read unit test DSN from $_CV
authorTim Otten <totten@civicrm.org>
Fri, 29 Jan 2016 00:27:07 +0000 (16:27 -0800)
committerTim Otten <totten@civicrm.org>
Tue, 2 Feb 2016 04:56:23 +0000 (21:56 -0700)
In the previous formulation, a dev would need to perform more steps during setup, ie

 1. Edit main civicrm.settings.php to add test DSN
 2. Run `cv vars:fill` and edit ~/.cv.json

This patch makes the first step redundant.

This patch also makes it easier to swtich DB's for parallel test execution.

Civi/API/ExternalBatch.php
templates/CRM/common/civicrm.settings.php.template

index f153795021b5e0464fcdb31f6b62871f4afe4a25..1a2230077ac6e8b4b038bca0908144ebca336bfa 100644 (file)
@@ -28,7 +28,7 @@ class ExternalBatch {
 
   protected $settingsPath;
 
-  protected $env = array();
+  protected $env;
 
   /**
    * @var array
@@ -51,9 +51,7 @@ class ExternalBatch {
     $this->root = $civicrm_root;
     $this->settingsPath = defined('CIVICRM_SETTINGS_PATH') ? CIVICRM_SETTINGS_PATH : NULL;
     $this->defaultParams = $defaultParams;
-    $this->addEnv(array(
-      'CIVICRM_UF' => CIVICRM_UF,
-    ));
+    $this->env = $_ENV;
   }
 
   /**
@@ -182,23 +180,35 @@ class ExternalBatch {
   public function createProcess($apiCall) {
     $parts = array();
 
-    $executableFinder = new PhpExecutableFinder();
-    $php = $executableFinder->find();
-    if (!$php) {
-      throw new \CRM_Core_Exception("Failed to locate PHP interpreter.");
+    if (defined('CIVICRM_TEST') && CIVICRM_TEST) {
+      // When testing, civicrm.settings.php may rely on $_CV, which is only
+      // populated/propagated if we execute through `cv`.
+      $parts[] = 'cv api';
+      $parts[] = escapeshellarg($apiCall['entity'] . '.' . $apiCall['action']);
+      $parts[] = "--out=json-strict";
+      foreach ($apiCall['params'] as $key => $value) {
+        $parts[] = escapeshellarg("$key=$value");
+      }
     }
-    $parts[] = $php;
-
-    $parts[] = escapeshellarg($this->root . '/bin/cli.php');
-    $parts[] = escapeshellarg("-e=" . $apiCall['entity']);
-    $parts[] = escapeshellarg("-a=" . $apiCall['action']);
-    $parts[] = "--json";
-    $parts[] = escapeshellarg("-u=dummyuser");
-    foreach ($apiCall['params'] as $key => $value) {
-      $parts[] = escapeshellarg("--$key=$value");
+    else {
+      // But in production, we may not have `cv` installed.
+      $executableFinder = new PhpExecutableFinder();
+      $php = $executableFinder->find();
+      if (!$php) {
+        throw new \CRM_Core_Exception("Failed to locate PHP interpreter.");
+      }
+      $parts[] = $php;
+      $parts[] = escapeshellarg($this->root . '/bin/cli.php');
+      $parts[] = escapeshellarg("-e=" . $apiCall['entity']);
+      $parts[] = escapeshellarg("-a=" . $apiCall['action']);
+      $parts[] = "--json";
+      $parts[] = escapeshellarg("-u=dummyuser");
+      foreach ($apiCall['params'] as $key => $value) {
+        $parts[] = escapeshellarg("--$key=$value");
+      }
     }
-    $command = implode(" ", $parts);
 
+    $command = implode(" ", $parts);
     $env = array_merge($this->env, array(
       'CIVICRM_SETTINGS' => $this->settingsPath,
     ));
index 3da5559f4d0eed714b3c5db0ba2bf666a4a95cb2..2b2060f1701d886ab8ad92498ed075fe6ca5650f 100644 (file)
@@ -94,11 +94,11 @@ if (!defined('CIVICRM_UF_DSN') && CIVICRM_UF !== 'UnitTests') {
  *
  */
 if (!defined('CIVICRM_DSN')) {
-  if (CIVICRM_UF !== 'UnitTests') {
-    define('CIVICRM_DSN', 'mysql://%%dbUser%%:%%dbPass%%@%%dbHost%%/%%dbName%%?new_link=true');
+  if (CIVICRM_UF === 'UnitTests' && isset($GLOBALS['_CV']['TEST_DB_DSN'])) {
+    define('CIVICRM_DSN', $GLOBALS['_CV']['TEST_DB_DSN']);
   }
   else {
-    define('CIVICRM_DSN', 'mysql://%%testUser%%:%%testPass%%@%%testHost%%/%%testName%%?new_link=true');
+    define('CIVICRM_DSN', 'mysql://%%dbUser%%:%%dbPass%%@%%dbHost%%/%%dbName%%?new_link=true');
   }
 }