fisharebest / webtrees

Online genealogy
https://webtrees.net
GNU General Public License v3.0
473 stars 301 forks source link

Using ExampleModuleServerConfig.php causes unit test error #4335

Open ddrury opened 2 years ago

ddrury commented 2 years ago

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

fisharebest commented 2 years ago
$ 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?

ddrury commented 2 years ago

Do you have another copy of the module installed?

No Steps to reproduce

ddrury commented 2 years ago

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

fisharebest commented 2 years ago

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.

ddrury commented 2 years ago

The failure report is the only thing shown. I don't know how to determine the failing test