From: Tim Otten Date: Thu, 15 Dec 2022 04:13:08 +0000 (-0800) Subject: CiviUnitTestCase - Fix miscoordination with CiviEnvBuilder X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a20fc42cf3ef955e92ff461644926edfed5c2076;p=civicrm-core.git CiviUnitTestCase - Fix miscoordination with CiviEnvBuilder Background ---------- Recall that `CiviUnitTestCase` and `CiviEnvBuilder` are two different implementations of a similar concept. * `Concept`: If two tests use the same baseline DB/environment, and if they preserve the baseline, then you don't need to reset everything in between tests. But if they change, then you need to reset. * `CiviUnitTestCase`: The older rendition. Tightly coupled. Only allows one specific baseline. Mingled with a lot of unrelated functionality in `CiviUnitTestCase`. * `CiviEnvBuilder`: The newer rendition. Loosely coupled. Allows different baselines. Can be mixed into any plain-old `TestCase`. Used by `Civi\Test::headless()` and `Civi\Test::e2e()`. Problem Scenario ---------------- Suppose you have a mix of different tests running with the same DB -- e.g. 1. Run some tests based on `CiviEnvBuilder` 2. Run some tests based on `CiviUnitTestCase` 3. Run some tests based on `CiviEnvBuilder` This wasn't originally anticipated, but it can happen -- either because the test-suite is large+mixed, or because a developer is manually running specific tests (which happen to be written differently). The problem goes like this: 1. Run some tests based on `CiviEnvBuilder` * This resets the DB and also stores a DB signature. ("Here is how we setup the DB...") 2. Run some tests based on `CiviUnitTestCase` * This resets the DB, but leaves the old DB signature in place. 3. Run some tests based on `CiviEnvBuilder` * This sees the old DB signature and wrongly concludes that we still have the DB from step (1). Solution -------- Whenever one resets the DB, it should update the DB signature. --- diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 0267d64048..304456c6e7 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -343,6 +343,10 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { Civi\Test::data()->populate(); + // `CiviUnitTestCase` has replaced the baseline DB configuration. To coexist with other + // tests based on `CiviEnvBuilder`, we need to store a signature marking the current DB configuration. + (new \Civi\Test\CiviEnvBuilder())->callback(function(){}, 'CiviUnitTestCase')->apply(TRUE); + return TRUE; }