Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
If you do something like:
$this->assertEquals('/a-url-that-does-not-match', $this->getSession()->getCurrentUrl());
This will populate BrowserTestBase::$mink and BrowserTestBase::$container, neither or which can be serialised. And the PHPUnit result will contain a stack trace which include the test object which will fail when serialized as part of the way tests in separate processes are run.
Steps to reproduce
- Add
$this->assertSame('asd', 'a');
as the first line of BrowserTestBaseTest::testGoTo() - Step up the environmental variables so that tests work
phpunit -c core core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php
Expected result
1) Drupal\Tests\simpletest\Functional\BrowserTestBaseTest::testGoTo
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-asd
+a
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/Constraint.php:110
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/Constraint/IsIdentical.php:88
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/Assert.php:2183
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/Assert.php:1174
/Volumes/devdisk/dev/sites/drupal8alt.dev/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php:30
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/TestCase.php:889
/Volumes/devdisk/dev/sites/drupal8alt.dev/core/modules/simpletest/src/BrowserTestBase.php:822
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/TestCase.php:749
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/TestResult.php:601
/Volumes/devdisk/dev/sites/drupal8alt.dev/vendor/phpunit/phpunit/src/Framework/TestCase.php:705
Actual result
1) Drupal\Tests\simpletest\Functional\BrowserTestBaseTest::testGoTo
PHPUnit_Framework_Exception: PHP Fatal error: Uncaught exception 'LogicException' with message '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 /Volumes/devdisk/dev/sites/drupal8alt.dev/core/lib/Drupal/Core/Database/Connection.php:1433
Stack trace:
#0 [internal function]: Drupal\Core\Database\Connection->__sleep()
#1 -(70): serialize(Array)
#2 -(83): __phpunit_run_isolated_test()
#3 {main}
thrown in /Volumes/devdisk/dev/sites/drupal8alt.dev/core/lib/Drupal/Core/Database/Connection.php on line 1433
The same problem afflicts KernelTestBase tests see #6
Eligible for 8.0.x because it is a test only change.
Proposed resolution
Don't serialise any properties - there is no point.
Remaining tasks
User interface changes
None
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#6 | 3-6-interdiff.txt | 910 bytes | alexpott |
#6 | 2636228-6.patch | 1.83 KB | alexpott |
#3 | 2-3-interdiff.txt | 881 bytes | alexpott |
#3 | 2636228-3.patch | 959 bytes | alexpott |
#2 | 2636228-2.patch | 959 bytes | alexpott |
Comments
Comment #2
alexpottComment #3
alexpottComment #4
alexpottComment #5
dawehnerInteresting!
Totally agree with that.
Should we add it to KernelTestBase as well, people could easily do a similar mistake, like storing something in
setup()
Comment #6
alexpott@dawehner yep - this can afflict KernelTestBase too... just do $this->db = $this->container->get('database'); and the assert in the issue summary and it breaks the same way.
Comment #7
dawehnerCan we please have some tests?
Comment #8
alexpottI have no idea how to test this - the error occurs in PHPUnit code outside of the test method.
Comment #9
bojanz CreditAttribution: bojanz at Centarro commentedTesting a test sounds a bit meta.
Comment #10
dawehnerFair. Its indeed pretty hard to test test failures.
Comment #11
catchCommitted/pushed to 8.1.x and cherry-picked to 8.0.x. Thanks!