FMCorz / mdk

Moodle Development Kit. A collection of tools meant to make developers' lives easier.
GNU General Public License v3.0
84 stars 47 forks source link

Avoid PHP Notice "Undefined variable: DB" in PHPUnit setup #210

Closed mudrd8mz closed 2 years ago

mudrd8mz commented 2 years ago

Without declaring $DB as a global variable in config.php (similarly to how it is done for $CFG), the PHPUnit raises

Notice: Undefined variable: DB in {...}/config.php on line 44
FMCorz commented 2 years ago

Hi @mudrd8mz.

The fix invalidates the primary purpose of declaring the variable in config.php, which is to help VSCode resolve $DB. I have not seen the error you are reporting, would you be provide steps for me to replicate it?

An alternative patch might be:

/** @var moodle_database */
$DB = isset($DB) ? $DB : null;

Thanks!

mudrd8mz commented 2 years ago
$ mdk create -v 400 -n test400 -i
$ gt test400
$ mdk run dev
$ mdk phpunit

$ mdk phpunit
Initialising Moodle PHPUnit test environment...
You are already using the latest available Composer version 2.3.6 (stable channel).
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Nothing to install, update or remove
Generating autoload files
52 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
PHP Notice:  Undefined variable: DB in /home/mudrd8mz/www/mdk/test400/moodle/config.php on line 41
mudrd8mz commented 2 years ago

IIRC there is something related to the fact how PHPUnit includes Moodle files. We had to use global $CFG for that reasons in some places, too.

FMCorz commented 2 years ago

Yes, likely caused by an inclusion within a function/method. I'm not seeing the notice myself, is that PHP 8?

mudrd8mz commented 2 years ago

PHP 7.4.28 with


$ php -i | grep '\(error_reporting\|display_errors\)'
display_errors => STDOUT => STDOUT
error_reporting => 32767 => 32767
FMCorz commented 2 years ago

Right. Does this solve it?

/** @var moodle_database */
$DB = isset($DB) ? $DB : null;

If not, we can remove these altogether, VS Code still doesn't seem to reliably resolve $DB.

mudrd8mz commented 2 years ago

Does this solve it?

It does. I've amended the PR.

FMCorz commented 2 years ago

Thanks!