CRM-14885 - CRM_Core_CodeGen_FreshnessTest - Add unit-test to ensure DAO's are current
authorTim Otten <totten@civicrm.org>
Wed, 27 Jul 2016 08:18:16 +0000 (01:18 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 28 Jul 2016 00:20:16 +0000 (17:20 -0700)
CRM/Core/CodeGen/Main.php
tests/phpunit/CRM/Core/CodeGen/FreshnessTest.php [new file with mode: 0644]

index 51b741310da6ba802dc42807cd4690800625d2b2..bb681dc2041da22564a7ced2764c5f9cbb5188e7 100644 (file)
@@ -63,7 +63,7 @@ class CRM_Core_CodeGen_Main {
 
     CRM_Core_CodeGen_Util_Smarty::singleton()->setPluginDirs($smartyPluginDirs);
 
-    $versionFile = "version.xml";
+    $versionFile = $this->phpCodePath . "/xml/version.xml";
     $versionXML = CRM_Core_CodeGen_Util_Xml::parse($versionFile);
     $this->db_version = $versionXML->version_no;
     $this->buildVersion = preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $this->db_version);
@@ -95,12 +95,6 @@ Alternatively you can get a version of CiviCRM that matches your PHP version
       exit();
     }
 
-    $specification = new CRM_Core_CodeGen_Specification();
-    $specification->parse($this->schemaPath, $this->buildVersion);
-    # cheese:
-    $this->database = $specification->database;
-    $this->tables = $specification->tables;
-
     foreach ($this->getTasks() as $task) {
       if (getenv('GENCODE_FORCE') || $task->needsUpdate()) {
         $task->run();
@@ -114,6 +108,8 @@ Alternatively you can get a version of CiviCRM that matches your PHP version
    * @throws \Exception
    */
   public function getTasks() {
+    $this->init();
+
     $tasks = array();
     $tasks[] = new CRM_Core_CodeGen_Config($this);
     $tasks[] = new CRM_Core_CodeGen_Version($this);
@@ -145,4 +141,14 @@ Alternatively you can get a version of CiviCRM that matches your PHP version
     return $this->sourceDigest;
   }
 
+  protected function init() {
+    if (!$this->database || !$this->tables) {
+      $specification = new CRM_Core_CodeGen_Specification();
+      $specification->parse($this->schemaPath, $this->buildVersion);
+      # cheese:
+      $this->database = $specification->database;
+      $this->tables = $specification->tables;
+    }
+  }
+
 }
diff --git a/tests/phpunit/CRM/Core/CodeGen/FreshnessTest.php b/tests/phpunit/CRM/Core/CodeGen/FreshnessTest.php
new file mode 100644 (file)
index 0000000..d20903b
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * Class CRM_Core_CodeGen_FreshnessTest
+ * @group headless
+ *
+ * Ensure that any files which are autogenerated AND which are
+ * committed to git are ALSO up-to-date.
+ */
+class CRM_Core_CodeGen_FreshnessTest extends CiviUnitTestCase {
+
+  public function testDAOs() {
+    $path = rtrim($GLOBALS['civicrm_root'], '/');
+
+    $genCode = new CRM_Core_CodeGen_Main(
+      $path . '/CRM/Core/DAO/', // $CoreDAOCodePath
+      $path . '/sql/', // $sqlCodePath
+      $path . '/', // $phpCodePath
+      $path . '/templates/', // $tplCodePath
+      array(
+        // smarty plugin dirs
+        $path . '/packages/Smarty/plugins',
+        $path . '/CRM/Core/Smarty/plugins',
+      ),
+      CIVICRM_UF, // cms
+      NULL, // db version
+      $path . '/xml/schema/Schema.xml', // schema file
+      NULL  // path to digest file
+    );
+
+    $tasks = $genCode->getTasks();
+    $names = array();
+    foreach ($tasks as $task) {
+      if ($task instanceof CRM_Core_CodeGen_DAO) {
+        $names[] = $task->name;
+        $this->assertFalse($task->needsUpdate(),
+          "Expect DAO for {$task->name} is up-to-date.");
+      }
+    }
+
+    // Pick some example to ensure the loop ran with real values.
+    $this->assertTrue(in_array('civicrm_contact', $names),
+      'Expect the list of tables to include civicrm_contact');
+  }
+
+}