webtrees
webtrees copied to clipboard
Using ExampleModuleServerConfig.php causes unit test error
Note: I uploaded the example module "as is" to prove the module code generates the error
Whilst the code runs with no errors, unit testing on PHP 7.4, 8.0 & 8.1 all produce the following error
PHP Fatal error: Cannot declare class ExampleNamespace\ExampleModuleServerConfig, because the name is already in use in /home/runner/work/webtrees/webtrees/modules_v4/ExampleModuleServerConfig/ExampleModuleServerConfig.php on line 17
$ cd modules_v4/
$ git clone https://github.com/webtrees/example-module-server-config
Works for me. Do you have another copy of the module installed?
Do you have another copy of the module installed?
No Steps to reproduce
- Create a temporary branch on github based on main
- Clone into modules_v4 as above
- Temporarily Remove modules_v4 from .gitignore
- Commit and push module to github
- Result is unit test error as above
Solved by changing line 19 of ExampleModuleServerConfig.php to
require_once __DIR__ . '/ExampleModuleServerConfig.php';
ie. require -> require_once
There is a lot of discussion on the web about this, primarily but not limited to Laravel unit testing, the recommended solution is as above
We prefer require
for performance. With require_once
, PHP must maintain a list of which files have been opened already.
The normal logic for webtrees would only initialise the modules once. Hence we can use require
.
But the test script is initialising them multiple times.
We can tell phpunit to run specific tests in isolation. This is a bit slower, so isn't enabled by default.
I think we need to find out which test is failing, and then run that one in isolation.
The failure report is the only thing shown. I don't know how to determine the failing test