Dismbiguate init and flush
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 16 Aug 2022 01:18:56 +0000 (13:18 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 16 Aug 2022 08:40:43 +0000 (20:40 +1200)
CRM/Core/DAO/AllCoreTables.php
tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php
tests/phpunit/CRM/Dedupe/BAO/RuleGroupTest.php
tests/phpunit/Civi/API/Subscriber/DynamicFKAuthorizationTest.php
tests/phpunit/Civi/API/Subscriber/WhitelistSubscriberTest.php
tests/phpunit/api/v3/CustomApiTest.php

index bb1e808d6a8c3dd267329d3d4fe0945f4232246b..acf66cb837f94d9bec1d380b0fe3950286b27a12 100644 (file)
@@ -24,13 +24,16 @@ class CRM_Core_DAO_AllCoreTables {
   /**
    * Initialise.
    *
-   * @param bool $fresh
+   * @param bool $fresh Deprecated parameter, use flush() to flush.
    */
-  public static function init($fresh = FALSE) {
-    static $init = FALSE;
-    if ($init && !$fresh) {
+  public static function init(bool $fresh = FALSE): void {
+    if (!empty(Civi::$statics[__CLASS__]['initialised']) && !$fresh) {
       return;
     }
+    if ($fresh) {
+      CRM_Core_Error::deprecatedWarning('Use CRM_Core_DAO_AllCoreTables::flush()');
+    }
+    Civi::$statics[__CLASS__]['initialised'] = TRUE;
     Civi::$statics[__CLASS__] = [];
 
     $file = preg_replace('/\.php$/', '.data.php', __FILE__);
@@ -50,7 +53,14 @@ class CRM_Core_DAO_AllCoreTables {
       );
     }
 
-    $init = TRUE;
+    Civi::$statics[__CLASS__]['initialised'] = TRUE;
+  }
+
+  /**
+   * Flush class cache.
+   */
+  public static function flush(): void {
+    Civi::$statics[__CLASS__]['initialised'] = FALSE;
   }
 
   /**
index 612b2c3cf7f67214cdb58c5bff8a41ad5b5a9082..2f51e02728d15d55a9d49616b31e9055a4257c9a 100644 (file)
@@ -26,7 +26,7 @@ class CRM_Core_DAO_AllCoreTablesTest extends CiviUnitTestCase {
     // 2. Now, let's hook into it...
     $this->hookClass->setHook('civicrm_entityTypes', [$this, '_hook_civicrm_entityTypes']);
     unset(Civi::$statics['CRM_Core_DAO_Email']);
-    CRM_Core_DAO_AllCoreTables::init(1);
+    CRM_Core_DAO_AllCoreTables::flush();
 
     // 3. And see if the data has changed...
     $fields = CRM_Core_DAO_Email::fields();
@@ -50,7 +50,7 @@ class CRM_Core_DAO_AllCoreTablesTest extends CiviUnitTestCase {
 
   protected function tearDown(): void {
     CRM_Utils_Hook::singleton()->reset();
-    CRM_Core_DAO_AllCoreTables::init(1);
+    CRM_Core_DAO_AllCoreTables::flush();
     parent::tearDown();
   }
 
index 3c32268ceab817be8a1b9dfe01a479e353d7f1b2..2438468206483021351a37b086595e5b82f837cc 100644 (file)
@@ -255,10 +255,10 @@ class CRM_Dedupe_BAO_RuleGroupTest extends CiviUnitTestCase {
    *
    * @throws \CRM_Core_Exception
    */
-  public function testHookDupeQueryMatch() {
+  public function testHookDupeQueryMatch(): void {
     $this->hookClass->setHook('civicrm_dupeQuery', [$this, 'hook_civicrm_dupeQuery']);
 
-    \CRM_Core_DAO_AllCoreTables::init(TRUE);
+    \CRM_Core_DAO_AllCoreTables::flush();
 
     \CRM_Core_DAO_AllCoreTables::registerEntityType('TestEntity', 'CRM_Dedupe_DAO_TestEntity', 'civicrm_dedupe_test_table');
     $this->apiKernel = \Civi::service('civi_api_kernel');
@@ -380,7 +380,7 @@ class CRM_Dedupe_BAO_RuleGroupTest extends CiviUnitTestCase {
     $this->assertCount(0, $foundDupes);
 
     CRM_Core_DAO::executeQuery('DROP TABLE civicrm_dedupe_test_table');
-    \CRM_Core_DAO_AllCoreTables::init(TRUE);
+    \CRM_Core_DAO_AllCoreTables::flush();
   }
 
   /**
index c69c3e4fb26725a87a9be50d00eec9df3cadb1f2..85314e82ca351c099a595eedc200f70d4827ec12 100644 (file)
@@ -30,7 +30,7 @@ class DynamicFKAuthorizationTest extends \CiviUnitTestCase {
 
   protected function setUp(): void {
     parent::setUp();
-    \CRM_Core_DAO_AllCoreTables::init(TRUE);
+    \CRM_Core_DAO_AllCoreTables::flush();
 
     \CRM_Core_DAO_AllCoreTables::registerEntityType('FakeFile', 'CRM_Fake_DAO_FakeFile', 'fake_file');
     $fileProvider = new StaticProvider(
@@ -104,7 +104,7 @@ class DynamicFKAuthorizationTest extends \CiviUnitTestCase {
 
   protected function tearDown(): void {
     parent::tearDown();
-    \CRM_Core_DAO_AllCoreTables::init(TRUE);
+    \CRM_Core_DAO_AllCoreTables::flush();
   }
 
   /**
index 768b8fe12a461bed32a20a28deaf8f5508cd85d0..0da270a6899f2566e7fc69d44ef088c5dbb2b6ea 100644 (file)
@@ -349,7 +349,7 @@ class WhitelistSubscriberTest extends \CiviUnitTestCase {
    * @dataProvider restrictionCases
    */
   public function testEach($apiRequest, $rules, $expectSuccess) {
-    \CRM_Core_DAO_AllCoreTables::init(TRUE);
+    \CRM_Core_DAO_AllCoreTables::flush();
 
     $recs = $this->getFixtures();
 
index 1ff50958619438cd16e80be38a2d99021e45c667..33c72bcb7c6bb85497ef87afc21eec26c0ab186e 100644 (file)
@@ -69,10 +69,10 @@ class api_v3_CustomApiTest extends CiviUnitTestCase {
   /**
    * Install the custom api.
    */
-  public function installApi() {
+  public function installApi(): void {
     require_once __DIR__ . '/custom_api/MailingProviderData.php';
     $this->hookClass->setHook('civicrm_entityTypes', [$this, 'hookEntityTypes']);
-    CRM_Core_DAO_AllCoreTables::init(TRUE);
+    CRM_Core_DAO_AllCoreTables::flush();
     CRM_Core_DAO::executeQuery(
       "CREATE TABLE IF NOT EXISTS `civicrm_mailing_provider_data` (
     `contact_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',