Skip to content

Commit

Permalink
Restore execution of CaseActivityTest
Browse files Browse the repository at this point in the history
Following eb92dd7, the `CaseActivityTest` started to run
only intermittently. Why?

__high-level__: `Civi\Core\ClassScanner` and `phpunit8` both do a scan over the folder `tests/phpunit/CRM/Case/WorkflowMessage`

__low-level__: `Civi\Core\ClassScanner` has caching. Depending on the state of the cache, it may or may not do a scan:

* If the cache is filled, then `ClassScanner` doesn't need to scan.
    * When `phpunit8` subsequently does a scan, it will load `CaseActivityTest.php` normally.
* If the cache is empty, then `ClassScanner` does the first scan. It is the one that actually loads `CaseActivityTest.php`.
    * Later, `phpunit8` does a scan. Due to a quirk, it doesn't realize the class exists.

The scanner in phpunit works roughly like this:

```php
$tests = [];
foreach (glob('*Test.php') as $file) {
  $before = get_declared_classes();
  require_once $file;
  $after = get_declared_classes();
  $tests = array_merge($tests, array_diff($before, $after));
}
```

So if the class was previously loaded, then phpunit doesn't see it.
  • Loading branch information
totten committed Jan 25, 2023
1 parent c8f4aed commit cdfefc5
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tests/phpunit/CiviTest/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
function civitest_civicrm_scanClasses(array &$classes): void {
$phpunit = \Civi::paths()->getPath('[civicrm.root]/tests/phpunit');
if (strpos(get_include_path(), $phpunit) !== FALSE) {
\Civi\Core\ClassScanner::scanFolders($classes, $phpunit, 'CRM/*/WorkflowMessage', '_');
\Civi\Core\ClassScanner::scanFolders($classes, $phpunit, 'Civi/*/WorkflowMessage', '\\');
\Civi\Core\ClassScanner::scanFolders($classes, $phpunit, 'CRM/*/WorkflowMessage', '_', '/Test$/');
\Civi\Core\ClassScanner::scanFolders($classes, $phpunit, 'Civi/*/WorkflowMessage', '\\', '/Test$/');
// Exclude all `*Test.php` files - if we load them, then phpunit gets confused.
}
}

Expand Down

0 comments on commit cdfefc5

Please sign in to comment.