Problem/Motivation

When running migration Kernel tests, i.e. MigrateNodeComleteTest.php, an error in the source plugin query results in fatal errors from PHPUnit and not a helpful SQL error as expected. The only way to see a useful error message is to use the debugger/

There were 2 errors:

1) Was moved into #3197324: Exception trace cannot be serialized because of closure.
2)

Drupal\Tests\taxonomy\Kernel\Migrate\d7\MigrateTaxonomyTermTest::testTaxonomyTermEntityTranslations
PHPUnit\Framework\Exception: PHP Fatal error:  Uncaught Exception: Serialization of 'Closure' is not allowed in Standard input code:80
Stack trace:
#0 Standard input code(80): serialize(Array)
#1 Standard input code(112): __phpunit_run_isolated_test()
#2 {main}
  thrown in Standard input code on line 80
Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in Standard input code:80
Stack trace:
#0 Standard input code(80): serialize(Array)
#1 Standard input code(112): __phpunit_run_isolated_test()
#2 {main}
  thrown in Standard input code on line 80

/var/www/html/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:254
/var/www/html/vendor/phpunit/phpunit/src/Util/PHP/AbstractPhpProcess.php:171
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:601
/var/www/html/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:633
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:204
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:163

--

Comments

quietone created an issue. See original summary.

quietone’s picture

Another one. This one from \Drupal\Tests\migrate_drupal\Kernel\d7\FieldDiscoveryTest::testAddAllFieldProcessesAlters when there is an error in the source data, just change any line in the provider and you get this unhelpful message when the test fails.

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/html/core/lib/Drupal/Core/Database/Connection.php:1831

matroskeen’s picture

StatusFileSize
new296.58 KB

I started this funny debug session and would like to share in-between results so maybe it'll help someone else to find a proper fix.

If the test is failing with exception, then PHP is trying to serialize a stack trace, that contains functions, methods, classes that have already been executed.

One of these functions is recognized as Closure in the stack trace (see the screenshot). I'm not 100% sure yet, but I think this is the problem.
xdebug trace

matroskeen’s picture

Status: Active » Needs review

The following patch should help to get rid of the issue described in the IS.

However, the error is now strange:

Testing Drupal\Tests\taxonomy\Kernel\Migrate\d6\MigrateTermNodeTest
Fatal error: Uncaught Drupal\Core\Database\ConnectionNotDefinedException: The specified database connection is not defined: migrate in /var/www/docroot/core/lib/Drupal/Core/Database/Database.php on line 371
Drupal\Core\Database\ConnectionNotDefinedException: The specified database connection is not defined: migrate in /var/www/docroot/core/lib/Drupal/Core/Database/Database.php on line 371

I expected to see something like "Column td.foo not found".

As I see, it won't be possible to add a test, because the serialization happens when the test is already executed.
Although it's not usual to have array_walk() inside array_walk(), someone can write such piece of code again 🤷

matroskeen’s picture

StatusFileSize
new894 bytes
matroskeen’s picture

The case described in #2 gives me another error:
Fatal error: Uncaught AssertionError: The container was serialized. in /var/www/docroot/core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php on line 152

Those issues seem similar, but can be occurred because of different reasons. Do we want to keep them in a single issue or we should have a META task?

quietone’s picture

@Matroskeen, Yes, a meta sounds good. These error are happening too often. Can you do that?

matroskeen’s picture

Title: PHPUnit fatal errors in Migration tests when the query has the error » [META] Serialization issues in Migration tests
Category: Task » Plan
Issue summary: View changes
Status: Needs review » Active

Sure! I decided to turn this one into meta and create a spin-off task for the first issue: #3197324: Exception trace cannot be serialized because of closure.

matroskeen’s picture

wim leers’s picture

Replying to #3189064-22: Migration sql source plugins can not be serialised because of reference to the database connection here:

@Wim Leers, I would love to, but I need more details.
Did you encounter the same issue as @quietone reported in #3192893: [META] Serialization issues in Migration tests?

No, but I'm looking into caching \Drupal\migrate\Plugin\Migration objects for performance reasons — that's how I ran into this.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

anybody’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pingwin4eg’s picture

I also encountered the "container was serialized" issue with a migration test in Style Switcher module with D 9.5.11, PHP 8.1, PHPUnits 8.5.31 (local) and 9.5.28 (DrupalCI):

1) Drupal\Tests\styleswitcher\Kernel\Migrate\d7\MigrateVariablesTest::testVariablesMigration
PHPUnit\Framework\Exception: PHP Fatal error:  Uncaught AssertionError: The container was serialized. in /var/www/html/core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php:99
Stack trace:
#0 /var/www/html/core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php(99): assert(false, 'The container w...')
#1 [internal function]: Drupal\Core\DependencyInjection\ContainerBuilder->__sleep()
#2 Standard input code(89): serialize(Array)
#3 Standard input code(123): __phpunit_run_isolated_test()
#4 {main}
  thrown in /var/www/html/core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php on line 99

When I switched PHP to 7.3 locally I saw the actual error:

1) Drupal\Tests\styleswitcher\Kernel\Migrate\d7\MigrateVariablesTest::testVariablesMigration
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 /app/core/lib/Drupal/Core/Database/Connection.php:2042
Stack trace:
#0 [internal function]: Drupal\Core\Database\Connection->__sleep()
#1 Standard input code(80): serialize(Array)
#2 Standard input code(112): __phpunit_run_isolated_test()
#3 {main}
  thrown in /app/core/lib/Drupal/Core/Database/Connection.php on line 2042

The workaround was to call $this->startCollectingMessages(); before $this->executeMigrations(...).

johnv’s picture

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.