| Q | A
| --------------------| ---------------
| PHPUnit version | 9.3
| PHP version | n/a
| Installation Method | Composer / PHAR (both)
PHPUnits bootstrapping behavior changed with Version 9.3.
PHPUnit fails with an Fatal error when libxml_disable_entity_loader(true) was called in a data provider.
cat <<'EOF' > phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="Functional">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
EOF
cat <<'EOF' > DummyTest.php
<?php
class DummyTest extends PHPUnit\Framework\TestCase
{
/** @dataProvider provideData */
public function testDummy() {}
public function provideData()
{
libxml_disable_entity_loader(true);
set_error_handler(function ($erno, $message) {
throw new \RuntimeException($message);
});
}
}
EOF
wget -O phpunit9.2 https://phar.phpunit.de/phpunit-9.2.phar
wget -O phpunit9.3 https://phar.phpunit.de/phpunit-9.3.phar
$ php phpunit9.2 --debug
PHPUnit 9.2.6 by Sebastian Bergmann and contributors.
Test 'DummyTest::testDummy' started
Test 'DummyTest::testDummy' ended
Time: 00:00, Memory: 12.00 MB
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Skipped: 1.
$ php phpunit9.3 --debug
PHP Fatal error: Uncaught RuntimeException: DOMDocument::schemaValidate(): Invalid Schema in /home/apidev/current/phpunit-repro/DummyTest.php:11
Stack trace:
#0 [internal function]: DummyTest->{closure}()
#1 phar:///home/apidev/current/phpunit-repro/phpunit9.3/phpunit/Util/Xml/Validator.php(26): DOMDocument->schemaValidate()
#2 phar:///home/apidev/current/phpunit-repro/phpunit9.3/phpunit/TextUI/XmlConfiguration/Loader.php(81): PHPUnit\Util\Xml\Validator->validate()
#3 phar:///home/apidev/current/phpunit-repro/phpunit9.3/phpunit/TextUI/TestRunner.php(639): PHPUnit\TextUI\XmlConfiguration\Loader->load()
#4 phar:///home/apidev/current/phpunit-repro/phpunit9.3/phpunit/TextUI/TestRunner.php(138): PHPUnit\TextUI\TestRunner->handleConfiguration()
#5 phar:///home/apidev/current/phpunit-repro/phpunit9.3/phpunit/TextUI/Command.php(128): PHPUnit\TextUI\TestRunner->run()
#6 phar:///home/apidev/current/phpunit-repro/phpunit9.3/phpunit/TextUI/Command.php(97): PHPUnit\TextUI\Command->run()
#7 /home/apidev/current/phpunit-repro/phpunit9.3(10 in phar:///home/apidev/current/phpunit-repro/phpunit9.3/phpunit/TextUI/Command.php on line 99
PHPUnit should not fail with an Fatal error but continue the programm flow.
Enable entity loading before schema validation in \PHPUnit\Util\Xml\Validator::validate():
try {
$before = libxml_disable_entity_loader(false);
$document->schemaValidate($xsdFilename);
} finally {
libxml_disable_entity_loader($before);
}
Would you be open for a fix PR?
CC @theseer
Would you be open for a fix PR?
Sure! Thanks. Please make it against the 9.3 branch.
I'm confused as to why this would be a problem. The validator should only be called at startup. Why would it be affecting any Tests or data providers?
I of course might be very well missing the point here ;)
The validator is called a second time during startup _after_ the data provider have been executed.
Why? ;-)
Why? ;-)
Because legacy? While the XML configuration loading as well as the CLI arguments/options parsing has been cleaned up recently, the test runner does not cleanly use the new APIs yet.
bc5f0c92031fb19a7ec05a9033c46f6bdc5347db should avoid loading the XML configuration file twice.
The validator is called a second time during startup _after_ the data provider have been executed.
Do I understand your observation correctly that if the XML configuration is not loaded twice that the problem is solved?
Do I understand your observation correctly that if the XML configuration is not loaded twice that the problem is solved?
As long as the config validation happens before any data providers or user provided bootstrapping code is executed, that problem should be solved.
As long as the config validation happens before any data providers or user provided bootstrapping code is executed, that problem should be solved.
Good. Can you please test your scenario with the current state of the 9.3 branch? Thanks!
Can confirm that it's fixed using the latest 9.3-branch:
Thank you @sebastianbergmann :+1:
$ ./phpunit/phpunit -c . --debug
PHPUnit 9.3.6-10-gb059a3576 by Sebastian Bergmann and contributors.
Test 'DummyTest::testDummy' started
Test 'DummyTest::testDummy' ended
Time: 00:00.002, Memory: 6.00 MB
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Skipped: 1.
Most helpful comment
bc5f0c92031fb19a7ec05a9033c46f6bdc5347db should avoid loading the XML configuration file twice.