MDL-77989 testing: Add test file isolation helper

When we deprecate the use of a file, we often include tests which ensure
that the legacy behaviour is maintained. There are also legacy uses
in the community where people would like to use the deprecated API for a
period.

The issue that we face is that, if the deprecated file is included once,
then it will be included for all other, unrelated, tests. This means
that other tests may not detect cases where the deprecated file was
included.

We can solve these cases by running the test that performs the inclusion
in a deprecated process. This means that the inclusion is only performed
in that isolated process, and other unrelated tests do not include the
file.

However, we also then need to detect which files which are including the
file and which we do not know about.

This change introduces:
- an override to the TestCase::setInIsolation method to define a
  constant when the test is running in isolation
- a new function that a file can call when it is included, to make sure
  that the test process was isolated, where there is any test.
This commit is contained in:
Andrew Nicols
2023-04-21 13:33:56 +08:00
parent f7d7ad7fed
commit 1a53cbbae4
4 changed files with 65 additions and 0 deletions
+14
View File
@@ -57,6 +57,20 @@ abstract class advanced_testcase extends base_testcase {
$this->setBackupGlobals(false);
$this->setBackupStaticAttributes(false);
$this->setPreserveGlobalState(false);
}
/**
* Hook into the setInIsolation method to define an optional constant.
*
* @param bool $inisolation
*/
public function setInIsolation(bool $inisolation): void {
parent::setInIsolation($inisolation);
if ($inisolation) {
// Note: This is safe to do because it will only be set once per test run.
define('PHPUNIT_ISOLATED_TEST', true);
}
}
/**