webtrees icon indicating copy to clipboard operation
webtrees copied to clipboard

Using ExampleModuleServerConfig.php causes unit test error

Open ddrury opened this issue 2 years ago • 5 comments

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

ddrury avatar Apr 23 '22 12:04 ddrury

$ 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?

fisharebest avatar Apr 25 '22 10:04 fisharebest

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

ddrury avatar Apr 25 '22 11:04 ddrury

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

ddrury avatar Apr 27 '22 10:04 ddrury

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.

fisharebest avatar Apr 29 '22 07:04 fisharebest

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

ddrury avatar Apr 29 '22 08:04 ddrury