From cf4c32ddeea91eee6f4a8fd30a92b288d9120a69 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 16 Jul 2019 00:19:22 +1000 Subject: [PATCH 1/2] Add in Deprecation warnings on Cache functons --- CRM/Core/BAO/Cache.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CRM/Core/BAO/Cache.php b/CRM/Core/BAO/Cache.php index d5b4ade2f2b8..e71e24e368c9 100644 --- a/CRM/Core/BAO/Cache.php +++ b/CRM/Core/BAO/Cache.php @@ -73,6 +73,9 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache { * @deprecated */ public static function &getItem($group, $path, $componentID = NULL) { + CRM_Core_Error::deprecatedFunctionWarning( + 'CRM_Core_BAO_Cache::getItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container' + ); if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) { $value = $adapter::getItem($group, $path, $componentID); return $value; @@ -116,6 +119,9 @@ public static function &getItem($group, $path, $componentID = NULL) { * @deprecated */ public static function &getItems($group, $componentID = NULL) { + CRM_Core_Error::deprecatedFunctionWarning( + 'CRM_Core_BAO_Cache::getItems is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container' + ); if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) { return $adapter::getItems($group, $componentID); } @@ -161,6 +167,9 @@ public static function &getItems($group, $componentID = NULL) { * @deprecated */ public static function setItem(&$data, $group, $path, $componentID = NULL) { + CRM_Core_Error::deprecatedFunctionWarning( + 'CRM_Core_BAO_Cache::setItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container' + ); if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) { return $adapter::setItem($data, $group, $path, $componentID); } @@ -232,6 +241,9 @@ public static function setItem(&$data, $group, $path, $componentID = NULL) { * @deprecated */ public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) { + CRM_Core_Error::deprecatedFunctionWarning( + 'CRM_Core_BAO_Cache::deleteGroup is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container' + ); if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) { return $adapter::deleteGroup($group, $path); } From fd5f6b03ff91b73120f71b779e9f928c111af081 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 16 Jul 2019 06:48:57 +1000 Subject: [PATCH 2/2] Update Unit tests to use Civi facade --- tests/phpunit/CRM/Core/BAO/CacheTest.php | 22 +++++++++++++++++++--- tests/phpunit/api/v3/SystemTest.php | 8 ++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/CacheTest.php b/tests/phpunit/CRM/Core/BAO/CacheTest.php index 537556c0e041..23326e9f8ee3 100644 --- a/tests/phpunit/CRM/Core/BAO/CacheTest.php +++ b/tests/phpunit/CRM/Core/BAO/CacheTest.php @@ -31,6 +31,20 @@ */ class CRM_Core_BAO_CacheTest extends CiviUnitTestCase { + /** + * @var CRM_Utils_Cache_Interface + */ + protected $a; + + public function createSimpleCache() { + return new CRM_Utils_Cache_FastArrayDecorator( + $this->a = CRM_Utils_Cache::create([ + 'name' => 'CRM_Core_BAO_CacheTest', + 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], + ]) + ); + } + public function testMultiVersionDecode() { $encoders = ['serialize', ['CRM_Core_BAO_Cache', 'encode']]; $values = [NULL, 0, 1, TRUE, FALSE, [], ['abcd'], 'ab;cd', new stdClass()]; @@ -68,9 +82,10 @@ public function exampleValues() { * @dataProvider exampleValues */ public function testSetGetItem($originalValue) { - CRM_Core_BAO_Cache::setItem($originalValue, __CLASS__, 'testSetGetItem'); + $this->createSimpleCache(); + $this->a->set('testSetGetItem', $originalValue); - $return_1 = CRM_Core_BAO_Cache::getItem(__CLASS__, 'testSetGetItem'); + $return_1 = $this->a->get('testSetGetItem'); $this->assertEquals($originalValue, $return_1); // Wipe out any in-memory copies of the cache. Check to see if the SQL @@ -78,7 +93,8 @@ public function testSetGetItem($originalValue) { CRM_Core_BAO_Cache::$_cache = NULL; CRM_Utils_Cache::$_singleton = NULL; - $return_2 = CRM_Core_BAO_Cache::getItem(__CLASS__, 'testSetGetItem'); + $this->a->values = []; + $return_2 = $this->a->get('testSetGetItem'); $this->assertEquals($originalValue, $return_2); } diff --git a/tests/phpunit/api/v3/SystemTest.php b/tests/phpunit/api/v3/SystemTest.php index 9bccdbe8abec..91a12ce2fcdc 100644 --- a/tests/phpunit/api/v3/SystemTest.php +++ b/tests/phpunit/api/v3/SystemTest.php @@ -54,17 +54,17 @@ public function testFlush() { // check all of them -- just enough to make sure that the API is doing // something - $this->assertTrue(NULL === CRM_Core_BAO_Cache::getItem(self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH)); + $this->assertTrue(NULL === Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH))); $data = 'abc'; - CRM_Core_BAO_Cache::setItem($data, self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH); + Civi::cache()->set(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH), $data); - $this->assertEquals('abc', CRM_Core_BAO_Cache::getItem(self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH)); + $this->assertEquals('abc', Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH))); $params = array(); $result = $this->callAPIAndDocument('system', 'flush', $params, __FUNCTION__, __FILE__, "Flush all system caches", 'Flush'); - $this->assertTrue(NULL === CRM_Core_BAO_Cache::getItem(self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH)); + $this->assertTrue(NULL === Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH))); } /**