MDL-86432 phpunit: Ensure that data providers do not modify state

This commit is contained in:
Andrew Nicols
2025-09-24 16:53:30 +01:00
committed by Tim Hunt
parent 089f43cfd8
commit db8a5b2f7a
3 changed files with 164 additions and 0 deletions
@@ -0,0 +1,116 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\tests\phpunit;
use PHPUnit\Event\Test\DataProviderMethodFinished;
use PHPUnit\Event\Test\DataProviderMethodFinishedSubscriber;
/**
* PHPUnit Event Subscriber for DataProviderMethodFinished event.
*
* @package core
* @copyright Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class data_provider_finished_subscriber implements DataProviderMethodFinishedSubscriber {
/** @var ?int The number of DB writes */
private static ?int $dbwrites = null;
#[\Override]
public function notify(DataProviderMethodFinished $event): void {
$resetall = false;
// Note: All of these checks must be extremely lightweight.
// This method is called after every single Data Provider.
if ($this->is_page_set()) {
$this->trigger_notice($event, "has set the theme");
$resetall = true;
}
if ($this->is_db_written()) {
$this->trigger_notice($event, "has written to the database");
$resetall = true;
}
if ($resetall) {
\phpunit_util::reset_all_data();
$this->update_db_writes();
}
}
/**
* Check whether the page/theme has been initialised.
*
* Data Providers should not rely on the page/theme being set.
*
* @return bool
*/
private function is_page_set(): bool {
global $PAGE;
return (new \ReflectionProperty(\moodle_page::class, '_theme'))->getValue($PAGE) !== null;
}
/**
* Check whether the database has been written to.
*
* Data Providers should not rely on the database being written to.
*
* @return bool
*/
private function is_db_written(): bool {
global $DB;
if (!$DB) {
// DB not initialised yet.
return false;
}
if (self::$dbwrites === null) {
self::$dbwrites = $DB->perf_get_writes();
}
return $DB->perf_get_writes() > self::$dbwrites;
}
/**
* Update the database write count.
*/
private function update_db_writes(): void {
global $DB;
self::$dbwrites = $DB->perf_get_writes();
}
/**
* Trigger a warning on the CLI.
*
* Note: PHPUnit does not let us actually emit a notice or warning for the DataProviderFinished event.
*
* @param DataProviderMethodFinished $event
* @param string $message
*/
private function trigger_notice(DataProviderMethodFinished $event, string $message): void {
printf(
"Warning: Data provider for %s::%s %s%s",
$event->testMethod()->className(),
$event->testMethod()->methodName(),
$message,
PHP_EOL,
);
}
}
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\tests\phpunit;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
/**
* Moodle PHPUnit Extension Registration.
*
* @package core
* @copyright Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class moodle_extension implements Extension {
#[\Override]
public function bootstrap(
Configuration $configuration,
Facade $facade,
ParameterCollection $parameters
): void {
if ($configuration->noOutput()) {
return;
}
$facade->registerSubscriber(new data_provider_finished_subscriber());
}
}