ClassScanner - Fix guard for preboot cache scenario
authorTim Otten <totten@civicrm.org>
Thu, 8 Sep 2022 00:40:44 +0000 (17:40 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 8 Sep 2022 00:40:44 +0000 (17:40 -0700)
I wanted to inspect how caches were getting setup during test-runs.  So I applied
this hack https://github.com/civicrm/civicrm-core/commit/02106e8693d13ced7a151834a4188ce8b16f6444
and then ran some tests, eg

```
CIVICRM_UF=UnitTests phpunit8 tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php
```

You can see the change in the HitOrMiss report (displayed after the test).

Before
======

```
OK (3 tests, 38 assertions)
HitOrMiss: {
    "ClassScanner::cache(index) => Early usage": 13,
    "ClassScanner::cache(structure) => New arraycache": 1
}
```

After
=====

```
OK (3 tests, 38 assertions)
HitOrMiss: {
    "ClassScanner::cache(index) => create(...)": 1,
    "ClassScanner::cache(structure) => New arraycache": 2
}
```

Civi/Core/ClassScanner.php

index 500278674987cafeaf8fe213c4f8d29ee463b195..5b318d056baadcc84615764bbf3c4511879894bb 100644 (file)
@@ -227,6 +227,7 @@ class ClassScanner {
     if (!isset(static::$caches[$name])) {
       switch ($name) {
         case 'index':
+          global $_DB_DATAOBJECT;
           if (empty($_DB_DATAOBJECT['CONFIG'])) {
             // Atypical example: You have a test with a @dataProvider that relies on ClassScanner. Runs before bot.
             return new \CRM_Utils_Cache_ArrayCache([]);