MDL-81456 core: Use DI for all hook access
Using DI for all hook access means that it becomes significantly easier to mock hooks and callbacks for unit testing without fundamentally altering the structure of the code purely for the purposes of unit testing.
This commit is contained in:
@@ -262,6 +262,7 @@ if (PHPUNIT_UTIL) {
|
||||
|
||||
// Make sure the hook manager gets initialised before anybody tries to override callbacks,
|
||||
// this is not using caches intentionally to help with development.
|
||||
// Note: We cannot use DI at this point in the bootstrap either.
|
||||
\core\hook\manager::get_instance();
|
||||
|
||||
// Is database and dataroot ready for testing?
|
||||
|
||||
@@ -14,6 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use core\{
|
||||
di,
|
||||
hook,
|
||||
};
|
||||
|
||||
/**
|
||||
* Advanced PHPUnit test case customised for Moodle.
|
||||
*
|
||||
@@ -486,7 +491,7 @@ abstract class advanced_testcase extends base_testcase {
|
||||
* @return void
|
||||
*/
|
||||
public function redirectHook(string $hookname, callable $callback): void {
|
||||
\core\hook\manager::get_instance()->phpunit_redirect_hook($hookname, $callback);
|
||||
di::get(hook\manager::class)->phpunit_redirect_hook($hookname, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -495,7 +500,7 @@ abstract class advanced_testcase extends base_testcase {
|
||||
* @return void
|
||||
*/
|
||||
public function stopHookRedirections(): void {
|
||||
\core\hook\manager::get_instance()->phpunit_stop_redirections();
|
||||
di::get(hook\manager::class)->phpunit_stop_redirections();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,11 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use core\{
|
||||
di,
|
||||
hook,
|
||||
};
|
||||
|
||||
require_once(__DIR__.'/../../testing/classes/util.php');
|
||||
require_once(__DIR__ . "/coverage_info.php");
|
||||
|
||||
@@ -105,7 +110,7 @@ class phpunit_util extends testing_util {
|
||||
global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $FULLME, $FILTERLIB_PRIVATE;
|
||||
|
||||
// Stop all hook redirections.
|
||||
\core\hook\manager::get_instance()->phpunit_stop_redirections();
|
||||
di::get(hook\manager::class)->phpunit_stop_redirections();
|
||||
|
||||
// Reset the hook manager instance.
|
||||
\core\hook\manager::phpunit_reset_instance();
|
||||
|
||||
Reference in New Issue
Block a user