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:
Andrew Nicols
2024-04-08 09:35:41 +08:00
parent 26649f5750
commit 2b49ad42f5
17 changed files with 89 additions and 42 deletions
+7 -2
View File
@@ -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();
}
/**