tripal/t4d8

LogicException when tests fail

laceysanderson opened this issue · 1 comments

We are currently seeing a LogicException regarding the database connection when tests fail.
Specifically, you will see the following error:

PHPUnit\Framework\Exception: PHP Fatal error: Uncaught LogicException: The database connection is not serializable.

This was first discovered in the Type Field PR (see output at the bottom). I confirmed the test was simply failing and had nothing to do with a database connection by isolating the exact assertion which was causing the failure. When I forced that assertion to pass (i.e.. replaced variables with static numbers which were equal) I found the logic exception went away.

Me + Stephen suspect that this may be due to initializing the chado database connection as a property in our Chado Test Base class.

docker exec --workdir=/var/www/drupal9/web/modules/contrib/tripal t4d8fields phpunit --filter testChadoPreparer

PHPUnit 9.5.27 by Sebastian Bergmann and contributors.

Testing
E 1 / 1 (100%)R

Time: 01:04.588, Memory: 14.00 MB

There was 1 error:

  1. Drupal\Tests\tripal_chado\Functional\Task\ChadoPreparerTest::testChadoPreparer
    PHPUnit\Framework\Exception: PHP Fatal error: Uncaught LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in /var/www/drupal9/web/core/lib/Drupal/Core/Database/Connection.php:2030
    Stack trace:
    #\0 [internal function]: Drupal\Core\Database\Connection->__sleep()
    #\1 Standard input code(89): serialize(Array)
    #\2 Standard input code(123): __phpunit_run_isolated_test()
    #\3 {main}
    thrown in /var/www/drupal9/web/core/lib/Drupal/Core/Database/Connection.php on line 2030

/var/www/drupal9/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:270
/var/www/drupal9/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:187
/var/www/drupal9/vendor/phpunit/phpunit/src/Framework/TestSuite.php:675
/var/www/drupal9/vendor/phpunit/phpunit/src/Framework/TestSuite.php:675
/var/www/drupal9/vendor/phpunit/phpunit/src/Framework/TestSuite.php:675
/var/www/drupal9/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:661
/var/www/drupal9/vendor/phpunit/phpunit/src/TextUI/Command.php:144
/var/www/drupal9/vendor/phpunit/phpunit/src/TextUI/Command.php:97

--

There was 1 risky test:

  1. Drupal\Tests\tripal_chado\Functional\Task\ChadoPreparerTest::testChadoPreparer
    This test did not perform any assertions

/var/www/drupal9/web/core/tests/Drupal/Tests/Listeners/DrupalListener.php:127
/var/www/drupal9/vendor/phpunit/phpunit/src/Framework/TestResult.php:452
/var/www/drupal9/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:377
/var/www/drupal9/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:187
/var/www/drupal9/vendor/phpunit/phpunit/src/Framework/TestSuite.php:675
/var/www/drupal9/vendor/phpunit/phpunit/src/Framework/TestSuite.php:675
/var/www/drupal9/vendor/phpunit/phpunit/src/Framework/TestSuite.php:675
/var/www/drupal9/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:661
/var/www/drupal9/vendor/phpunit/phpunit/src/TextUI/Command.php:144
/var/www/drupal9/vendor/phpunit/phpunit/src/TextUI/Command.php:97

ERRORS!
Tests: 1, Assertions: 0, Errors: 1, Risky: 1.

It turns out this was only happening if a test failed within the helper function runPrepareStepAssertions(). It was due to passing a connection object as a parameter to the helper function during testing 🙈 Switching to instead pass the schema name and then re-setup the connection fixed the bug.